gpt4 book ai didi

Java2D 在移动图像后删除旧像素?

转载 作者:行者123 更新时间:2023-11-30 04:40:54 26 4
gpt4 key购买 nike

所以我在 Java2D 中移动图像,它也会反弹。出于某种原因,它总是留下一些旧图像的痕迹。我该如何解决这个问题?

主类:

package org.main.graphics;

import java.awt.Graphics;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import org.main.entity.Entity;
import org.main.entity.Loael;

@SuppressWarnings("serial")
public class GameWindow extends JFrame implements Runnable {

private List<Entity> entities = new ArrayList<Entity>();
private Thread animator;

public GameWindow() throws IOException {
super("Game");
setSize(640, 480);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
revalidate();

entities.add(new Loael(500, 400));

animator = new Thread(this);
animator.start();
}

public void paint(Graphics g) {
for (Entity entity : entities) {
try {
g.drawImage(entity.getImage(), entity.getX(), entity.getY(),
this);
} catch (IOException e) {
e.printStackTrace();
}
}
}

@Override
public void run() {
while (true) {
for (Entity entity : entities) {
entity.animate(getBounds());
repaint();
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
}
}
}

错误示例:

enter image description here

最佳答案

不要直接在 JFrame 上绘画。相反,请使用 JPanelJComponent 的扩展。对于绘画,重写 paintComponent() 而不是 paint()。不要忘记调用 super.paintComponent(g);,否则您将遇到与示例中相同的行为 - 之前的 drawImage() 结果未清除踪迹依然存在。

看看Performing Custom Painting教程,和 Closer Look at the Paint Mechanism特别是部分,了解更多详细信息。

考虑以下使用 Swing 计时器在 JPanel 中对图像进行动画处理的示例:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class AnimateDemo {
private static void createAndShowUI() {
try {
Image image = ImageIO.read(new URL(
"http://duke.kenai.com/iconSized/duke.gif"));
final MyPanel panel = new MyPanel(image);
JFrame frame = new JFrame("AnimateDemo");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

ActionListener timerAction = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
panel.animate();
}
};
Timer timer = new Timer(10, timerAction);
timer.setRepeats(true);
timer.start();

} catch (IOException e) {
e.printStackTrace();
}
}

static class MyPanel extends JPanel {
private Image image;
private int coordinateX = 0;
private int coordinateY = 0;

private boolean incrementX = true;
private boolean incrementY = true;

public MyPanel(Image image) {
this.image = image;
}

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

if (image != null) {
g.drawImage(image, coordinateX, coordinateY, this);
}
}

@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}

public void animate() {
if (image != null) {

if (image.getWidth(this) + coordinateX > getWidth()) {
incrementX = false;
}
if (coordinateX < 0) {
incrementX = true;
}

if (incrementX)
coordinateX++;
else
coordinateX--;

if (image.getHeight(null) + coordinateY > getHeight()) {
incrementY = false;
}
if (coordinateY < 0) {
incrementY = true;
}

if (incrementY)
coordinateY++;
else
coordinateY--;

repaint();
}
}
}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}

关于Java2D 在移动图像后删除旧像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12332377/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com