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