gpt4 book ai didi

Java-如何从 JPanel 中清除图形

转载 作者:搜寻专家 更新时间:2023-11-01 01:33:26 24 4
gpt4 key购买 nike

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

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)

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

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

Javadoc 解释 the Paint Mechanism :

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:

  • public void paint(Graphics g)

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

  • protected void paintComponent(Graphics g)
  • protected void paintBorder(Graphics g)
  • protected void paintChildren(Graphics g)

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) 的原因。

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

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

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

关于Java-如何从 JPanel 中清除图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30005331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com