62,621
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import javax.swing.*;
class MyWindow extends JFrame implements Runnable
{
int x = 10, y;
Thread a = new Thread(this);
Thread b = new Thread(this);
MyWindow()
{
this.setBackground(Color.white);
this.setBounds(200, 200, 200, 200);
this.setVisible(true);
a.start();
b.start();
}
public void paint(Graphics g)
{
super.paint(g);
Color c = g.getColor();
g.setColor(Color.BLUE);
g.fillRect(x, y, 20, 15);
g.setColor(c);
}
public void run()
{
while(true)
{synchronized(this){
if(Thread.currentThread() == a)
{
x = x + 2;
y = 35;
}
else if(Thread.currentThread() == b)
{
x = x + 2;
y = 60;
}
repaint(1, 0, 0, 200, 200);
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}}
}
}
class B
{
public static void main(String[] args)
{
MyWindow w = new MyWindow();
}
}