5

I have a JFrame with 2 JPanel in it: a PaintPanel (with a paint() method) and a ButtonPanel (with buttons). When I invoke the repaint() of the PaintPanel (but clicking the button) the button of the ButtonPanel is being painted in the PaintPanel! It isn't clickable or anything, it is just there.

I tried to recreate the problem with this code:

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame("frame");
        frame.setSize(400,400);
        frame.setLayout(new GridLayout(2,1));
        PaintPanel paint = new PaintPanel();
        ButtonPanel buttons = new ButtonPanel(paint);
        frame.add(paint);
        frame.add(buttons);
        frame.setVisible(true);
    }
}

public class PaintPanel extends JPanel{
    public void paint(Graphics g){
        g.drawRect(10, 10, 10, 10);
    }
}

public class ButtonPanel extends JPanel implements ActionListener{

    private PaintPanel paintPanel;

    public ButtonPanel(PaintPanel paintPanel){
        this.paintPanel=paintPanel;
        JButton button = new JButton("button");
        button.addActionListener(this);
        add(button);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        paintPanel.repaint();           
    }
}

This sould recreate the problem I have (sorry for the odd code markings, can't seem to get it right).

I really hope one of you knows what is happening here because i don't...

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Lucivius
  • 83
  • 4
  • 1
    Such rendering artifacts often arise from failing to honor the [opacity](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#props) property. Moreover, "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#callbacks). – trashgod Aug 31 '12 at 23:41

1 Answers1

6

First of all, you should override paintComponent() instead of paint(). It's part of the best practices in Swing when it comes to do some panel customization.

Secondly, here is the code that works for me (I don't know why yours doesn't though :S):

public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame("frame");
        frame.setSize(400, 400);
        // frame.setLayout(new GridLayout(2, 1));
        PaintPanel paint = new PaintPanel();
        ButtonPanel buttons = new ButtonPanel(paint);
        // frame.add(paint);
        // frame.add(buttons);
        frame.setVisible(true);

        JPanel pan = new JPanel(new BorderLayout());
        pan.add(paint);
        pan.add(buttons, BorderLayout.SOUTH);
        frame.add(pan);

    }
}

class PaintPanel extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(new Color(new Random().nextInt()));
        g.drawRect(10, 10, 10, 10);
    }
}

class ButtonPanel extends JPanel implements ActionListener {

    private final PaintPanel paintPanel;

    public ButtonPanel(PaintPanel paintPanel) {

        this.paintPanel = paintPanel;
        JButton button = new JButton("button");
        button.addActionListener(this);
        add(button);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        if (getParent() != null) {
            getParent().repaint();
        }
    }
}
aymeric
  • 3,877
  • 2
  • 28
  • 42
  • 1
    Often you want to have super.paintComponent(g); as the first line in your overridden paintComponent method. It will clear everything that you aren't drawing in that particular call to repaint. – mrranstrom Aug 31 '12 at 20:51
  • 2
    +1 for paintComponent & super.paintCompont. @aymeric you have to remember, the Graphics object is reused, so if you don't take the time to make sure you've cleaned it first, you'll end up with unexpected paint artefacts – MadProgrammer Aug 31 '12 at 21:11
  • 2
    +1 for using `super.paintComponent()` to honor the [opacity](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#props) property. – trashgod Aug 31 '12 at 23:43