项目4要求
代码如下:
public class TankClient extends Frame{
int x = 50;
int y = 50 ;
public void lanuchFrame(){
this.setLocation(300,200);
this.setSize(800,600);
this.setResizable(false);
this.setBackground(Color.GREEN);
setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Frame f =(Frame)e.getSource();
f.setVisible(false);
}
});
new Thread(new PaintThread()).start();
}
public void paint(Graphics g){
Color c =g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
y+=5;
}
public static void main(String[] args) {
new TankClient().lanuchFrame();
}
private class PaintThread implements Runnable{
public void run() {
while(true){
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
//repaint方法,通过一个线程来不断地重画,调用的顺序为repaint ->update(Graphics g)-> paint(Graphics g)
//并且由于该方法是内部类,可以直接访问外部类的属性与方法,因为生成内部类的对象
//启动线程的方法:实现Runnable或者继承Thread。