Sign in to follow this  

PreLoading Images in java

This topic is 4455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to use the draw method to print pictures in my program. It works usually the second time the image is called. It seems it has to load the image before it will print the image. Is there anyway to preload the images so that it will print immediately. This is how im printing the image in the first place. myCard = myDeck.deal(); Graphics g2 = jPanel3.getGraphics(); Image card1 = getImage(getCodeBase(), myCard.getFileName()); g2.drawImage(card1, 0, 0, jPanel3); If anyone can help i would appreciate it

Share this post


Link to post
Share on other sites
Have you seen this page? (Under the heading Tracking Image Loading: MediaTracker and ImageObserver)

The easiest way is probably to use the MediaTracker- but AFAIK this only works in Applets.

Share this post


Link to post
Share on other sites
Looks like you're right, not sure why I was under that impression. Seems the MediaTracker would be the most attractive option, then.

Share this post


Link to post
Share on other sites
Also, don't reload/reget your image every time you want to draw it. Load it into an instance variable once at startup.

For your situation you would need an array of images, load all 52 cards, then instead of using the filename after a deal, ask each card for its index so you can get the correct image out of the array.

Share this post


Link to post
Share on other sites
Sign in to follow this