/*
 * 绘画 坦克
 * 
 */
package com.tank;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class tank extends JFrame{ MyPanel mp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
tank tank1=new tank();
}
//创建构造函数
public tank()
{
mp=new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(300, 300);
this.setSize(300,300);
this.setTitle("启鑫制作");
}
//定义 MyPanel
class MyPanel extends Panel implements KeyListener
{
int m=10;
int n=10;
MYtank myt=null;
public MyPanel()
{
myt=new MYtank(m,n);

}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 300, 300);

this.drawtank(myt.getX(),myt.getY(), g, 0, 1);
}


public void drawtank(int x,int y,Graphics g,int direct,int type)
{
switch(type)
{
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.blue);
break;
}
switch(direct)
{
case 0:
g.setColor(Color.CYAN);
g.fill3DRect(x, y, 5, 30,false);
g.fill3DRect(x+15,y, 5, 30,false);
g.fill3DRect(x+5, y+5, 10, 20,false);
g.fillOval(x+5, y+10, 9, 9);
g.drawLine(x+9, y+15, x+9, y-5);
break;


}

}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_W)
{

this.myt.setDirect(0);
this.myt.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.myt.setDirect(1);
this.myt.moveDowm();
}else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.myt.setDirect(2);
this.myt.moveLeft();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
this.myt.setDirect(3);
this.myt.moveRight();
}
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
{
int direct=0;
int speed=1;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
//定义坦克的横坐标
int x=0;
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 y=0;
public Tank(int x,int y)
{
this.x=x;
this.y=y;
}

}

class MYtank extends Tank
{
public MYtank(int x,int y)
{
super(x,y);
}
public void moveUp()
{
y-=speed;
}
public void moveDowm()
{
y+=speed;
}
public void moveLeft()
{
x-=speed;
}
public void moveRight()
{
x+=speed;
}
  }
    }
如题