1

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"));
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
EricR
  • 1,487
  • 2
  • 21
  • 42

3 Answers3

6

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
  • 203,806
  • 29
  • 246
  • 1,045
5

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
  • 321,443
  • 19
  • 166
  • 288
  • 2
    even when covering everything, it might have transparent areas - so not calling super is complying to super's contract only if opaque == false. which it isn't by default and can be changed by client code at any time. So it's not really safe, and letting your IDE type one additional line is ... :-) – kleopatra Dec 05 '11 at 10:47
  • Then why not use something like "if(image!=null) {drawImage;} else {super.paintComponent;}" in paintComponent in case of the image could be linked later ? Is it a good idea ? – Benj Mar 05 '13 at 22:52
  • @Benj, This isn't a problem with a null image. This comment relates to an existing image that may contain transparent pixels so you can get unexpected painting problems. – camickr Mar 06 '13 at 01:52
  • @camickr, OK it was just about the default behavior, because in my context there is a potentially null image set further. Is it mandatory to call `super` when there is no image to draw ? – Benj Mar 06 '13 at 21:28
  • 1
    @Benj, it is safer since you make sure the background it at least painted. After than you can paint over it any way you want. – camickr Mar 06 '13 at 22:49
2

Use a component Listener to know when the windows has been resized and then rescale the image using

image.getScaledInstance(getWidth(), getHeight(), 0);
viraptor
  • 33,322
  • 10
  • 107
  • 191
Diego Ramos
  • 989
  • 4
  • 16
  • 35