7 Replies - 647 Views - Last Post: 18 May 2011 - 06:37 AM Rate Topic: -----

#1 Apokio  Icon User is offline

  • D.I.C Addict

Reputation: 135
  • Posts: 511
  • Joined: 14-August 09

What does this line of code do?

Posted 17 May 2011 - 07:54 AM

I am working through a Java book and I have come to this method many times in the book and I know what the method does but I do not understand this while statement in the method. It looks like it checks if the image width is less than 0 and then just ends. It just doesn't look like it does anything at all. I will post the whole method.

public void load(String filename, int _columns, int _totalFrames, int _width, int _height) {
     Toolkit tk = Toolkit.getDefaultToolkit();
     image = tk.getImage(getURL(filename));
     while(image.getWidth(frame) <= 0);
     columns = _columns;
     totalFrames = _totalFrames;
     frameWidth = _width;
     frameHeight = _height;
}


That while statement just makes no sense to me. I have googled and looked all over and i can't make any sense of it. I have checked the entire book and he never explains that line of code. Just wondering what it does if anything?

Is This A Good Question/Topic? 0
  • +

Replies To: What does this line of code do?

#2 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: What does this line of code do?

Posted 17 May 2011 - 08:11 AM

  while(image.getWidth(frame) <= 0) {
//EDIT: remove ; after 0) and add parentheses {


Basically it is as you said, while the Image's width is less than 0 as observed by the Frame then execute the following lines of code:
  Toolkit tk = Toolkit.getDefaultToolkit();
	     image = tk.getImage(getURL(filename));
     while(image.getWidth(frame) <= 0) {
	     columns = _columns;
	     totalFrames = _totalFrames;
	     frameWidth = _width;
     frameHeight = _height;


In otherwards whilst the image has no width (can't have negative width as far as I know), reload the image(filename from specified URL)
I hope this has helped somewhat if you need help on the the Image Class, the documentation can be found Here

This post has been edited by v0rtex: 17 May 2011 - 08:23 AM

Was This Post Helpful? 1
  • +
  • -

#3 Apokio  Icon User is offline

  • D.I.C Addict

Reputation: 135
  • Posts: 511
  • Joined: 14-August 09

Re: What does this line of code do?

Posted 17 May 2011 - 08:20 AM

I understand that I can change it and make it a valid while loop, but the way I put it in is exactly how it is in the book. I wondered if it was just an editing error but I checked the website and there is nothing. It is also this way several places in the book. I was thinking that maybe there was some other form of a while statement that I just didn't understand, but it looks like I am right and this line basically does nothing helpful for the program.
Was This Post Helpful? 0
  • +
  • -

#4 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: What does this line of code do?

Posted 17 May 2011 - 08:25 AM

Not necessarily true, if one resizes the Frame, it will tamper with the width of the Image (doubt it will be 0 though) however it is also true that if the Image is not loaded correctly, assuming that you set the Image width after you load it, it is possible the while loop is there to check if the Image was loaded correctly, hence why it tries to get the image here:
 Toolkit tk = Toolkit.getDefaultToolkit();
	     image = tk.getImage(getURL(filename));


Revise the explanation of the code, I am sure there is a reason for this however as for:
     while(image.getWidth(frame) <= 0);


I am sure that is a editing error.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7884
  • View blog
  • Posts: 13,396
  • Joined: 19-March 11

Re: What does this line of code do?

Posted 17 May 2011 - 08:47 AM

[EDIT - my post was incorrect. removed to avoid confusion. See masijade's for a better answer]
[I really ought to be able to down-vote my own posts!]

This post has been edited by jon.kiparsky: 17 May 2011 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: What does this line of code do?

Posted 17 May 2011 - 09:13 AM

It looks as though whoever wrote that is assuming that toolkit getImage is an asynchronous process that returns immediately and continues loading the image in the background. If that were the case then getWidth may return 0 (or maybe -1) until the image is loaded (although it would probably only return that until it knows how wide the image is which could easilybe, and probably is, before the image is completely loaded). So, it looks as though he wanted to "wait" until the image was completely loaded. It is not a bad idea (badly done however), but toolkit's getImage is not asynchronous, although many of ImageIO's methods are. You would be better off creating an anonymous ImageObserver class and calling one of the ImageIO methods using that imageobserver that simply synchs on some object and calls notify, then the main class simply synchs on that Object and calls wait and waits until the imageobserver calls notify.

P.S. toolkit does not handle images very well (memory wise) as it usually winds up holding onto a reference to the image long after you've released the image. Use ImageIO.

This post has been edited by masijade: 17 May 2011 - 09:16 AM

Was This Post Helpful? 3
  • +
  • -

#7 Apokio  Icon User is offline

  • D.I.C Addict

Reputation: 135
  • Posts: 511
  • Joined: 14-August 09

Re: What does this line of code do?

Posted 18 May 2011 - 06:11 AM

Just to update. I wrote the author an email and he says that that line of code is "programmer's shorthand." He told me it is used to check if the image is loaded correctly. Now I did some testing of my own and if I comment out that line of code the program still runs. If I it commented out and then point it at an image file that does not exist then the program trys to run and x y cord trackers and frame counts still tick off like an invisible image is bouncing around the screen. If I point at a wrong file and leave that line of code in then the program just brings up a black JFrame. So basically somehow it stops the program from running if the wrong image is loaded. I am going to step through with the debugger and check it out.
Was This Post Helpful? 2
  • +
  • -

#8 qxc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-April 11

Re: What does this line of code do?

Posted 18 May 2011 - 06:37 AM

Good you went the extra mile to find out :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1