Java - how to clear graphics from JPanel

I am creating a simple program where I draw a black oval that I click on with the mouse. However, I want the new oval to appear and the old one to disappear. How should I do it? I messed up the removeAll () method inserted in my mousePressed method, however it doesn't work for me. Is the removeAll () method appropriate for this? Or should I be using something else? Sorry if the answer is obvious, but I'm still new to this and trying to learn. Any advice would be much appreciated. Thank you.

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) {       
    }
}

      

+3


source to share


4 answers


Immediate solution for this Just call super.paint(g)

in the method paint(Graphics g)

.

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

      

The Paint mechanism and why I should override paintComponent()

instead of overriding paint()

:

The Javadoc explains the Paint Mechanism :

Now you know that the paintComponent method is where all your painting code should be placed. It's true that this method will be called when it's time to paint, but painting actually starts higher up the class hierarchy, with a paint method (defined by java.awt.Component.) This method will be executed by the painting subsystem whenever a component needs to be visualized. His signature is:

  • public void paint (Graphics g)

javax.swing.JComponent extends this class and drawing method into three separate methods, which are called 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 overlapping paintBorder and paintChildren , but generally speaking there is no reason for you to do this. For all practical purposes, paintComponent will be the only method you will ever need to override .



This is why your PaintPractice

code should call super.paintComponent(g)

instead.

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

      

Also you don't need to call removeAll()

in the method mousePressed(MouseEvent e)

.

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

      

+1


source


  • Since JPanel is a subclass of JComponent, you must override paintComponent

    instead paint

    and also usesuper.paintComponent(g)

    in the paintComponent

    method.

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
    
          

  • When you use removeAll

    , all components (buttons, text boxes, labels, etc.) in the JPanel are removed, if any. However, you are not adding any components to the JPanel, so there is no need to call this method.



+3


source


One possible workaround if you just want to show the newly created oval. Make your frame and panel static, then call frame.setContentPane (panel) on mousePressed.

Another working method is to call g.clearRect (0, 0, getWidth (), getHeight ()) in paint, but this will do all the background whitecolor.

+2


source


just fillOval with the background color of the current drawing surface

 {
        g.setColor(...);//setColor to  surface background 
        g.fillOval(x, y, 50, 50);
    }

      

if you want you can clear the scope: more at OracleDoc

+1


source







All Articles