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
}
}
代码:
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
}
}
相关文章推荐
- J2SE 坦克大战 马士兵(4)
- J2SE 坦克大战 马士兵(5)
- J2SE 坦克大战 马士兵(1)
- J2SE 坦克大战 马士兵(2)
- J2SE 坦克大战 马士兵(3)
- 马士兵的坦克大战,以前做过,今天又找出来看了看
- nyoj 284-坦克大战
- 脚本游戏之四: 坦克大战源码注释(待续。。。)
- 自学Unity笔记 坦克大战项目
- java坦克大战0.3(画出代表坦克的实心圆)
- 坦克大战 python 游戏
- 坦克大战,使敌人坦克随机运动
- cocos2d-x的初步学习十六之坦克大战三
- nyo-284-坦克大战--(bfs+优先队列)
- NUOJ 284 坦克大战【BFS】
- 记录Java学习过程之坦克大战1
- NYOJ 284--坦克大战【BFS && 优先队列】
- java视频 坦克大战制作
- 坦克大战中,给子弹加载图片
- NYOJ-284 坦克大战 ------BFS/优先队列