public class PlayBalloon
class Balloon
1: // PlayBalloon.java
2: // Simple illustration of MVC design pattern
4: import java.applet.Applet;
5: import java.awt.*;
6: import java.awt.event.*;
8: public class PlayBalloon
9: extends Applet
10: implements ActionListener
11: {
13: private Button grow, shrink, left, right;
14: private Balloon myBalloon;
16: public void init()
17: {
18: grow = new Button("Grow");
19: add(grow);
20: grow.addActionListener(this);
22: shrink = new Button("Shrink");
23: add(shrink);
24: shrink.addActionListener(this);
26: left = new Button("Left");
27: add(left);
28: left.addActionListener(this);
30: right = new Button("Right");
31: add(right);
32: right.addActionListener(this);
34: myBalloon = new Balloon();
35: }
38: public void actionPerformed(ActionEvent event)
39: {
40: if (event.getSource() == grow)
41: myBalloon.grow();
42: if (event.getSource() == shrink)
43: myBalloon.shrink();
44: if (event.getSource() == left)
45: myBalloon.left();
46: if (event.getSource() == right)
47: myBalloon.right();
48: repaint();
49: }
52: public void paint(Graphics g)
53: {
54: myBalloon.display(g);
55: }
56: }
59: class Balloon
60: {
62: private int diameter = 10;
63: private int xCoord = 20, yCoord = 50;
65: public void display(Graphics g)
66: {
67: g.drawOval(xCoord, yCoord, diameter, diameter);
68: }
70: public void left()
71: {
72: xCoord = xCoord - 10;
73: }
75: public void right()
76: {
77: xCoord = xCoord + 10;
78: }
80: public void grow()
81: {
82: diameter = diameter+5;
83: }
85: public void shrink()
86: {
87: diameter = diameter-5;
88: }
89: }