aboutsummaryrefslogtreecommitdiffstats
path: root/sample/duplicate/Viewer.java
blob: aec13f6b56a250120b119a36f463a726c1b255ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package sample.duplicate;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Viewer extends Applet
    implements MouseListener, ActionListener, WindowListener
{
    private static final Color[] colorList = {
	Color.orange, Color.pink, Color.green, Color.blue };

    private Ball ball;
    private int colorNo;

    public void init() {
	colorNo = 0;
	Button b = new Button("change");
	b.addActionListener(this);
	add(b);

	addMouseListener(this);
    }

    public void start() {
	ball = new Ball(50, 50);
	ball.changeColor(colorList[0]);
    }

    public void paint(Graphics g) {
	ball.paint(g);
    }

    public void mouseClicked(MouseEvent ev) {
	ball.move(ev.getX(), ev.getY());
	repaint();
    }

    public void mouseEntered(MouseEvent ev) {}

    public void mouseExited(MouseEvent ev) {}

    public void mousePressed(MouseEvent ev) {}

    public void mouseReleased(MouseEvent ev) {}

    public void actionPerformed(ActionEvent e) {
	ball.changeColor(colorList[++colorNo % colorList.length]);
	repaint();
    }

    public void windowOpened(WindowEvent e) {}

    public void windowClosing(WindowEvent e) {
	System.exit(0);
    }

    public void windowClosed(WindowEvent e) {}

    public void windowIconified(WindowEvent e) {}

    public void windowDeiconified(WindowEvent e) {}

    public void windowActivated(WindowEvent e) {}

    public void windowDeactivated(WindowEvent e) {}

    public static void main(String[] args) {
	Frame f = new Frame("Viewer");
	Viewer view = new Viewer();
	f.addWindowListener(view);
	f.add(view);
	f.setSize(300, 300);
	view.init();
	view.start();
	f.setVisible(true);
    }
}