0

I have read some posts here about changing image background for JdesktopPane, but it doesn't seem to work:

I'm using netbeans so that's in the constructor:

  ImageIcon icon = new ImageIcon("/gui/orange.png");
            img = icon.getImage();   //this is a field Image img;

and in the init method (that also is in the constructor):

theDesktop = new javax.swing.JDesktopPane()
{
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
    }

I am new to this so I can't really understand what I did wrong. and I wonder when it works - as every computer may use different resolution - what would be the solution for the background to match every computer? (when the user wants the window to be the maximum size for example)

Thank you in advace!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You need to do some debugging, and first step I suggest is that you *divide and conquer*. I suggest dividing the steps you're taking to display an image to find out which one is causing your problem. I'd start with reading the image from disk and displaying the ImageIcon in a JOptionPane. Do that first before moving on, as more than likely that is where the bug exists. – Hovercraft Full Of Eels Jul 15 '14 at 00:10
  • Note that myself, I usually read an image in using `ImageIO.read(...)`, and I usually get it as a resource, not as a File. Also, if you're still stuck at the end of these debugging attempts, then you will want to consider creating and posting a [minimal example program](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Jul 15 '14 at 00:11
  • 1
    Consider having a look at [this](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) which demonstrates how to scale an image while maintaining its aspect ratio and [this](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) which discusses scaling algorithms – MadProgrammer Jul 15 '14 at 00:41
  • What posts? What didn't work? – trashgod Jul 15 '14 at 02:17
  • Thank you for your help! the problem was that I forgot this line:getClass().getResource(...) for reading the picture from file. – user3813409 Jul 15 '14 at 09:03

0 Answers0