简体   繁体   中英

How to clear Graphics/JPanel before new draw

There are a lot of similar questions but I didn't find a solution in them so here is my problem:

In my JPanel I have a visual proof which I update to develop my proof, everything is good here. But I have a button which allows me to restart my proof. Instead of having my JPanel with the first line of my proof I have effectively my first line but I also see my old proof alternately (it's blinking). I dont't understand why.

My JPanel contains several components, not only my JPanel so I don't think that use removeAll() is a good idea. As you will see, I tried revalidate() and repaint() method but it doesn't work.

I would like to pass from在此处输入图像描述 to在此处输入图像描述 but actually I see both. Why?

Here is some extracts of my code:

Here is the action when I push the button "debut". Only the end is useful for you, I use revalidate and repaint methods but nothing works. Pann is my class which extends JPanel.

//Come back to the first step of the proof
debut.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {
        //I delete my old proof on my objects

        pann.revalidate();
        pann.repaint();
    }         
}); 

And here is my paint method for my JPanel. I show you it because I use the super constructor and I think it's important.

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setFont(new Font("serif", Font.PLAIN, 20)); 

    if (arbre != null) 
        dessiner(arbre, g, new Dimension(0,0), 0, 'c', 0, 0);
}

Dessiner is my method which draw my proof, I think it's not necessary to show you this function.

Maybe you can tell me why it doesn't work? Why I see both proof in the same time? And maybe you know what I have to do? Thank you for your help and tell me if you need more code or other.

Finally I used getContentPane().removeAll(); and then I added all my components:

getContentPane().removeAll();
//Modifications on pann
getContentPane().add(pann);
getContentPane().add(regles);
getContentPane().add(boutons);
getContentPane().add(pformule);
getContentPane().add(sequent);

I'm open to any other solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM