Question

private void renderLevelBackground(Graphics2D g2) {
    Image backgroundImage = model.getBackgroundImage();

    g2.drawImage(backgroundImage, 0, 0, null);
}

This is my code, I know that the backgroundImage is loaded properly because if I getWidth/Height it gives me the correct values. Is there anyway to test if it's the image or if it's the method somehow? It just won't draw on my screen despite the fact that I've used the drawImage method many times in this project already, all of which work flawlessly.

Thanks

Was it helpful?

Solution

"Is there ever a reason drawImage won't actually draw an image?"

Possibility, there is no image due to a bad path provided. Use ImageIO.read() which will cause an exception if the image file is not found. For example

public class ImagePanel extends JPanel {
    private BufferedImage image;

    public ImagePanel() {
        try {
            image = ImageIO.read(getClass().getResource("/resources/image.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

If the image is not found, it will throw an IO exception.


Possibility, the panel to which you are drawing, has no preferred size (0 x 0), and you are adding it a container wit layout that respects preferred sizes, so the ImagePanel will not be able to show the image. For example

public class ImagePanel extends JPanel {
    protected void paintComponent(...) {
        ...
}

JPanel panel = new JPanel(); // default FlowLayout that respects preferred sizes
panel.add(new ImagePanel());

To fix this, you can override the gerPreferredSize() of the ImagePanel

public class ImagePanel extends JPanel {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }
}

Possibility, you are not calling your method within the graphics context of the paint[Component] method.

protected void paintComponent(Graphics 2d) {
    super.paintComponent(g);
    Grapchics2D g2 = (Graphics2D)g;
    renderLevelBackground(g2);
}

Other than that, these are all just guesses, and you should provide some more code code (preferably an MCVE) to help us better help you with the problem.


Possible Alternative to your approach. It seems like (from the little code snippet and it method signature semantics) you want to change the background image, when a level have changed. Consider using setBackgroundImage(BufferedImage image) method in your panel class, when the image is drawn. You can then set the background, whenever need be. Something like

public class BackgroundPanel extends JPanel {
    private BufferedImage backgroundImage;

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

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

So whenever you call setBackgroundImage, the image will change, and be repainted.

OTHER TIPS

You may need to explicitly tell the JComponent that you are painting onto that it needs to be redrawn with revalidate() and repaint()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top