-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallThread.java
More file actions
46 lines (45 loc) · 1002 Bytes
/
BallThread.java
File metadata and controls
46 lines (45 loc) · 1002 Bytes
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
package xianchen;
import java.awt.*;
import java.applet.*;
@SuppressWarnings("serial")
public class BallThread extends Applet implements Runnable{
Thread red, blue;
Graphics redPen, bluePen;
int t=0;
public void init(){
red = new Thread(this);
blue = new Thread(this);
redPen = getGraphics();
bluePen = getGraphics();
redPen.setColor(Color.red);
bluePen.setColor(Color.blue);
}
public void start(){
red.start();
blue.start();
}
@SuppressWarnings("static-access")
public void run(){
while(true){
t=t+1;
if(Thread.currentThread()==red){ //ºìÏß³Ì
if(t>100)
t=0;
redPen.clearRect(0,0,110,400);
redPen.fillOval(0,(int)(1.0/2*t*9.8),20,20);
try{
red.sleep(40);
}
catch(InterruptedException e){}
}
else{ //Thread.currentThread()==blue À¶Ïß³Ì
bluePen.clearRect(120,0,900,500);
bluePen.fillOval(120+7*t,(int)(1.0/2*t*9.8),20,20);
try{
blue.sleep(40);
}
catch(InterruptedException e){}
}
}
}
}