PDA

View Full Version : [Java] Pictures question


ramapple
12-14-2006, 04:30 AM
Recently, (call me a noob if you want), I found that initialzing a new ImageIcon in the paint function is a terrible thing to do, as it will always look for the image which will slow the game down and/or flicker. So the solution to this problem is to define the ImageIcon in the class, and call the picture in public void init or something.

However, would I have to do this process for 100++ pictures? I tried to use and array to load all my pictures, but then I had a problem finding the one I needed. Is there another solution to stop the game from being so slow/flickering? Or just a better way to do this in general?

Aradon
12-14-2006, 06:16 PM
Can you associate the pictures with some type of string? Such as the filename or something of that sort? That way you can look it up in a hashmap which will give you as close as O(1) time as you're going to get. There is also included a way to get the entrySet from the hashmap if you need to go through each of them. This is included in the API:

HashMap (http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html)

Basically I think you need to look into ways to optimizing the whole thing. It may be worth wild to do a search on google about optimizing java code. Besides that without looking at the code I can't tell you much else about it :P

ramapple
12-14-2006, 11:48 PM
Thanks for the reply Aradon; and yes, I associate file names with Strings. This is how I had the pictures and making them change:


class MainChar
{
String left = "left";
String right = "right";
String flying = "fly";
String standing = "stand";

int x, y;

String side;
String state;

ImageIcon e;

public MainChar(int x, int y)
{
this.x = x;
this.y = y;

side = left;
state = standing;
}

public void changeSide(int number)
{
if(number == 1)
{
side = left;
}

else if(number == 2)
{
side = right;
}
}

public void paint(Graphics g)
{
e = new ImageIcon(state + side + ".png");
e.paintIcon(null, g, x, y)
}
}


And doing so, will slow the game drastically both during gameplay and loading. Will HashMap make it much more efficient? If so, how can I be doing so? I looked over the API on hashmap, and I'm not too sure how I could use it.

daniel_g
12-15-2006, 04:40 AM
would I have to do this process for 100++ pictures?
hmmm never seen a java game that uses 100+ pictures at a time. Maybe that's the reason cell phone games have very small levels and repetitive items, and still take ages to load.

I'm going to guess the best way to do it, is initialize everything you need for a specific level from the beggining, and not as you go. That will probably slow down the loading for each level, but the game will run smooth.

ramapple
12-15-2006, 05:48 AM
I could do that, but the HashMap idea is interesting. I'm not too sure how it works, but I assume it will make the game run more smooth without having to write in tons of lines just importing pictures... or I could be wrong.

ramapple
12-16-2006, 03:13 AM
Fixed! Thank you Aradon for the HashMap idea, it works like a charm after a few trials. If anyone wants to see what was done, pm me. This isn't the first time that you helped me =)