Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom JPanel causing rendering problems in second custom JPanel

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.

like image 721
Gorbles Avatar asked Aug 28 '26 09:08

Gorbles


1 Answers

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.

like image 192
Hovercraft Full Of Eels Avatar answered Aug 30 '26 00:08

Hovercraft Full Of Eels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!