import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class PlayBalloon extends JFrame implements ActionListener { /** Size change buttons. */ private JButton grow, shrink; /** Four movement buttons. */ private JButton moveLeft, moveRight, moveUp, moveDown; /** To flip picture <-> description mode. */ private JButton mode; /** 'Flag' indicating the current mode: * true = show a picture, false = show a description. */ private boolean pictureMode = true; /** Panel for drawing on. */ private JPanel panel; /** To hold a single instance of the Balloon class. */ private Balloon myBalloon; /** Storage for a balloon 'party'. */ // private final int partySize = 10; // Number of balloons in the party // private Balloon[] party; // There will be one balloon per element of this array /** * The main program for the PlayBalloon class * * @param args The command line arguments */ public static void main( String[] args ) { PlayBalloon play = new PlayBalloon(); play.setSize( 320, 430 ); play.createGUI(); play.setVisible( true ); } // End of main /** Set up the Graphical user interface and balloon information */ private void createGUI() { setDefaultCloseOperation( EXIT_ON_CLOSE ); Container window = getContentPane(); window.setLayout( new FlowLayout() ); grow = new JButton( "Grow" ); window.add( grow ); grow.addActionListener( this ); shrink = new JButton( "Shrink" ); window.add( shrink ); shrink.addActionListener( this ); moveLeft = new JButton( "Left" ); window.add( moveLeft ); moveLeft.addActionListener( this ); moveRight = new JButton( "Right" ); window.add( moveRight ); moveRight.addActionListener( this ); moveUp = new JButton( "Up" ); window.add( moveUp ); moveUp.addActionListener( this ); moveDown = new JButton( "Down" ); window.add( moveDown ); moveDown.addActionListener( this ); mode = new JButton( "Picture/description mode" ); window.add( mode ); mode.addActionListener( this ); panel = new JPanel(); panel.setPreferredSize( new Dimension( 300, 300 ) ); panel.setBackground( Color.white ); window.add( panel ); // Create a single balloon object myBalloon = new Balloon( 20, 50, 50, Color.black ); // Set up the balloon party // party = new Balloon[partySize]; // for (int b = 0; b < partySize; b++) { // Set up party of balloons // Color randomColour = new Color((int)(Math.random()*256), // Red component 0-255 // (int)(Math.random()*256), // Blue component 0-255 // (int)(Math.random()*256)); // Green component 0-255 // party[b] = new Balloon( // Create a balloon with random size, position and colour: // (int) (Math.random()*30+10), // size 10-39 // (int) (Math.random()*200), // x 0-199 // (int) (Math.random()*200), // y 0-199 // randomColour); // } } // End of createGUI /** * React to the various GUI buttons: * Update balloon or mode information as necessary and redraw the screen. * * @param event The event details */ public void actionPerformed( ActionEvent event ) { if ( event.getSource() == grow ) myBalloon.changeSize( 10 ); // Tell myBalloon to become larger if ( event.getSource() == shrink ) myBalloon.changeSize( -10 ); // Tell myBalloon to become smaller if (event.getSource() == moveLeft) myBalloon.move ( -10, 0 ); // Tell myBalloon to adjust its x coordinate by -10 and y by 0 if (event.getSource() == moveRight) myBalloon.move ( +10, 0 ); //... if (event.getSource() == moveUp) myBalloon.move ( 0, -10); if (event.getSource() == moveDown) myBalloon.move (0, +10); if ( event.getSource() == mode ) pictureMode = !pictureMode; // Flip the mode // Note: ! is logical "not" (negation) // Finally redraw the screen to show any changes paintScreen(); } // End of actionPerformed /** Redraw the graphics panel */ private void paintScreen() { Graphics g = panel.getGraphics(); g.setColor( Color.white ); g.fillRect( 0, 0, 300, 300 ); // Tell myBalloon to draw itself on g myBalloon.display( g ); myBalloon.describe( g ); // Insert code to draw the whole balloon party here } // End of paintScreen } // End of class PlayBalloon