java Repaint() 不清除框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4904807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:33:37  来源:igfitidea点击:

Repaint() doesn't clear the frame

javaswing

提问by Gopal

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. So, how to do it? Please explain your answers.

问题是 repaint() 应该清除框架并在该位置绘制矩形,但之前绘制的矩形仍然存在。那么,该怎么做呢?请解释你的答案。

回答by Hovercraft Full Of Eels

Have you tried calling super.paintComponent(g) in your paintComponent method? This will clear prior images drawn in your JPanel:

您是否尝试过在您的paintComponent 方法中调用super.paintComponent(g)?这将清除在您的 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. 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.

此外,不要在paintComponent 方法中启动计时器或执行任何程序逻辑。首先你不能绝对控制何时或是否调用该方法,其次,该方法必须只关心绘画而不关心其他事情,并且需要尽可能快。

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();
            }
        });
    }
}

回答by jzd

You need to repaint the background each time as well. Add code to paint the background before you paint the rectangle.

您也需要每次都重新绘制背景。在绘制矩形之前添加代码以绘制背景。

回答by Eric Giguere

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/