Custom JPanel causing rendering problems in second custom JPanel

120 views Asked by At

Bit of an odd one here. I have two classes extending from JPanel, overriding paintComponent in both. One implements Runnable (for animation purposes).

However, when used together with the Runnable one on top, I get the wonderful "paint a copy of everything the mouse points at" in the background of the Runnable instance. See screenshots below:

using_a_jpanel

using a custom component

The only difference between the two is me using JPanel in the former and a custom JPanel with a background image in the latter. Code for the second JPanel below:

package view.widgets;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class PaintedJPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private BufferedImage backgroundImage = null;

    public PaintedJPanel() {
        super();
    }

    public PaintedJPanel(File image) {
        super();
        try {
            backgroundImage = ImageIO.read(image);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        if(null != backgroundImage) {
            g2d.drawImage(backgroundImage, 0, 0, null);
        }
    }

    public BufferedImage getBackgroundImage() {
        return backgroundImage;
    }

    public void setBackgroundImage(BufferedImage backgroundImage) {
        this.backgroundImage = backgroundImage;
    }

}

EDIT: editing in details because the Enter key shouldn't submit the question when I'm adding tags.

FINISHED EDITING @ 13:38.

1

There are 1 answers

3
Hovercraft Full Of Eels On BEST ANSWER

Ah, your paintComponent method is missing the super's call. Change

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    if(null != backgroundImage) {
        g2d.drawImage(backgroundImage, 0, 0, null);
    }
}

to

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    if(null != backgroundImage) {
        g2d.drawImage(backgroundImage, 0, 0, null);
    }
}

As noted in my comments to your question (before seeing the code), without calling super, you're breaking the painting chain, possibly resulting in side effects with child component rendering.