Images loading slowly

I’m currently working on a game which uses a few .PNG files from
a local folder. When I run the game, these images load very slowly:

here is some pseduo-code representing my game


Screen class extends Thread implements runnable
-every Thread.sleep(50) it passes
its graphics objects (I use two for buffering) and itself as
an observer to either the BattleManager or Overworld Manager
depending on it's state


BattleManager

beginNewBattle()
Toolkit t = Toolkit.getDefaultToolkit();
Image background = t.createImage(filepath);
Image player
Image menubuttons
Image w/e

drawBattle(g, gBuff, Canvas observer)
{
draws Images to gBuff, then draws gBuff to g
}

during the game, beginNewBattle is called within the BattleManager as soon as
a fight is about to break out. But it usually takes a long time (sometimes 10 seconds or so)
to finally display the background (a 1000x800 pixel image).

Is there a way to speed the load process of these Toolkit images, or a way to allow the images to load before
attempting to use them?

Thanks!

a) Use MediaTracker and wait till the images are loaded.
b) Use ImageIO to load the images synchronously. (This option makes more sense than using an asynchronous method with some kind of listener mechanic in order to get the synchronous behavior you actually want.)

Ok, so let’s say I want to use the ImageIO.
Looks like it’s an import from javax?

How will synchronized loading help my speed issue, and
more importantly, how do I utilize the ImageIO to
get my images?

BufferedImage image = ImageIO.read(new File("something.png"));

I can already see huge changes in load time just by changing my
background image to be a BufferedImage.
My overworld tile set also loads almost instantly.

I’m going to make sure the rest of my stuff is BufferedImage.

Thanks for the help you guys!

I’m using Nate’s code snippet:

BufferedImage image = ImageIO.read(new File("something.png"));

to create my character image on the screen but I need it to support transparecny like the
Toolkit images do.

The only problem is

// Create a buffered image that supports transparency
    bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

I can do one or the other, but how do I get an image from my png file AND
have it recognize the transparency?

I don’t see why ImageIO.read would load your PNG without transparency? Are you sure you are saving your PNG files properly?

I just resaved the images and tried it again.
They still do not show correctly. Anything else I can try?

It should be working, but you can try this:


//Import the PNG.
BufferedImage *png = ImageIO.read(myPNGFileLoc);

// Create a buffered image that supports transparency
bimage = new BufferedImage(png.getWidth(), png.getHeight(), BufferedImage.TYPE_INT_ARGB);

bimage.getGraphics().drawImage(png,0,0);

That’ll put the correct image data into the image type that you want.

You first have to clear the new BufferedImage, because it will be black.


   public static void makeTransparant(BufferedImage img)
   {
      Graphics2D g = img.createGraphics();
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
      g.fillRect(0, 0, img.getWidth(), img.getHeight());
      g.dispose();
   }

Attach the png file in question to one of your posts.

http://photos-g.ak.fbcdn.net/hphotos-ak-snc1/hs057.snc3/14440_157407663341_602278341_2876961_4552133_n.jpg

That’s an example of one of the images.

The combined suggestions of Demonpants and Riven didn’t work either.
It was working fine with the Toolkit images, but the new BufferedImages aren’t working. O_o
I don’t think it has to do with the png files, due to the above statement.

That’s a jpg not a png.

the jpg format does not support transparency.

Got wrecked.

I got it taken care of now, thanks.