Source of PlayBalloonApplication.java


  1: // PlayBalloonApplication.java
  2: // An AWT application version of the PlayBalloon example.
  3: // Allows the user to "grow" and/or "shrink" the balloon.

  5: import java.awt.*;
  6: import java.awt.event.*;

  8: public class PlayBalloonApplication
  9:     extends Frame
 10:     implements ActionListener, WindowListener
 11: {
 12:     private Button grow, shrink;
 13:     private Balloon myBalloon;


 16:     public static void main(String[] args)
 17:     {
 18:         PlayBalloonApplication app = new PlayBalloonApplication();
 19:         app.setSize(200, 200);
 20:         app.setVisible(true);
 21:     }


 24:     public  PlayBalloonApplication()
 25:     {
 26:         setTitle("Balloon");
 27:         setLayout(new FlowLayout());
 28:         addWindowListener(this);

 30:         grow   = new Button("Grow");
 31:         shrink = new Button("Shrink");
 32:         grow.addActionListener(this);
 33:         shrink.addActionListener(this);
 34:         add(grow);
 35:         add(shrink);

 37:         myBalloon = new Balloon(40, 80, 80);
 38:     }


 41:     public void actionPerformed(ActionEvent event)
 42:     {
 43:         if (event.getSource() == grow)
 44:             myBalloon.changeSize(5);
 45:         if (event.getSource() == shrink)
 46:             myBalloon.changeSize(-5);
 47:         repaint();
 48:     }


 51:     public void windowClosing(WindowEvent event)
 52:     {
 53:         System.exit(0);
 54:     }
 55:     public void windowClosed(WindowEvent event)     {}
 56:     public void windowDeiconified(WindowEvent event){}
 57:     public void windowIconified(WindowEvent event)  {}
 58:     public void windowActivated(WindowEvent event)  {}
 59:     public void windowDeactivated(WindowEvent event){}
 60:     public void windowOpened(WindowEvent event)     {}

 62: 
 63:     public void paint(Graphics g)
 64:     {
 65:         myBalloon.display(g);
 66:     }
 67: }


 70: class Balloon
 71: {
 72:     private int diameter;
 73:     private int xCoord, yCoord;

 75:     Balloon(int initialDiameter, int initialX, int initialY)
 76:     {
 77:         diameter = initialDiameter;
 78:         xCoord = initialX;
 79:         yCoord = initialY;
 80:     }

 82:     public void changeSize(int change)
 83:     {
 84:         diameter += change;
 85:     }

 87:     public void display(Graphics g)
 88:     {
 89:         g.drawOval (xCoord, yCoord, diameter, diameter);
 90:     }
 91: }