/Users/lyon/j4p/src/javassist/sample/duplicate/Viewer.java

1    package javassist.sample.duplicate; 
2     
3    import java.applet.*; 
4    import java.awt.*; 
5    import java.awt.event.*; 
6     
7    public class Viewer extends Applet 
8            implements MouseListener, ActionListener, WindowListener { 
9        private static final Color[] colorList = { 
10           Color.orange, Color.pink, Color.green, Color.blue}; 
11    
12       private Ball ball; 
13       private int colorNo; 
14    
15       public void init() { 
16           colorNo = 0; 
17           Button b = new Button("change"); 
18           b.addActionListener(this); 
19           add(b); 
20    
21           addMouseListener(this); 
22       } 
23    
24       public void start() { 
25           ball = new Ball(50, 50); 
26           ball.changeColor(colorList[0]); 
27       } 
28    
29       public void paint(Graphics g) { 
30           ball.paint(g); 
31       } 
32    
33       public void mouseClicked(MouseEvent ev) { 
34           ball.move(ev.getX(), ev.getY()); 
35           repaint(); 
36       } 
37    
38       public void mouseEntered(MouseEvent ev) { 
39       } 
40    
41       public void mouseExited(MouseEvent ev) { 
42       } 
43    
44       public void mousePressed(MouseEvent ev) { 
45       } 
46    
47       public void mouseReleased(MouseEvent ev) { 
48       } 
49    
50       public void actionPerformed(ActionEvent e) { 
51           ball.changeColor(colorList[++colorNo % colorList.length]); 
52           repaint(); 
53       } 
54    
55       public void windowOpened(WindowEvent e) { 
56       } 
57    
58       public void windowClosing(WindowEvent e) { 
59           System.exit(0); 
60       } 
61    
62       public void windowClosed(WindowEvent e) { 
63       } 
64    
65       public void windowIconified(WindowEvent e) { 
66       } 
67    
68       public void windowDeiconified(WindowEvent e) { 
69       } 
70    
71       public void windowActivated(WindowEvent e) { 
72       } 
73    
74       public void windowDeactivated(WindowEvent e) { 
75       } 
76    
77       public static void main(String[] args) { 
78           Frame f = new Frame("Viewer"); 
79           Viewer view = new Viewer(); 
80           f.addWindowListener(view); 
81           f.add(view); 
82           f.setSize(300, 300); 
83           view.init(); 
84           view.start(); 
85           f.setVisible(true); 
86       } 
87   } 
88