您的位置:首页 > 其它

J2SE 坦克大战 马士兵(5)

2012-03-15 20:30 357 查看
项目6要求:



代码:

public class TankClient extends Frame{

public final int GAME_WIDTH = 800;

public final int GAME_HEIGHT = 600;//用来表名坦克的宽和高

int x = 50;

int y = 50 ;//用来暂时表现坦克的运动

public void lanuchFrame(){

this.setLocation(300,200);

this.setSize(GAME_WIDTH ,GAME_HEIGHT);

this.setResizable(false);

this.setBackground(Color.GREEN);

setVisible(true);

this.addKeyListener(new KeyMonitor());

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(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

private class KeyMonitor extends KeyAdapter{

public void keyPressed(KeyEvent e) {

int key = e.getKeyCode();

switch(key){

case KeyEvent.VK_LEFT: x-=5;

break;

case KeyEvent.VK_UP :y -=5;

break;

case KeyEvent.VK_RIGHT:x+=5;

break;

case KeyEvent.VK_DOWN:y+=5;

break;

}

}//添加键盘事件,java.awt.event

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: