50,528
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import javax.swing.*;
public class TestThread2 extends JApplet implements Runnable{
private Image ImageMaker=null;
private Graphics ImageGraphics=null;
Thread Threadme=null;
private int x=0;
private final static int y=225;
private final static int x1=450;
private int y1=225;
private int move1=1;
private int move2=1;
public void init(){
ImageMaker=this.createImage(600,600);
ImageGraphics=ImageMaker.getGraphics();
}
public void start(){
if(Threadme==null){
Threadme=new Thread(this);
Threadme.start();
}
}
public void run(){
Thread myThread=Thread.currentThread();
while(Threadme==myThread){
x+=move1;
y1+=move2;
if(x>=450||x<=0){move1*=-1;}
if(y1>=450||y1<=0){move2*=-1;}
repaint();
}
}
public void drawOval(Graphics g){
Graphics2D g1=(Graphics2D)g;
g1.setColor(Color.magenta);
g1.fillRect(0,0,300,300);
g1.setColor(Color.green);
g1.fillRect(300,0,300,300);
g1.setColor(Color.black);
g1.fillRect(0,300,300,300);
g1.setColor(Color.BLUE);
g1.fillRect(300,300,300,300);
g1.setColor(Color.YELLOW);
g1.fillOval(x,y,150,150);
g1.setColor(Color.orange);
g1.fillOval(x1,y1,150,150);
}
public void paint(Graphics ImageGraphics){
//ImageGraphics.setColor(Color.white);
//ImageGraphics.fillRect(0,0,0,0);
drawOval(ImageGraphics);
ImageGraphics.drawImage(ImageMaker,600,600,this);
}
}