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.
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.
There are no new questions, but there may be new answers.
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.
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.
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.
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!
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.
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.