This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes MediaTracker Practices Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "MediaTracker Practices" Watch "MediaTracker Practices" New topic
Author

MediaTracker Practices

R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

Hello all,
First let me take a second and explain that I'm not providing real code (code will not compile) at the moment because the actual code is very very long and I have no issues other than I'm just trying to make a splash screen because it is taking a little while to load the images in the beginning. I am happy to offer real code and I realize that I may not be very efficient in my code and am very interested in how I can be more efficient once I figure out how to complete the task I need. Currently I have an applet














The only thing that happens though is I don't get the splash page until the images have loaded because the images load one by one as evidence by my System.out.println("getting image : "+path); in the ImageHandler class's addImage method and I never get the StoryPage run method System.out.println("story page running"); . I would like the images to load with a splash screen and I had thought mediatracker was the way to go with this. Any suggestions or help would be greatly appreciated. Again the code above is not real and I hand coded it in the forum text box so there is probably a lot wrong with it I was just attempting to provide the architecture of the program so I could be given the best place to test the images.

Thanks again for any help this community can provide I have had better luck here then most other forums.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

ImageIO#read(...) blocks until the image is fully read, rendering use of a MediaTracker redundant in the code as posted.

To initiate image loading and continue execution without waiting for the image to be fully loaded, use Toolkit#createImage(...) / getImage(...). Make sure to read all the details in the API for those methods before deciding which to use.

edit I hope your real code doesn't swallow Exceptions in empty catch blocks. Except in the rare case that an Exception should be ignored, at the very least provide a printStackTrace().

edit2: The recommended method to override for custom painting in Swing components is paintComponent(...), not paint(...).

Another approach to this is to test the image for null in the painting method override and draw the splash text:With this approach, you should load the image with ImageIO.


luck, db
There are no new questions, but there may be new answers.
R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

Thank you Darryl for your reply. I am curious though about the api. I believe Java is one of the best documented languages out there. Even Microsoft has third party programs to purchase just to read the .NET framework classes provided which I believe to be ridicioulous. Although my source for java api is just oracles website is there another location, because although I believe it is well documented I don't believe it is documented so well that they say java will not move forward with ImageIO and will with Toolkit. Here's a snippet of both ImageIO.read from oracles website perhaps I'm missing your reference to the api where it says this.


read

public static BufferedImage read(File input)
throws IOException

Returns a BufferedImage as the result of decoding a supplied File with an ImageReader chosen automatically from among those currently registered. The File is wrapped in an ImageInputStream. If no registered ImageReader claims to be able to read the resulting stream, null is returned.

The current cache settings from getUseCacheand getCacheDirectory will be used to control caching in the ImageInputStream that is created.

Note that there is no read method that takes a filename as a String; use this method instead after creating a File from the filename.

This methods does not attempt to locate ImageReaders that can read directly from a File; that may be accomplished using IIORegistry and ImageReaderSpi.

Parameters:
input - a File to read from.
Returns:
a BufferedImage containing the decoded contents of the input, or null.
Throws:
IllegalArgumentException - if input is null.
IOException - if an error occurs during reading.

read

public static BufferedImage read(InputStream input)
throws IOException

Returns a BufferedImage as the result of decoding a supplied InputStream with an ImageReader chosen automatically from among those currently registered. The InputStream is wrapped in an ImageInputStream. If no registered ImageReader claims to be able to read the resulting stream, null is returned.

The current cache settings from getUseCacheand getCacheDirectory will be used to control caching in the ImageInputStream that is created.

This methods does not attempt to locate ImageReaders that can read directly from an InputStream; that may be accomplished using IIORegistry and ImageReaderSpi.

Parameters:
input - an InputStream to read from.
Returns:
a BufferedImage containing the decoded contents of the input, or null.
Throws:
IllegalArgumentException - if input is null.
IOException - if an error occurs during reading.



But I do believe you answered my question and will test it tomorrow. I am greatly appreciative of your help because I have posted this question in many different forums and places and was beginning to lose hope on getting an answer. My alternative move next was to multi-thread the image loading somehow.

Thanks
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

R Johnson wrote:I have posted this question in many different forums and places

In future, please BeForthrightWhenCrossPostingToOtherSites
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

R Johnson wrote:I'm missing your reference to the api where it says this.

While I agree that it would be nice if the API were to clearly indicate which method calls block and which return immediately, the API isn't the only documentation available. Scroll down to "Synchronicity" in this page.

Or go through the whole page, it's interesting reading!
R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

Thank you Darryl I will read the link you have provided. I do not like toolkit create because it creates an image and I need a buffered image in order to draw subimages, but it seems your link may address this so I may need to ask more on this after reading it. Thanks again for your help.

Edit:
I did not read past Image Creation mark, but it seems to disagree with the use of Toolkit/Applet/ImageIcon unless I'm only displaying the image and have no need for the bufferedImage capabilities. I have been avoiding a multithreaded solution for one because I assumed there might be an easier approach because I would think this to be a common issue. Thanks for the link though hopefully I can get a multithreaded solution to work. If you have any recommendations on the best place to implement and add such a thing I am all ears. Or if there is a way into the ImageIO thread to alter the effect this seems like it would be possible as well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MediaTracker Practices