Sign in to follow this  
BigED140

PreLoading Images in java

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this