-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle.java
More file actions
185 lines (167 loc) · 4.12 KB
/
circle.java
File metadata and controls
185 lines (167 loc) · 4.12 KB
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
175
176
177
178
179
180
181
182
183
184
185
import java.applet.Applet;
import java.awt.*;
import java.io.PrintStream;
import java.util.Random;
public class circle extends Applet
implements Runnable
{
public class ball
{
public void moveball()
{
if(xdir == 0)
x += xinc;
else
x -= xinc;
if(ydir == 0)
y += yinc;
else
y -= yinc;
if(x >= appwidth - (circlewidth + inc))
{
xdir = 1;
xinc = randinc(inc) + 1;
x -= xinc;
}
if(x <= inc)
{
xdir = 0;
xinc = randinc(inc) + 1;
x += xinc;
}
if(y >= appheight - (circleheight + inc))
{
ydir = 1;
yinc = randinc(inc) + 1;
y -= yinc;
}
if(y <= inc)
{
ydir = 0;
yinc = randinc(inc) + 1;
y += yinc;
}
}
public int randinc(int i)
{
return Math.abs(rand.nextInt() % i);
}
int x;
int y;
int circlewidth;
int circleheight;
int xdir;
int ydir;
int xinc;
int yinc;
int red;
int green;
int blue;
Color ballcolor;
Random rand;
public ball()
{
rand = new Random(System.currentTimeMillis());
x = randinc(appwidth);
y = randinc(appheight);
circlewidth = randinc(circle.girth) + 10;
circleheight = circlewidth;
xdir = randinc(2);
ydir = randinc(2);
xinc = randinc(inc) + 1;
yinc = randinc(inc) + 1;
red = randinc(circle.color);
green = randinc(circle.color);
blue = randinc(circle.color);
ballcolor = new Color(red, green, blue);
}
}
public circle()
{
inc = 17;
}
public void init()
{
if(appwidth == 0)
{
appwidth = 400;
appheight = 400;
} else
{
appwidth = 300;
appheight = 300;
}
marbles = new ball[ballcount];
for(int i = 0; i < ballcount; i++)
{
marbles[i] = new ball();
try
{
Thread.sleep(100L);
}
catch(Exception _ex)
{
System.out.println("main sleep problem");
}
}
offscreenimage = createImage(appwidth, appheight);
}
public synchronized void paint(Graphics g)
{
g.setXORMode(getBackground());
for(int i = 0; i < ballcount; i++)
marbles[i].moveball();
for(int j = 0; j < ballcount; j++)
{
g.setColor(marbles[j].ballcolor);
g.fillOval(marbles[j].x, marbles[j].y, marbles[j].circlewidth, marbles[j].circleheight);
}
}
public void run()
{
while(bouncer != null)
{
try
{
Thread.sleep(100L);
}
catch(Exception _ex)
{
System.out.println("Sleep problem");
}
repaint();
}
}
public void start()
{
if(bouncer == null)
{
bouncer = new Thread(this);
bouncer.start();
}
}
public void stop()
{
bouncer = null;
}
public void update(Graphics g)
{
offscreenGraphics = offscreenimage.getGraphics();
offscreenGraphics.setColor(getBackground());
offscreenGraphics.fillRect(0, 0, appwidth, appheight);
offscreenGraphics.setColor(g.getColor());
paint(offscreenGraphics);
g.drawImage(offscreenimage, 0, 0, this);
}
Thread bouncer;
ball marbles[];
Image offscreenimage;
Graphics offscreenGraphics;
int inc;
static int color = 256;
static int girth = 65;
static int tall = 65;
static int ballcount = 30;
int appwidth;
int appheight;
}