如题,怎么解决?尽量不要粘贴代码,说明原因即可。其他错误不用管,谢谢
package Tank;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//主程序
public class Game extends JFrame{
public static void main(String[] args) {
Game game=new Game();
}
Game(){
MyPanel mp=new MyPanel();
this.add(mp);
mp.setBackground(Color.BLACK);
this.setBounds(100,100,400,300);
this.setVisible(true);
this.addKeyListener(mp);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
Game.this.setVisible(false);
System.exit(0);
}
});
}
}
//面板区域
class MyPanel extends JPanel implements KeyListener{
Hero hero=null;
MyPanel(){
hero=new Hero(150,200);
}
public void paint(Graphics g){
super.paint(g);
this.drawTank(hero.getX(), hero.getY(), g, 0, 0);
}
public void drawTank(int x,int y,Graphics g,int direct,int type){
switch(type){
case 0:
g.setColor(Color.YELLOW);
break;
case 1:
g.setColor(Color.CYAN);
break;
}
switch(direct){
case 0:
g.fillRect(x, y, 5, 30);
g.fill3DRect(x+15, y, 5, 30,false);
g.fill3DRect(x+5,y+5, 10, 20,false);
g.fillOval(x+5, y+10, 10, 10);
g.drawLine(x+10, y+17, x+10, y);
break;
case 1:
/*g.fillRect(x, y, 5, 30);
g.fill3DRect(x+15, y, 5, 30,false);
g.fill3DRect(x+5,y+5, 10, 20,false);
g.fillOval(x+5, y+10, 10, 10);
g.drawLine(x+10, y+17, x+10, y);
break;
case 2:
g.fillRect(x, y, 5, 30);
g.fill3DRect(x+15, y, 5, 30,false);
g.fill3DRect(x+5,y+5, 10, 20,false);
g.fillOval(x+5, y+10, 10, 10);
g.drawLine(x+10, y+17, x+10, y);
break;
case 3:
g.fillRect(x, y, 5, 30);
g.fill3DRect(x+15, y, 5, 30,false);
g.fill3DRect(x+5,y+5, 10, 20,false);
g.fillOval(x+5, y+10, 10, 10);
g.drawLine(x+10, y+17, x+10, y);
break;*/
}
}
//键盘处理
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_W);{
hero.setDirect(0);
hero.moveUp();
System.out.println("按下 "+e.getKeyChar());
System.out.println("x -"+hero.x+"y-"+hero.y);
}
if(e.getKeyCode()==KeyEvent.VK_D){
hero.setDirect(1);
hero.moveRight();
System.out.println("按下 "+e.getKeyChar());
System.out.println("x -"+hero.x+"y-"+hero.y);
}
if(e.getKeyCode()==KeyEvent.VK_S){
hero.setDirect(2);
hero.moveDown();;
System.out.println("按下 "+e.getKeyChar());
System.out.println("x -"+hero.x+"y-"+hero.y);
}
if(e.getKeyCode()==KeyEvent.VK_A){
hero.setDirect(3);
hero.moveLeft();;
System.out.println("按下 "+e.getKeyChar());
System.out.println("x -"+hero.x+"y-"+hero.y);
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {}
}
//坦克
class Tank{
int direct;
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;
int y=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;
}
Tank(int x,int y){
this.x=x;
this.y=y;
}
public void moveUp(){
y-=speed;
}
public void moveRight(){
x+=speed;
}
public void moveDown(){
y+=speed;
}
public void moveLeft(){
x-=speed;
}
}
//我的坦克
class Hero extends Tank{
Hero(int x,int y){
super(x,y);
}
}
public void keyPressed(KeyEvent e) {
hero.setDirect(0);
hero.moveUp();
System.out.println("按下 "+e.getKeyChar());
System.out.println("x -"+hero.x+"y-"+hero.y);
}
hero.moveDown();;方法呢
程序太长了,你自己debug一下,在按键处理的地方设断点,然后跟踪程序看看问题究竟出在哪里