You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Viewer.java 1.7KB

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