// Theresa Walunas // SE 450 Section 303, Tuesday 5:45 - 9:00 pm // Assignment #4, Due Date 5/09/00 // Source File Name: FallingStar2.java Class File: FallingStar2.class // Adding GUI widgits to class developed in Assignment#3: FallingStar Class // This class, FallingStar2.class contains four protected classes that // manage the Start and Stop buttons (ButtonHandler), the Choice widget // that selects the color of the star polygon (StarColorChoiceHandler), // the check box group widget that selects the background color // (BckGrndColorChkBoxHandler) and the MouseClickHandler class which // implements the MouseListener interface and allows the corner of the // 3D box that is changing size to be moved to the point on the canvas // where the mouse button was clicked. The FallingStarCanvas object is // the animated applet in a Canvas object. It's class file is separate. // FallingStar2.class also provides the layout parameters and instructions // in the FallingStar2 constructor. import java.awt.*; import java.awt.event.*; public class FallingStar2 extends java.applet.Applet implements Runnable{ // Create objects to be inserted into FallingStar2 layout handlers protected FallingStarCanvas canvas; // Object containing FallingStar animation protected Panel controlPanel; // Panel containing Start/Stop and Choice Widgets protected Panel bottomPanel; // Panel containing controlPanel and sky panels protected Panel sky; // Panel containing skycolor check boxes protected Label Title; // Title for box, North position protected Label Instructions; // Instructions for mouse click, in bottomPanel // Define font objects protected Font titlefont = new Font("Helvetica", Font.BOLD, 24); protected Font buttonfont = new Font("Helvetica", Font.PLAIN, 16); protected Font choicefont = new Font ("Helvetica", Font.PLAIN, 12); // Define buttons protected static final int START = 1; protected static final int STOP = 2; // Applet control elements Thread FallingThread; int delay; // Implements button actions. Clicking "Stop" stops the applet. // Clicking "Start" starts the applet from the beginning. protected class ButtonHandler implements ActionListener { private int cmd = 0; public ButtonHandler(int cmd){ this.cmd = cmd; } public void actionPerformed (ActionEvent event) { switch (cmd){ case START: start(); break; case STOP: stop(); break; } } } // Implements choice widget. Clicking indicated colors changes // polygon star object to fill with the indicated color. Clicking // "Random" causes a random color to be selected using r,g,b codes. protected class StarColorChoiceHandler implements ItemListener { public void itemStateChanged(ItemEvent event){ Choice choice = (Choice) event.getSource(); if (choice != null){ if ("Light Gray".equals(event.getItem())){ canvas.setStarColor(Color.lightGray); } else if ("Magenta".equals(event.getItem())){ canvas.setStarColor(Color.magenta); } else if ("Yellow".equals(event.getItem())){ canvas.setStarColor(Color.yellow); } else if ("Cyan".equals(event.getItem())){ canvas.setStarColor(Color.cyan); } else if ("Green".equals(event.getItem())){ canvas.setStarColor(Color.green); } else if ("Random".equals(event.getItem())){ canvas.setStarColor(new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256))); } } } } // Implements Check Box widget. Checkboxes are in a group and thus only // one box can be true at any one time. If "Night" is true, then the // background is black. If "Day" is true, then the background is yellow protected class BckGrndColorChkBoxHandler implements ItemListener { public void itemStateChanged(ItemEvent event){ Checkbox Ckbx = (Checkbox) event.getSource(); if (Ckbx != null) { if ("Night".equals(event.getItem())){ canvas.setBckGrndColor (Color.black); } else if ("Day".equals(event.getItem())){ canvas.setBckGrndColor (new Color (255, 255, 204)); } } } } // Implements the MouseListener. The only method that is used is the // mouseClicked method. When the portion of the applet containing the // FallingStar animation is clicked on, the top left corner of the // 3D recangle object in the animation is moved there. protected class MouseClickHandler implements MouseListener { public void mouseClicked (MouseEvent event){ Point p = event.getPoint(); canvas.setRectPosition(p); } // Must be defined but will not be used. public void mouseEntered (MouseEvent event){} public void mouseExited (MouseEvent event){} public void mousePressed (MouseEvent event){} public void mouseReleased (MouseEvent event){} } public FallingStar2 (){ // Creates main Layout setLayout (new BorderLayout()); canvas = new FallingStarCanvas(); add ("Center", canvas); Title = new Label("Catch a Falling Star", Label.CENTER); Title.setFont(titlefont); Title.setColor(Color.green); add ("North", Title); // Creates panel for controls. bottomPanel = new Panel(); bottomPanel.setLayout(new GridLayout(0,1)); controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); // Add Start Button Button startButton = new Button ("Start"); startButton.setFont(buttonfont); controlPanel.add(startButton); // Add Stop Button Button stopButton = new Button ("Stop"); stopButton.setFont(buttonfont); controlPanel.add(stopButton); // Create Choice widget and add color choices Choice starcolor = new Choice(); starcolor.setFont(choicefont); starcolor.addItem("Cyan"); starcolor.addItem("Green"); starcolor.addItem("Light Gray"); starcolor.addItem("Magenta"); starcolor.addItem("Yellow"); starcolor.addItem("Random"); controlPanel.add(starcolor); // Create check box widgets and group sky = new Panel(); sky.setLayout(new GridLayout(1,0)); CheckboxGroup skycolor = new CheckboxGroup(); Checkbox night = new Checkbox("Night", skycolor, true); Checkbox day = new Checkbox("Day", skycolor, false); night.setFont(buttonfont); day.setFont(buttonfont); sky.add(night); sky.add(day); controlPanel.add(sky); // Create instruction label bottomPanel.add(controlPanel); Instructions = new Label ("Click Anywhere to Move Box to that Location", Label.CENTER); bottomPanel.add(Instructions); // Add all controls to "South" in top level border layout add ("South", bottomPanel); // Register the listeners to the appropriate objects startButton.addActionListener(new ButtonHandler(START)); stopButton.addActionListener(new ButtonHandler(STOP)); starcolor.addItemListener(new StarColorChoiceHandler()); day.addItemListener (new BckGrndColorChkBoxHandler()); night.addItemListener (new BckGrndColorChkBoxHandler()); canvas.addMouseListener (new MouseClickHandler()); } public void init(){ // Parameterized frames per second (fps) String frames = getParameter ("fps"); if (frames != null){ delay = 1000/Integer.parseInt(frames); } else { delay = 100; } doLayout(); canvas.init(); } public void start(){ if (FallingThread == null){ FallingThread = new Thread(this); FallingThread.start(); } } public void stop(){ if (FallingThread != null){ FallingThread = null; } } public void run(){ while (Thread.currentThread() == FallingThread){ try { Thread.currentThread().sleep(delay); } catch (InterruptedException e){} canvas.repaint(); } } }