CTZStef
CTZStef

Reputation: 1715

Weird JPanel behavior when calling repaint()

I have a class PanelFormes that extends JPanel, which I use as a container. I also have two other JPanel, one to paint some figure in it, the second one contains buttons. These two JPanel are included into the first one, using BorderLayout.

One my buttons is supposed to call the repaint method of the JPanel where there should be figures. In my main window constructor, I do that:

boutonGetForme.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        ajoutForme(rect);
    }
});

The method ajoutForme, in my main window class :

// Methode ajout de forme, normalement appelee par le controleur
public void ajoutForme(Forme f) {

    //dire au jpanel quelle forme on veut dessiner et comment
    jpanel.definirForme(f);
    //mettre a jour le jpanel
    jpanel.repaint();  
}

And finally the class PanelFormes :

package gui;

import java.awt.Graphics;
import javax.swing.JPanel;

public class PanelFormes extends JPanel {

private Forme f;
PanelFormes() {
    f = new Rectangle(1,1,1,1);
}
public void definirForme(Forme f) {
    this.f = f;
}
public void paintComponent(Graphics g) {
    //dessiner la forme
    f.dessine(g);
}            
}

I think I gave you all relevants informations. My problem is that, when I try to paint a figure with the button buttonGetForme, I not only get the figure, but also some weird screenshot-copy of the very button I just pressed, that is pasted in the upper left corner of my JPanel, I really wonder how... If I try to paint the figure by calling ajoutForme directly, it works just fine. There must be something with the ActionListener, but I can't understand what... I am a newbee in Java, any help will be very much appreciated. Thanks

Upvotes: 1

Views: 363

Answers (1)

trashgod
trashgod

Reputation: 205875

If you setOpaque(true), verify that you are completely rendering the area defined by the component's bounds; if not, use setOpaque(false). If the problem persists, please edit your question to include an sscce that exhibits the artifact.

Addendum: See also Painting in AWT and Swing: Opacity.

Upvotes: 2

Related Questions