/
RoadApplet.java
174 lines (155 loc) · 4 KB
/
RoadApplet.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.awt.*;
import java.applet.*;
public class RoadApplet extends Applet implements Runnable
{
public void init()
{
canvas = new RoadCanvas();
slowdown = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 100);
slowdown.setPageIncrement(10);
slowdown.setValue(10);
arrival = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 100);
arrival.setPageIncrement(10);
arrival.setValue(50);
Panel p = new Panel();
p.setLayout(new GridLayout(1, 6));
p.add(new Label("Slowdown"));
p.add(slowdown);
p.add(new Label(""));
p.add(new Label("Arrival"));
p.add(arrival);
p.add(new Label(""));
setLayout(new BorderLayout());
add("North", p);
add("Center", canvas);
}
public double getSlowdown()
{
return 0.01 * slowdown.getValue();
}
public double getArrival()
{
return 0.01 * arrival.getValue();
}
public void run()
{
for(;;)
{
canvas.update(getSlowdown(), getArrival());
try { Thread.sleep(50); } catch(InterruptedException e) {}
}
}
public void start()
{
runner = new Thread(this);
runner.start();
}
private RoadCanvas canvas;
private Scrollbar slowdown;
private Scrollbar arrival;
private Thread runner;
}
class RoadCanvas extends Canvas
{
public RoadCanvas()
{
freeway = new Road();
row = 0;
}
public void update(double slowdown, double arrival)
{
freeway.update(slowdown, arrival);
if (buffer == null)
{
xsize = size().width;
ysize = size().height;
buffer = createImage(xsize, ysize);
}
Graphics bg = buffer.getGraphics();
freeway.paint(bg, row, XDOTDIST, DOTSIZE);
if (row < ysize - 2 * DOTSIZE + 1)
row += DOTSIZE;
else
{
bg.copyArea(0, DOTSIZE, xsize, ysize - DOTSIZE, 0, -DOTSIZE);
bg.clearRect(0, ysize - DOTSIZE, xsize, DOTSIZE);
}
bg.dispose();
repaint();
}
public void paint(Graphics g)
{
if (buffer != null)
g.drawImage(buffer, 0, 0, null);
}
public void update(Graphics g)
{
paint(g);
}
private final int DOTSIZE = 2;
private final int XDOTDIST = 1;
private Road freeway;
private Image buffer;
private int row;
private int xsize;
private int ysize;
}
class Road
{
public Road()
{
speed = new int[LENGTH];
colors = new Color[LENGTH];
for (int i = 0; i < LENGTH; i++) speed[i] = -1;
}
public void update(double prob_slowdown, double prob_create)
{
int i = 0;
while(i < LENGTH && speed[i] == -1)
i++;
while (i < LENGTH)
{
if (Math.random() <= prob_slowdown && speed[i] > 0)
speed[i]--;
else if (speed[i] < MAXSPEED)
speed[i]++;
int inext = i + 1;
while(inext < LENGTH && speed[inext] == -1)
inext++;
if (inext < LENGTH)
{
if (speed[i] >= inext - i)
speed[i] = inext - i - 1;
}
if (speed[i] > 0)
{
if (i + speed[i] < LENGTH)
{
int ni = i + speed[i];
speed[ni] = speed[i];
colors[ni] = colors[i];
}
speed[i] = -1;
}
i = inext;
}
if (Math.random() <= prob_create && speed[0] == -1)
{
speed[0] = (int)(5.99 * Math.random());
colors[0] = ++count % 10 == 0 ? Color.red : Color.black;
}
}
public void paint(Graphics g, int row, int dotdist, int dotsize)
{
for (int i = 0; i < LENGTH; i++)
{
g.setColor(colors[i]);
if (speed[i] >= 0) g.fillRect(i * dotdist, row, dotsize, dotsize);
}
}
public static final int LENGTH = 400;
public static final int MAXSPEED = 5;
private int[] speed;
private Color[] colors;
private int count;
}