This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Game Development and the fly likes Drawing An Image In Full-Screen Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Drawing An Image In Full-Screen" Watch "Drawing An Image In Full-Screen" New topic
Author

Drawing An Image In Full-Screen

Brandon Tom
Ranch Hand

Joined: Jan 11, 2006
Posts: 48
I'm trying to draw a single image in full-screen mode. I can get it to switch into full-screen and draw strings, but not images. Here's my code:



This draws a blue screen with the string "Loading iamges...". Which means taht imagesAreLoaded is never being flipped to true. I have changed my code so that loadImage () appears like this following:



That draws a blue screen, but no "Loading images..." string, which I think means that it atleast allows the program to flip the imagesAreLoaded switch.

For the sake of completion, I'll post the ScreenManager class as well.


[ February 05, 2006: Message edited by: Brandon Tom ]

Brandon Tom, SCJP, MCP<br />Programmer/Graphic Designer<br /><a href="http://mailto:tombrand@hawaii.edu" target="_blank" rel="nofollow">tombrand@hawaii.edu</a>
Brandon Tom
Ranch Hand

Joined: Jan 11, 2006
Posts: 48
I forgot to mention that I'm using Java 5.0
Jeremy Tartaglia
Ranch Hand

Joined: Mar 11, 2004
Posts: 62
This is a flaw (in my opinion anyway) in the ImageIcon class. It does not throw an exception when the file is not found. It does not return a null ImageIcon class. It doesn't even return a null Image class from getImage(). In short, it will load a nonexistant file with a width and height of -1. That's the only way I found to detect when the file is not being loaded, or being loaded improperly.

I'd bet that's what's happening. Make sure the file is being loaded.
Brandon Tom
Ranch Hand

Joined: Jan 11, 2006
Posts: 48
So basically, there's two issues: the first is that ImageIcon is not finding the image and failing to report that (in a way that's meaningful to this program), and the second issue would be that the image I'm trying to load isn't where I'm saying it is... hmm...

There is still something very strange occurring. loadImages () is still exiting before it switches the imagesAreLoaded flag to true. I'm baffled as to why this occurs if not exception is being thrown.

Thanks for the insight. It has been very clarifying.
[ February 06, 2006: Message edited by: Brandon Tom ]
Brandon Tom
Ranch Hand

Joined: Jan 11, 2006
Posts: 48
I guess, in a way, having ImageIcon.getImage () return an Image object with a height and width of -1 makes some sense.

Some programs have, literally hundreds of images, most of which are insignificant to the core functionality of the program. If you had to deal with try and catching in those circumstances, it might be a big headache (although I don't think it would take too a big a brain to work around it).

Null Image objects might be not better. You might not want to have to test every insignificant image whether or not it's a real object before you try to draw it. It could bear down on the application.

By returning an actual Image object with significant markers, if the image couldn't be found, you could still draw, but it would just draw a blank. If the images was crucial to the program you could still check it.

I'm just surprised at how hard it was to learn about this. I've been to dozens of websites that actual neglect to mention this fact.

Of course if I had just checked the API...
[ February 06, 2006: Message edited by: Brandon Tom ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
If you wanted to see if ImageIcon's image loading succeeded, you could always call it getImageLoadStatus method. That being said, I prefer to use javax.imageio.ImageIO.


There is no emoticon for what I am feeling!
Brandon Tom
Ranch Hand

Joined: Jan 11, 2006
Posts: 48
Well I resolved the issue. I discovered that when I run my program from the NetBeans IDE, creating a JAR and launching it from a directory where the image files aren't. I wonder if I can configure that.

I'll still need to modify the code so that I can detect when an images isn't

Also, I should look into the ImageIO class. That's the second time someone has mentioned it. What are the advantages of ImageIO to ImageIcon?
[ February 06, 2006: Message edited by: Brandon Tom ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
>Also, I should look into the ImageIO class. That's the second time someone
>has mentioned it. What are the advantages of ImageIO to ImageIcon?

1. ImageIO knows more image formats. It has a plug-in architecture so you can install more reader/writers. A typical additional format people are interested in is TIFF.

2. You may not be interested in it for this example, but as the name suggests, the ImageIO classes also allow you to write images.

3. ImageIO reads in an image as a java.awt.image.BufferedImage, which for a lot of uses is a more convenient API than java.awt.Image. No more MediaTrackers!

4. If you create an ImageIcon from a URL or a filename it will uses Toolkit's getImage method which caches the Image data, and often this cache gets in the way. For example, if you update a file and try to reread an image, you will get the stale cached copy instead.

5. You can do more advanced things with image files like just reading the height and width of the image, reading any stored thumbnails or reading a series of images (common in TIFF files).

6. If you want to read a quick thumbnail on the fly, you can set the subsampling and read only every fourth pixel, for example, instead of creating the larger image only to shrink it.

7.Using an IIOReadProgressListener you can animate a progress bar to show how reading is progressing in large files.

I'm sure others can add more to this list.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drawing An Image In Full-Screen
 
Similar Threads
nullPointerException found regarding Window
Images don't painting
Developing Games In Java chapter 2 example not doing as i would have expected
background and foreground color is not changing in full screen window
Having problems with Window and Frame