package com.Tank;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;public class MainTank extends JFrame{

/**
 * 
 */
MyPanel mp=null;
private static final long serialVersionUID = 8654325620366756927L; public static void main(String[] args){

new MainTank();
}

//构造函数
public MainTank()
{
//创建我的面板
mp=new MyPanel();
//添加我的面板到窗口
this.add(mp);
//注册监听
this.addKeyListener(mp);
//设置窗口的大小
this.setSize(400,300);
//设置窗口标题
this.setTitle("坦克大战");
//初始化窗口的位置
this.setLocation(200,200);
//设置不允许用户调整窗口大小
this.setResizable(false);
//设置关闭窗口并释放内存
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口为可见
this.setVisible(true);
}}
//我的面板
class MyPanel extends JPanel implements KeyListener
{
MyTank mt=null;
private static final long serialVersionUID = 1L;
//MyPanel的构造函数
public MyPanel()
{
mt=new MyTank(100,100);
}
//重写paint函数
public void paint(Graphics g)
{
super.paint(g);
//设置背景颜色
g.setColor(Color.BLACK);
//设置活动区域
g.fillRect(0, 0, 400, 300);
this.drawTank(this.mt.getX(), this.mt.getY(), g, this.mt.getDirection(), this.mt.getColor());
this.repaint();
}
//画坦克
public void drawTank(int x,int y,Graphics g,int direction,int color)
{
//设置画笔颜色
switch(color)
{
case 0:
g.setColor(Color.YELLOW);
break;
case 1:
g.setColor(Color.RED);
break;
}
switch(direction)
{
//坦克向上
case 0:
g.fill3DRect(x-20, y-20, 10, 40, true);
g.fill3DRect(x+10, y-20, 10, 40, true);
g.fill3DRect(x-10, y-10, 20, 20, true);
g.drawOval(x-5, y-5, 10, 10);
g.drawLine(x, y, x, y-20);
//坦克向右
case 1:
g.fill3DRect(x-20, y-20, 40, 10, true);
g.fill3DRect(x-20, y+10, 40, 10, true);
g.fill3DRect(x-10, y-10, 20, 20, true);
g.drawOval(x-5, y-5, 10, 10);
g.drawLine(x, y, x-20, y);
//坦克向下
case 2:
g.fill3DRect(x-20, y-20, 10, 40, true);
g.fill3DRect(x+10, y-20, 10, 40, true);
g.fill3DRect(x-10, y-10, 20, 20, true);
g.drawOval(x-5, y-5, 10, 10);
g.drawLine(x, y, x, y+20);
//坦克向左
case 3:
g.fill3DRect(x-20, y-20, 40, 10, true);
g.fill3DRect(x-20, y+10, 40, 10, true);
g.fill3DRect(x-10, y-10, 20, 20, true);
g.drawOval(x-5, y-5, 10, 10);
g.drawLine(x, y, x-20, y);
}
}
@Override
//键盘输入并输出
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}
@Override
//键盘按下
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_W)
{
this.mt.setDirection(0);
this.mt.goUp();
}
else if(e.getKeyCode()==KeyEvent.VK_D)
{
this.mt.setDirection(1);
this.mt.goRight();
}
else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.mt.setDirection(2);
this.mt.goDown();
}
else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.mt.setDirection(3);
this.mt.goLeft();
}

}
@Override
//键盘释放
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}
}package com.Tank;
//抽象坦克的共性
public class Tank {
//坦克的坐标
int x=0;
int y=0;
//坦克颜色0代表黄色 1代表红色
int color=0;
//坦克速度
int speed=1;
//坦克方向0代表向上 1代表向右 2代表向下3代表向左
int direction=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;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}


}package com.Tank;
//我的坦克
public class MyTank extends Tank{

//构造函数
public MyTank(int x,int y)
{
super(x, y);
}
//坦克向上运动
public void goUp()
{
y-=speed;
}
//坦克向右运动
public void goRight()
{
x+=speed;
}
//坦克向下运动
public void goDown()
{
y+=speed;
}
//坦克向左运动
public void goLeft()
{
x-=speed;
}}
这是按W,D,S三个键的状态
这是按A键的状态哪的问题啊??????
java坦克

解决方案 »

  1.   

    switch语句里面的case后没没有加break switch(direction)
            {
                //坦克向上
            case 0:
                g.fill3DRect(x-20, y-20, 10, 40, true);
                g.fill3DRect(x+10, y-20, 10, 40, true);
                g.fill3DRect(x-10, y-10, 20, 20, true);
                g.drawOval(x-5, y-5, 10, 10);
                g.drawLine(x, y, x, y-20);
             break;
                //坦克向右
            case 1:
                g.fill3DRect(x-20, y-20, 40, 10, true);
                g.fill3DRect(x-20, y+10, 40, 10, true);
                g.fill3DRect(x-10, y-10, 20, 20, true);
                g.drawOval(x-5, y-5, 10, 10);
                g.drawLine(x, y, x-20, y);
    break;
                //坦克向下
            case 2:
                g.fill3DRect(x-20, y-20, 10, 40, true);
                g.fill3DRect(x+10, y-20, 10, 40, true);
                g.fill3DRect(x-10, y-10, 20, 20, true);
                g.drawOval(x-5, y-5, 10, 10);
                g.drawLine(x, y, x, y+20);
    break;
                //坦克向左
            case 3:
                g.fill3DRect(x-20, y-20, 40, 10, true);
                g.fill3DRect(x-20, y+10, 40, 10, true);
                g.fill3DRect(x-10, y-10, 20, 20, true);
                g.drawOval(x-5, y-5, 10, 10);
                g.drawLine(x, y, x-20, y);
            }