import java.awt.*;
import java.awt.event.*;
public class window extends Frame//建立主类
{
private Image iBuffer;
private Graphics gBuffer;
int x=290,y=0,x1=290,y1=0;
public paintThread pT;
public paintThread1 pT1;
public window()//构造函数
{
pT=new paintThread(this);//开辟线程
pT1=new paintThread1(this,300);
this.setTitle("move");
this.setBounds(50,50,300,300);
this.addWindowListener(new closeWin());
this.setVisible(true);
pT.start();//开启线程
pT1.start();
}
public void paint(Graphics g)//绘图方法
{
if(iBuffer==null)
{
iBuffer=createImage(this.getSize().width,this.getSize().height);
gBuffer=iBuffer.getGraphics();
}
gBuffer.setColor(getBackground());
gBuffer.fillRect(0,0,this.getSize().width,this.getSize().height);
gBuffer.setColor(Color.RED);
gBuffer.fillOval(x,y,10,10);//绘制圆
gBuffer.setColor(Color.blue);
gBuffer.fillRect(x1,y1,10,10);
g.drawImage(iBuffer,0,0,this);
}
public void update(Graphics g)//重载绘图函数
{
paint(g);
}
public static void main(String[] args)//主算法
{
window a = new window();
}
}
class paintThread extends Thread//绘图线程类
{
window a;
public paintThread( window a) //构造函数
{
this.a=a;
}
public void run()//重载run()函数
{
while(true)//线程中的无限循环
{
try
{
sleep(30); //线程休眠30ms
}
catch(InterruptedException e){}
a.y+=1; //修改小圆左上角的纵坐标
a.x-= 1;
if(a.y>300) //小圆离开窗口后重设左上角的纵坐标
{
a.y=0;
a.x=290;
}
a.repaint();//窗口重绘
}
}
}
class closeWin extends WindowAdapter//关闭窗口
{
public void windowClosing(WindowEvent e)
{
Frame frm =(Frame)(e.getSource());
frm.dispose();
System.exit(0);
}
}
class paintThread1 extends Thread//绘图线程类
{
int time;
window a;
public paintThread1( window a,int time) //构造函数
{
this.a=a;
this.time=time;
}
public void run()//重载run()函数
{
while(true)//线程中的无限循环
{
try
{
sleep(time); //线程休眠30ms
}
catch(InterruptedException e){}
a.y1+=1; //修改小圆左上角的纵坐标
a.x1-= 1;
if(a.y>300) //小圆离开窗口后重设左上角的纵坐标
{
a.y1=0;
a.x1=290;
}
a.repaint();//窗口重绘
}
}
}
import java.awt.event.*;
public class window extends Frame//建立主类
{
private Image iBuffer;
private Graphics gBuffer;
int x=290,y=0,x1=290,y1=0;
public paintThread pT;
public paintThread1 pT1;
public window()//构造函数
{
pT=new paintThread(this);//开辟线程
pT1=new paintThread1(this,300);
this.setTitle("move");
this.setBounds(50,50,300,300);
this.addWindowListener(new closeWin());
this.setVisible(true);
pT.start();//开启线程
pT1.start();
}
public void paint(Graphics g)//绘图方法
{
if(iBuffer==null)
{
iBuffer=createImage(this.getSize().width,this.getSize().height);
gBuffer=iBuffer.getGraphics();
}
gBuffer.setColor(getBackground());
gBuffer.fillRect(0,0,this.getSize().width,this.getSize().height);
gBuffer.setColor(Color.RED);
gBuffer.fillOval(x,y,10,10);//绘制圆
gBuffer.setColor(Color.blue);
gBuffer.fillRect(x1,y1,10,10);
g.drawImage(iBuffer,0,0,this);
}
public void update(Graphics g)//重载绘图函数
{
paint(g);
}
public static void main(String[] args)//主算法
{
window a = new window();
}
}
class paintThread extends Thread//绘图线程类
{
window a;
public paintThread( window a) //构造函数
{
this.a=a;
}
public void run()//重载run()函数
{
while(true)//线程中的无限循环
{
try
{
sleep(30); //线程休眠30ms
}
catch(InterruptedException e){}
a.y+=1; //修改小圆左上角的纵坐标
a.x-= 1;
if(a.y>300) //小圆离开窗口后重设左上角的纵坐标
{
a.y=0;
a.x=290;
}
a.repaint();//窗口重绘
}
}
}
class closeWin extends WindowAdapter//关闭窗口
{
public void windowClosing(WindowEvent e)
{
Frame frm =(Frame)(e.getSource());
frm.dispose();
System.exit(0);
}
}
class paintThread1 extends Thread//绘图线程类
{
int time;
window a;
public paintThread1( window a,int time) //构造函数
{
this.a=a;
this.time=time;
}
public void run()//重载run()函数
{
while(true)//线程中的无限循环
{
try
{
sleep(time); //线程休眠30ms
}
catch(InterruptedException e){}
a.y1+=1; //修改小圆左上角的纵坐标
a.x1-= 1;
if(a.y>300) //小圆离开窗口后重设左上角的纵坐标
{
a.y1=0;
a.x1=290;
}
a.repaint();//窗口重绘
}
}
}