Java-如何从JPanel清除图形 [英] Java- how to clear graphics from a JPanel

查看:822
本文介绍了Java-如何从JPanel清除图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个简单的程序,在其中绘制一个黑色椭圆形,然后用鼠标单击.但是,我希望出现一个新的椭圆形,而一个旧的椭圆形消失.我将如何去做呢?我弄乱了在mousePressed方法中插入的removeAll()方法,但是它对我不起作用. removeAll()方法是否甚至适用于此?还是我应该使用其他东西?很抱歉,答案很明显,但是我对此还不陌生,正在尝试学习.任何建议将不胜感激.谢谢.

I'm creating a simple program where I draw a black oval where I click with my mouse. However I want a new oval to appear and the old one to disappear. How would I go about doing this? I've messed around with the removeAll() method inserted into my mousePressed method, however it isn't working for me. Is the removeAll() method even suitable for this? Or should I use something else? Sorry if the answer is obvious, but I am still new to this and trying to learn. Any advice would be immensely appreciated. Thanks.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PaintPractice extends JPanel implements MouseListener {

    Random rand = new Random(); 
    int x = rand.nextInt(450);
    int y = rand.nextInt(450);

    public PaintPractice(){
        super();
        addMouseListener(this);
    }

    public static void main(String[] args){

        JFrame frame = new JFrame();
        PaintPractice panel = new PaintPractice();

        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(panel);        
    }

    public void paint(Graphics g){
        g.setColor(Color.BLACK);
        g.fillOval(x, y, 50, 50);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        removeAll();
        repaint();      
    }

    @Override
    public void mouseClicked(MouseEvent e) {        
    }

    @Override
    public void mouseEntered(MouseEvent e) {        
    }

    @Override
    public void mouseExited(MouseEvent e) {     
    }

    @Override
    public void mouseReleased(MouseEvent e) {       
    }
}

推荐答案

立即解决该问题,只需在paint(Graphics g)方法中调用super.paint(g).

Immediate solution it to Just call super.paint(g) in the paint(Graphics g) method.

public void paint(Graphics g){
        super.paint(g);
        g.setColor(Color.BLACK);
        g.fillOval(x, y, 50, 50);
    }

绘画机制以及为什么我应该覆盖paintComponent()而不是覆盖paint():

Javadoc解释了绘制机制:

The Paint Mechanism and why Should i override paintComponent() instead of overriding paint():

Javadoc explains the Paint Mechanism:

现在,您知道paintComponent方法是您所有 绘画代码应放置.的确,这种方法将 该在绘画时调用,但绘画实际上从更高处开始 使用paint方法(由定义 java.awt.Component.)此方法将由绘画执行 子系统,无论何时需要渲染组件.它的签名 是:

By now you know that the paintComponent method is where all of your painting code should be placed. It is true that this method will be invoked when it is time to paint, but painting actually begins higher up the class heirarchy, with the paint method (defined by java.awt.Component.) This method will be executed by the painting subsystem whenever you component needs to be rendered. Its signature is:

  • 无效涂料(图形g)

javax.swing.JComponent扩展了此类,并进一步对 将paint方法分为三个独立的方法,分别在 以下顺序:

javax.swing.JComponent extends this class and further factors the paint method into three separate methods, which are invoked in the following order:

  • 受保护的空paintComponent(图形g)
  • 受保护的空paintBorder(图形g)
  • 保护无效的paintChildren(图形g)
  • protected void paintComponent(Graphics g)
  • protected void paintBorder(Graphics g)
  • protected void paintChildren(Graphics g)

API并没有采取任何措施来防止您的代码覆盖paintBorder 和paintChildren,,但总的来说,没有理由让您 这样做.实际上,paintComponent将是唯一的 您将需要覆盖的方法.

The API does nothing to prevent your code from overriding paintBorder and paintChildren, but generally speaking, there is no reason for you to do so. For all practical purposes paintComponent will be the only method that you will ever need to override.

这就是为什么您的PaintPractice代码应改为调用super.paintComponent(g)的原因.

This is why your PaintPractice code should invoke super.paintComponent(g) instead.

public void paintComponent(Graphics g) {    
    super.paintComponent(g);       
     g.setColor(Color.BLACK);
     g.fillOval(x, y, 50, 50);
}  

此外,您无需在mousePressed(MouseEvent e)方法中调用removeAll().

Also you don't need to call removeAll() in the mousePressed(MouseEvent e) method.

    @Override
    public void mousePressed(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        repaint();     
    }

这篇关于Java-如何从JPanel清除图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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