所以基本上它不会留下痕迹。我试图删除super.paint并且我试图制作多个但是它要么创建错误,要么它没有。我已经完成了至少10次,这就是我去这里的原因。提前谢谢!
import javax.swing.*;
import java.awt.*;
public class Grafik extends JPanel {
private int x = 0;
private void moveBall()
{
x += 1;
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillRect(x, 50, 20, 80);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
Grafik grafik = new Grafik();
frame.setSize(700, 800);
frame.setLocation(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(grafik);
frame.setTitle("Mitt spel");
frame.setResizable(false);
frame.setVisible(true);
while(true)
{
grafik.repaint();
grafik.moveBall();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:3)
问题/解决方案:
super.paintComponent(g)
之后但在绘制移动sprint之前立即在paintComponent方法中绘制。通过调用BufferedImage上的getGraphics()
,通过从中获取Graphics对象,将您的踪迹绘制到此BufferedImage中。