3

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.

Gorbles
  • 1,169
  • 12
  • 27
  • Without code, it will be difficult if not impossible to help, but I agree, don't post the "entire class code" since that will likely contain much code completely unrelated to your problem. Instead, I suggest that you create and post a [minimal example program](http://stackoverflow.com/help/mcve) or [SSCCE](http://sscce.org). – Hovercraft Full Of Eels Jun 14 '15 at 12:33
  • 2
    A few quick questions though -- do your `paintComponent(Graphics g)` override methods call `super.paintComponent(g)` within them? If not, they're breaking the painting chain, possibly resulting in side effects with child component rendering. Do you change the Graphics AffineTransform, Stroke, or other critical properties within your paintComponent? If so, only make these changes on a copy of the Graphics object, not on the one given by the JVM. Do you hold any program logic within a paintComponent method, or change the state of non-graphical elements of the component from within the method? – Hovercraft Full Of Eels Jun 14 '15 at 12:34

1 Answers1

2

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Was about to comment on your initial comment, can't believe I missed that. The other class had it there! Will mark as an answer when the site lets me! :) – Gorbles Jun 14 '15 at 12:41
  • @Gorb: glad this helped. – Hovercraft Full Of Eels Jun 14 '15 at 13:06
  • @Gorb: Note that you didn't get an automatic response to close this question from me, and you did in fact get decent help **because** you posted a minimal example program here. How is this need of ours to fully understand your problems and your question different here when compared to your [current question](http://stackoverflow.com/questions/31165732/jdialog-doesnt-respect-dimensions-of-child-components)? – Hovercraft Full Of Eels Jul 01 '15 at 16:03
  • Because I'm asking for different things and the situation is different. I'm on a train to a hospital and that is more than enough RL justification than I should have to give. If you don't have time, and you don't know, that is fine. Just listen to what I'm asking. I'm asking for knowledge, not a segment of code to paste in. – Gorbles Jul 01 '15 at 16:11