简体   繁体   English

Repaint()不会清除帧

[英]Repaint() doesn't clear the frame

public class Graphics2DTest extends JPanel implements ActionListener{
private Timer time = new Timer(5,(ActionListener) this);
int x = 0,y = 0;
public void paintComponent(Graphics g){

    Graphics2D gui = (Graphics2D) g;
    Rectangle2D rectangle = new Rectangle2D.Double(x,y,100,150);
    gui.setPaint(Color.GREEN);
    gui.fill(rectangle);
    time.start();
}

public void actionPerformed(ActionEvent arg0) {
    x++;
    y++;
    repaint();
}
}

The problem is repaint() is supposed to clear the frame and draw the rectangle in the position, but the previously painted rectangle remains. 问题是repaint()应该清除框架并在该位置绘制矩形,但先前绘制的矩形仍然存在。 So, how to do it? 那么,怎么做呢? Please explain your answers. 请解释一下你的答案。

Have you tried calling super.paintComponent(g) in your paintComponent method? 您是否尝试在paintComponent方法中调用super.paintComponent(g)? This will clear prior images drawn in your JPanel: 这将清除JPanel中绘制的先前图像:

public void paintComponent(Graphics g){
  super.paintComponent(g);
  Graphics2D gui = (Graphics2D) g;
  Rectangle2D rectangle = new Rectangle2D.Double(x,y,100,150);
  gui.setPaint(Color.GREEN);
  gui.fill(rectangle);
  //time.start();
}

Also, don't start a timer or do any program logic within the paintComponent method. 此外,不要在paintComponent方法中启动计时器或执行任何程序逻辑。 First of all you cannot absolutely control when or if the method will be called, and secondly, this method must be concerned only with painting and nothing else, and needs to be as fast as possible. 首先,你无法绝对控制何时或是否会调用该方法,其次,这种方法必须只涉及绘画而不是其他任何东西,并且需要尽可能快。

For instance: 例如:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

import javax.swing.*;

public class Graphics2DTest extends JPanel implements ActionListener {
    private Timer time = new Timer(5, (ActionListener) this);
    int x = 0, y = 0;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D gui = (Graphics2D) g;
        Rectangle2D rectangle = new Rectangle2D.Double(x, y, 100, 150);
        gui.setPaint(Color.GREEN);
        gui.fill(rectangle);
        //time.start();
    }

    public void actionPerformed(ActionEvent arg0) {
        x++;
        y++;
        repaint();
    }

    public Graphics2DTest() {
        setPreferredSize(new Dimension(700, 500));
        time.start();
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("Graphics2DTest");
        frame.getContentPane().add(new Graphics2DTest());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

You need to repaint the background each time as well. 您每次都需要重新绘制背景。 Add code to paint the background before you paint the rectangle. 在绘制矩形之前添加代码以绘制背景。

You need to clear the background first. 您需要先清除背景。

A resource is this: 资源是这样的:

http://java.sun.com/products/jfc/tsc/articles/painting/ http://java.sun.com/products/jfc/tsc/articles/painting/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM