坦克动起来了——事件处理机制

/*
* 坦克游戏1.01版本
* 1在我的面板MyPanel中缺少了构造函数
* 2在java中的坐标y轴式向下为正方向递增的与普通的数学坐标差别就在这了
*/
package com.TankGame01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyTankGame extends JFrame {
   
    MyPanel mp=null;
public static void main(String[] args){
// TODO Auto-generated method stub
new MyTankGame();   
}
//构造函数
    public MyTankGame(){
    mp=new MyPanel();
    //往JFrame添加我的面板MyJPanel
    this.add(mp);
    //注册监听器
    this.addKeyListener(mp);
    //设置JFrame容器大小
    this.setSize(600,500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //设置面板可见
    this.setVisible(true);
    }
}
class MyPanel extends JPanel implements KeyListener{
Hero hero=null;
public MyPanel(){
hero=new Hero(100,100);
}
//实现paint方法和画笔工具Graphics
public void paint(Graphics g){
super.paint(g);
g.fillRect(0,0,400,400);
this.MakeTank(this.hero.getX(), this.hero.getY(), g, this.hero.direct, 0);

}
public void MakeTank(int x,int y,Graphics g,int direct,int type){
switch(type){
case 0:
g.setColor(Color.yellow);
break;
case 1:
g.setColor(Color.blue);
break;
}
switch(direct){
//0表示向上1表示向右2表示向下3表示向左
case 0:
//画出左边的矩形
g.fill3DRect(x, y, 10, 40, true);
//画出右边的矩形
g.fill3DRect(x+30, y, 10, 40, true);
//画出中间的矩形
g.fill3DRect(x+10, y+10, 20, 20, true);
//画出中间的圆形
g.setColor(Color.red);
g.fillOval(x+12, y+12, 16, 16);
//画出中间的直线
g.drawLine(x+20, y, x+20, y+20);
break;
case 1:
//画出上边的矩形
g.fill3DRect(x, y, 40, 10, true);
//画出下边的矩形
g.fill3DRect(x, y+30, 40, 10, true);
//画出中间的矩形
g.fill3DRect(x+10, y+10, 20, 20, true);
//画出中间的圆形
g.setColor(Color.red);
g.fillOval(x+12, y+12, 16, 16);
//画出中间的直线
g.drawLine(x+20,y+20,x+40,y+20);
break;
case 2:
//画出左边的矩形
g.fill3DRect(x, y, 10, 40, true);
//画出右边的矩形
g.fill3DRect(x+30, y, 10, 40, true);
//画出中间的矩形
g.fill3DRect(x+10, y+10, 20, 20, true);
//画出中间的圆形
g.setColor(Color.red);
g.fillOval(x+12, y+12, 16, 16);
//画出中间的直线
g.drawLine(x+20, y+20, x+20, y+40);
break;
case 3:
//画出上边的矩形
g.fill3DRect(x, y, 40, 10, true);
//画出下边的矩形
g.fill3DRect(x, y+30, 40, 10, true);
//画出中间的矩形
g.fill3DRect(x+10, y+10, 20, 20, true);
//画出中间的圆形
g.setColor(Color.red);
g.fillOval(x+12, y+12, 16, 16);
//画出中间的直线
g.drawLine(x,y+20,x+20,y+20);
break;
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_W){
this.hero.moveUp();
this.hero.setDirect(0);
}else if(e.getKeyCode()==KeyEvent.VK_D){
this.hero.moveRight();
this.hero.setDirect(1);
}else if(e.getKeyCode()==KeyEvent.VK_S){
this.hero.moveDown();
this.hero.setDirect(2);
}else if(e.getKeyCode()==KeyEvent.VK_A){
this.hero.moveLeft();
this.hero.setDirect(3);
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}
}
class Tank{
//得到坦克的坐标
//利用set与get方法
int x=0;
int y=0;
public Tank(int x,int y){
this.x=x;
this.y=y;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}
    //坦克炮筒的方向
int direct=0;
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
    //坦克的上下左右移动
    int speed=5;
}
class Hero extends Tank{
public Hero(int x,int y){
super(x,y);
}
    public void moveUp(){
    y-=speed;
    }
    public void moveRight(){
    x+=speed;
    }
    public void moveDown(){
    y+=speed;
    }
    public void moveLeft(){
    x-=speed;
    }
}

你可能感兴趣的:(游戏,swing)