views:

144

answers:

2

I'm using a self-made DesktopPaneUI for a JDesktopPane, I've written the proper methods for the class, and I'm running into trouble. When I resize the JDesktopPane, the background image doesn't resize with the frame. The image appears to be clipped at the size it was when the window initially opened. I'm giving it an image larger than the window, and I'm still having this problem.

Here's my method call inside the constructor of my desktopUI class.

super();
this.background = javax.imageio.ImageIO.read(new File(fileName));

Is there a way I can change my main class where I set the UI, or the myDesktopPaneUI class such that the background still fills the window when the JDesktopPane changes size?

setUI(new myDesktopPaneUI("media/bg.jpg"));
+2  A: 

Override paint() to invoke drawImage() in a way that scales the image to the pane's size:

@Override
public void paint(Graphics g, JComponent c) {
    g.drawImage(image, 0, 0, c.getWidth(), c.getHeight(), null);
}
trashgod
+2  A: 

If you are only creating the custom UI to add the background image, the easier approach is to do custom painting of the JDesktopPane so that it works for all LAF's:

JDesktopPane desktop = new JDesktopPane()
{
    protected void paintComponent(Graphics g)
    {
        g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    }

};

Normally you would invoke super.paintComponent(g) first, but since the image will cover the entire background there is no need to do this.

camickr