为什么下面代码绘制出来的坦克位置不对,而且设置窗口颜色为黑色以后,生成的窗口有一部分是白色,当按下键盘表示方向的wasd任意一个键,坦克位置会发生跳变


import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class TankGame_2 extends JFrame {
    MyPanel mp;

    public static void main(String[] args) {
        new TankGame_2(100, 100, 5, 1);//TankGame_2类中的参数传递:TankGame_2→MyPanel
    }

    public TankGame_2(int x, int y, int direction, int type) {
        mp=new MyPanel(x,y,direction,type);
        add(mp);
        addKeyListener(mp);
        setSize(1000, 800);
        setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class MyPanel extends JPanel implements KeyListener {
private int speed=10;
    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {//针对键盘按下的键判断是将坦克设置为哪个朝向
        if (e.getKeyCode() == KeyEvent.VK_W) {
            direction = 8;
            y -= speed;
            this.repaint();
        }
        else if(e.getKeyCode()==KeyEvent.VK_S){
            direction=5;
            y+=speed;
            this.repaint();
        }
        else if(e.getKeyCode()==KeyEvent.VK_A) {
            direction = 4;
            x -= speed;
            this.repaint();
        }
            else if(e.getKeyCode()==KeyEvent.VK_D){
                direction=6;
                x+=speed;
            this.repaint();
            }
        }



    @Override
    public void keyReleased(KeyEvent e) {

    }

    private int x, y, direction, type;
public MyPanel(){

}
    public MyPanel(int x, int y, int direction, int type) {
        this.x = x;
        this.y = y;
        this.direction = direction;
        this.type = type;
    }

    @Override
    public int getX() {
        return x;
    }

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

    @Override
    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    Tank tank = new Tank();//这里创建Tank对象只是为了使用画坦克的方法

    @Override
    public void paint(Graphics g) {//重写paint方法,实现画坦克的目的,将画坦克封装到一个方法中去,这里paint方法直接调用
        super.paint(g);
        g.fillRect(0, 0, 1000, 800);
        tank.PaintTank(x, y, direction, type,g);
    }
}

class Tank {//一个坦克类,是我方坦克还是地方坦克通过type这个参数来体现

    /**
     * @param x         横坐标
     * @param y         纵坐标
     * @param direction 坦克朝向: 8上 5下 4左 6右
     * @param type      我方还是敌方坦克
     * @param g         画笔
     */
    public void PaintTank(int x, int y, int direction, int type, Graphics g) {//专门画坦克的一个方法
        switch (type) {
            case 1:
                g.setColor(Color.cyan);//我方坦克,颜色为青色
                break;
            case 0:
                g.setColor(Color.orange);//地方坦克,为橙色
                break;
            default:
                System.out.println("暂无处理");
        }
        switch (direction) {//四种不同方向的坦克绘制
            case 8://超前
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x + 20, y);
                break;
            case 5://朝后
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x + 20, y + 60);
                break;
            case 4://朝左
                g.fill3DRect(x-10, y+10, 60, 10, false);
                g.fill3DRect(x-10, y + 40, 60, 10, false);
                g.fill3DRect(x , y + 20, 40, 20, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x-10, y + 30);
                break;
            case 6://朝右
                g.fill3DRect(x-10, y+10, 60, 10, false);
                g.fill3DRect(x-10, y + 40, 60, 10, false);
                g.fill3DRect(x , y + 20, 40, 20, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x + 50, y + 30);

        }

    }
}

img

img