如何在Java中删除或取消图像?

问题描述 投票:-1回答:1

我试图在JPanel中为背景设置动画,但在几个周期后,背景开始出现这种“拖影”效果。我认为这是因为Java不断地覆盖以前的图像,但我似乎无法找到一种“取消绘制”图像的方法。

Background类的一部分

public void setPosition(double x, double y) {

    this.x = (x * moveScale) % GamePanel.WIDTH; 
    this.y = (y * moveScale) % GamePanel.HEIGHT;

}

public void setVector(double dx, double dy) {

    this.dx = dx;
    this.dy = dy;

}

public void update() {

    x += dx % GamePanel.WIDTH;
    y += dy % GamePanel.HEIGHT;  

}

public void draw(Graphics2D g) {

    g.drawImage(image, (int) x, (int) y, null);


    if (x < 0) {

        g.drawImage(image, (int) x + GamePanel.WIDTH, (int) y, null);

    }

    if (x > 0) {

        g.drawImage(image, (int) x - GamePanel.WIDTH, (int) y, null);

    }

}

调用它的Menu类的一部分

public void update() {

    bg.update();

}

public void draw (Graphics2D g) {

    bg.draw(g);

    g.setColor(titleColor);
    g.setFont(titleFont);
    g.drawString("Save Squishy!", 80, 70);

    g.setFont(font);
    for (int i = 0; i < options.length; i++) {

        if (i == currentChoice) {

            g.setColor(Color.BLACK);

        } else {

            g.setColor(Color.RED);

        }

        g.drawString(options[i], 145, 140 + (i * 15));

    }

}

适用于第一个周期:

Works fine for the first cycle

然后开始涂抹

Then Starts to smear

java eclipse swing jpanel graphics2d
1个回答
1
投票

在您的示例中,您应该使用从paintComponent(g)等容器类重写的JPanel方法,而不是创建自己的绘制/绘制方法或使用从图像获取的图形对象。像这样,你可以避免重新绘制的麻烦,只需在扩展JPanel的类上调用repaint()

如评论中所述,建议使用paintComponent(g)方法。从这样的事情开始:

public class TestImage extends JPanel {
    private BufferedImage bimg = null;

    public TestImage() {
        // initiate the class ...
        try {
            bimg = ImageIO.read(new File("C:/Path/To/Image/image.jpg"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bimg, 0, 0, null);
        // do other painting stuff with g
    }

    public static void main(String[] args) {
        JFrame fr = new JFrame("Image test demo");
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setSize(600, 400);
        fr.setLocationRelativeTo(null);
        fr.add(new TestImage());
        fr.setVisible(true);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.