您的位置:首页 > 其它

J2SE 坦克大战 马士兵(4)

2012-03-15 20:24 507 查看
项目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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: