-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chase.java
executable file
·117 lines (95 loc) · 2.54 KB
/
Chase.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class Chase extends Applet implements Runnable, MouseMotionListener
{
private static final int NUMBER_OF_DOTS=300;
private Thread workThread;
private boolean running;
private int currentMouseX=0;
private int currentMouseY=0;
private Vector dots;
//Where instance variables are declared:
Dimension offDimension;
Image offImage;
Graphics offGraphics;
public void init()
{
// System.out.println("Inited");
running = false;
dots = new Vector();
for(int i=0; i<NUMBER_OF_DOTS; i++) {
dots.addElement(new Dot());
}
addMouseMotionListener(this);
}
public void start()
{
// System.out.println("Started");
if(!running) {
running = true;
workThread = new Thread(this);
workThread.start();
}
}
public void stop()
{
// System.out.println("Stopped");
running = false;
}
public void destroy()
{
// System.out.println("Destroyed");
}
public void run()
{
Dot d;
// System.out.println("Entering thread");
while(running) {
try{
Thread.sleep(50);
} catch (InterruptedException ie) {}
// System.out.println("Pip");
repaint();
}
workThread = null;
// System.out.println("Leaving thread");
}
public void update(Graphics g) {
//In the update() method, where d holds the size of the
//onscreen drawing area:
Dimension dim = getSize();
if ( (offGraphics == null)
|| (dim.width != offDimension.width)
|| (dim.height != offDimension.height) ) {
offDimension = dim;
offImage = createImage(dim.width, dim.height);
offGraphics = offImage.getGraphics();
}
offGraphics.setColor(getBackground());
offGraphics.fillRect(0, 0, dim.width, dim.height);
offGraphics.setColor(Color.black);
// Draw each dot
Dot d;
for (Enumeration e = dots.elements() ; e.hasMoreElements() ;) {
d = (Dot)e.nextElement();
d.updateDot(currentMouseX, currentMouseY);
offGraphics.drawLine(d.getX(), d.getY(), d.getX(), d.getY());
}
g.drawImage(offImage, 0, 0, this);
}
public void mouseDragged(MouseEvent e)
{
// Potential bug. Mouseposition may be outside the canvas.
// System.out.println("Tút");
currentMouseX=e.getX();
currentMouseY=e.getY();
}
public void mouseMoved(MouseEvent e)
{
// System.out.println("Tut");
currentMouseX=e.getX();
currentMouseY=e.getY();
}
}