8 Replies - 1453 Views - Last Post: 30 November 2012 - 10:51 PM Rate Topic: -----

#1 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Image.getWidth() and getHeight() return -1 first time after resizing

Posted 30 November 2012 - 09:56 AM

I have the below code in a program to add a background image to one of my panels. I added the System.out.println lines for debugging because when I added the loop to tile the image, I found it ended up in an infinite loop and I had to kill the program from the task manager.

With the code as it is (with the debug lines), it actually works fine, and the output is:
-1 -1
550 220

When I comment out the *first* println, I get this output, and it goes into an infinite loop because of these values:
-1 -1

So it looks like somehow what's happening is that the first call to getWidth() and getHeight() on the newly-scaled image will always return -1, but will change subsequent calls to the correct values.

What on earth is going on here? Why is it exhibiting this behaviour?

		bg = new ImageIcon(OpenScreen.class.getResource("/res/images/HomepageHeader.png")).getImage();
		bg = bg.getScaledInstance(TOP_HEIGHT*bg.getWidth(null)/bg.getHeight(null), TOP_HEIGHT, Image.SCALE_FAST);
		System.out.println(bg.getWidth(null) + "\t" + bg.getHeight(null));
		JPanel topPanel = new JPanel() {
			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
			    Dimension d = getSize();
			    int width = bg.getWidth(null);
			    int height = bg.getHeight(null);
			    System.out.println(width + "\t" + height);

			    for (int x = 0; x < d.width; x += width) {
			    	for (int y = 0; y < d.height; y += height) {
			    		g.drawImage(bg, x, y, null, null);
			    		//System.out.println(x + "\t" + y);
			    	}
			    }
			}
		};


Is This A Good Question/Topic? 0
  • +

Replies To: Image.getWidth() and getHeight() return -1 first time after resizing

#2 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Image.getWidth() and getHeight() return -1 first time after resizing

Posted 30 November 2012 - 10:14 AM

Use ImageIO.read(File/URL) to get the image...

If you still have problem, read this...

Hope this Helps!
Was This Post Helpful? 2
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1400
  • View blog
  • Posts: 3,108
  • Joined: 05-April 11

Re: Image.getWidth() and getHeight() return -1 first time after resizing

Posted 30 November 2012 - 10:26 AM

From the docs

Quote

The new Image object may be loaded asynchronously even if the original source image has already been loaded completely.

Maybe that has something to do with it

If nothing works, then you could load the image as a BufferedImage, grab the Graphics object from the BufferedImage, and use the scale method on the Graphics object
Was This Post Helpful? 1
  • +
  • -

#4 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Re: Image.getWidth() and getHeight() return -1 first time after resizing

Posted 30 November 2012 - 11:45 AM

View PostAKMafia001, on 30 November 2012 - 01:14 PM, said:

Use ImageIO.read(File/URL) to get the image...

If you still have problem, read this...

Hope this Helps!

Thank you both very much, this was exactly the problem, and it also helped me to finally solve another extremely frustrating problem I had been having.
Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 848
  • View blog
  • Posts: 2,585
  • Joined: 29-July 11

Re: Image.getWidth() and getHeight() return -1 first time after resizing

Posted 30 November 2012 - 01:28 PM

I'd like to know why this happens. I always get a -1 on width and height when I load images with Toolkit's getImage(). I have no clue why. The image obviously loads. It would be awesome if someone who knew could share with us. :)/>/>/>

public class Main {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame() {
                    Image image;

                    {
                        image = Toolkit.getDefaultToolkit().getImage("images/babymia.jpg");
                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        System.out.println(image.getWidth(null));
                        System.out.println(image.getWidth(null));
                        setSize(image.getWidth(null), image.getHeight(null));
                        setVisible(true);
                    }

                    public void paint(Graphics g) {
                        g.drawImage(image, 0, 0, null);
                    }
                };
            }
        });
    }
}


This post has been edited by farrell2k: 30 November 2012 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1400
  • View blog
  • Posts: 3,108
  • Joined: 05-April 11

Re: Image.getWidth() and getHeight() return -1 first time after resizing

Posted 30 November 2012 - 02:00 PM

farrel try and mess around with the prepareImage method, and make sure to add an ImageObserver since it is async
The image may not have been loaded yet
Toolkit.getDefaultToolkit().prepareImage(image, -1, -1, myObserver);



Check if the image is loaded by using the checkImage method and pass in -1
Was This Post Helpful? 0
  • +
  • -

#7 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 848
  • View blog
  • Posts: 2,585
  • Joined: 29-July 11

Re: Image.getWidth() and getHeight() return -1 first time after resizing

Posted 30 November 2012 - 02:11 PM

[quote name='CasiOo' date='30 November 2012 - 09:00 PM' timestamp='1354309205' post='1757998']
The image may not have been loaded yet
Toolkit.getDefaultToolkit().prepareImage(image, -1, -1, myObserver);



It gives a -1 for size because it has not finished loading. The questions is what is taking so long, and is it loaded on the Toolkit Thread?
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2733
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: Image.getWidth() and getHeight() return -1 first time after resizing

Posted 30 November 2012 - 03:00 PM

Quote

It gives a -1 for size because it has not finished loading. The questions is what is taking so long
What do you mean by 'so long'? As Casio has said, it's loaded in another thread, and as he's also said, you can use an ImageObserver, although it's simpler to use a MediaTracker which forces synchronicity (if you can't avoid asynchronicity or don't want to)
Was This Post Helpful? 1
  • +
  • -

#9 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 848
  • View blog
  • Posts: 2,585
  • Joined: 29-July 11

Re: Image.getWidth() and getHeight() return -1 first time after resizing

Posted 30 November 2012 - 10:51 PM

View Postg00se, on 30 November 2012 - 10:00 PM, said:

Quote

It gives a -1 for size because it has not finished loading. The questions is what is taking so long
What do you mean by 'so long'? As Casio has said, it's loaded in another thread, and as he's also said, you can use an ImageObserver, although it's simpler to use a MediaTracker which forces synchronicity (if you can't avoid asynchronicity or don't want to)


Ahuh. Thanks. :rolleyes:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1