在java中移动后删除图像

ax6ht2ek  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(348)

在太空船周围移动图像后,我很难处理图像的删除
首先,我有我的movingsprite类,使董事会:
公共类movingspriteex扩展了jframe{

private static final long serialVersionUID = 1L;

public MovingSpriteEx() {
    initUI();
}

private void initUI() {
    add(new Board());

    setTitle("Moving sprite");
    setSize(500,400);

    setLocationRelativeTo(null);
    System.out.println("calling");
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

    EventQueue.invokeLater(() -> {
        MovingSpriteEx ex = new MovingSpriteEx();
        ex.setVisible(true);
    });
}

}
我的board类正在使用paintcomponent从一个太空船类中进行绘制,如下所示:
公共类板扩展jpanel实现actionlistener{

private static final long serialVersionUID = 1L;
private Timer timer;
private Spaceship ship;
private final int DELAY = 10;
private boolean incrementX = true;
private boolean incrementY=true;

public Board() {

    initBoard();

}

private void initBoard() {//building the frame
    addKeyListener(new TAdapter());//listens to the KeyAdapter that can read on the keyboards input
    setBackground(Color.black);
    setFocusable(true);

    ship = new Spaceship();
    timer = new Timer(DELAY,this);
    timer.start();

}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

        doDrawing(g);
        Toolkit.getDefaultToolkit().sync();

}

private void doDrawing(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(ship.getImage(),ship.getX(), ship.getY(), this);

}

@Override
public void actionPerformed(ActionEvent arg0) {
    step();
}

private void step() {

    ship.move();

    repaint(ship.getX()-1,ship.getY()-1,ship.getWidth()+2,ship.getHeight()+2);
}

private class TAdapter extends KeyAdapter{
    @Override
    public void keyReleased(KeyEvent e) {
        ship.keyReleased(e);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        ship.keyPressed(e);
    }
}

}
宇宙飞船舱:
公共级宇宙飞船{

private int dx;
private int dy;
private int x = 40;
private int y = 60;
private int w;
private int h;
private Image image;

public Spaceship() {

    loadImage();
}

private void loadImage() {

    ImageIcon ii =  createImageIcon("../Images/spaceship.jpg","something");

    image = ii.getImage(); 

    w = ii.getIconWidth();
    h = ii.getIconHeight();
}

private ImageIcon createImageIcon(String path, String description) {
    java.net.URL imgURL = getClass().getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL, description);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

public void move() {

    x += dx;
    y += dy;
}

public int getX() {

    return x;
}

public int getY() {

    return y;
}

public int getWidth() {

    return w;
}

public int getHeight() {

    return h;
}    

public Image getImage() {

    return image;
}

public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
        dx = -2;
    }

    if (key == KeyEvent.VK_RIGHT) {
        dx = 2;

    }

    if (key == KeyEvent.VK_UP) {
        dy = -2;
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 2;
    }
}

public void keyReleased(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
        dx = 0;
    }

    if (key == KeyEvent.VK_RIGHT) {
        dx = 0;
    }

    if (key == KeyEvent.VK_UP) {
        dy = 0;
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 0;
    }
}

public static void main(String [] args) {

}

}
希望有人能看出我做错了什么
拉斯姆斯

huwehgph

huwehgph1#

你的代码是绘制船舶图像到你的董事会(jpanel)的每一帧,但没有清除的图像,绘制帧之前,jpanel不会为你这样做。保留一份调用drawimage时使用的坐标副本,然后下次清除该区域,对于您的普通背景,您可以使用fillrect。
例如,当您在纯黑色背景上绘制时,您可以向spaceship类中添加如下内容并调用它,而不是在dodrawing中调用drawimage。

private java.awt.Rectangle dirtyArea;
public void draw(java.awt.Graphics2D g2d, java.awt.image.ImageObserver observer) {
    if (dirtyArea != null) {
        g2d.setColor(java.awt.Color.BLACK);
        g2d.fillRect(dirtyArea.x, dirtyArea.y, dirtyArea.width, dirtyArea.height);
    }
    int shipX = getX();
    int shipY = getY();
    Image shipImage = getImage();
    g2d.drawImage(shipImage, shipX, shipY, observer);

    if (dirtyArea == null || dirtyArea.width == -1) {  // -1 check is there just in case the image wasn't loaded when this code ran before
        dirtyArea = new java.awt.Rectangle();
        dirtyArea.setSize(shipImage.getWidth(null), shipImage.getHeight(null)); // don't need observer as we're checking for -1 next time round
    }
    dirtyArea.setLocation(shipX, shipY);
}

相关问题