开发者

Background settings for a JDesktopPane

开发者 https://www.devze.com 2022-12-31 19:32 出处:网络
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开发者_如何学C I resize the JDesktopPane, the background ima

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开发者_如何学C 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"));


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);
}


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.


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

image.getScaledInstance(getWidth(), getHeight(), 0);
0

精彩评论

暂无评论...
验证码 换一张
取 消