1

Without using super.paintComponent(g); can i still clear my Jpanel or Jframe Screen? I have some shapes drawn on JPanel and i want to clear all the drawing when user presses the right click without Using this method. or i say is there any alternate of super.paintCompenent(g);method or method like clrscr(); In Java.

EDIT

  public void mousePressed(MouseEvent e) {
            super.paintComponents(null); //i want to use this method here?? how can i? 
            if(e.isPopupTrigger())
            {
                s=e.getX();
                as=e.getY();
              try {
                    Thread.sleep(10L);
                } catch (InterruptedException ex) {
                    Logger.getLogger(animate.class.getName()).log(Level.SEVERE, null, ex);
                }
        p.repaint();
            }
        }

i am painting the shape like this

public class mypanel extends JPanel {

   @Override
    public void paintComponent(Graphics g)
    {

       super.paintComponent(g);
        Graphics2D g2=(Graphics2D)g ;

        Color[] c = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, 
                 Color.MAGENTA, Color.WHITE, Color.ORANGE, Color.PINK};
    for(int i=0; i<8; ++i){
        g2.setColor(c[i]);
        int start_angle=i*45;
    g2.fillArc(mx-100, my-100, 200, 200, start_angle,45);



    }
3
  • you can call repaint and stop calling Graphics primitives. If you have components, then simply call removeAll() Apr 8, 2012 at 16:49
  • @Jeffrey code for what?? its too lengthy. Apr 8, 2012 at 16:49
  • 1
    @james post an SSCCE then: sscce.org Apr 8, 2012 at 16:51

2 Answers 2

6

mre solution is actually a good idea, but you may have issues if another repaint comes in (because you move the JFrame, because you resize it, because another window comes on top of it and then leavs, etc...)

Alternatively, you can have something like this to make the change permanent:

public class mypanel extends JPanel {

    private boolean draw = true;

    @Override
    public void paintComponent(Graphics g)
    {

        super.paintComponent(g);
        if (draw) {
            Graphics2D g2=(Graphics2D)g ;

            Color[] c = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, 
                     Color.MAGENTA, Color.WHITE, Color.ORANGE, Color.PINK};
            for(int i=0; i<8; ++i){
                g2.setColor(c[i]);
            int start_angle=i*45;
            g2.fillArc(mx-100, my-100, 200, 200, start_angle,45);
        }
    }

    public void setDraw(boolean draw) {
        this.draw = draw;
        repaint();
    }
}

And then you can just draw or not in your JPanel simlpy by calling mypanel.setDraw(boolean)

2
  • Did you missed the limits {} after if (draw) by mistake :-), That's exactly what i thought of +1 for this :-) Nice Answer :-)
    – nIcE cOw
    Apr 8, 2012 at 17:18
  • @nIcEcOw yup, I sure did. I just fixed it. Apr 8, 2012 at 17:20
4

Perhaps Graphics#clearRect is what you're looking for?

1
  • 1
    +1, I will go with this answer. The OP and simply put a boolean variable, somewhere, make it true at mousePressed(...), and do what you told in your answer, by checking it inside paintComponent(...) method, else do the normal drawing as usual :-)
    – nIcE cOw
    Apr 8, 2012 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.