Repaint()不会清除帧 [英] Repaint() doesn't clear the frame

查看:124
本文介绍了Repaint()不会清除帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

您是否尝试在paintComponent方法中调用super.paintComponent(g)?这将清除JPanel中绘制的先前图像:

Have you tried calling super.paintComponent(g) in your paintComponent method? This will clear prior images drawn in your 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();
}

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

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.

例如:

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

这篇关于Repaint()不会清除帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆