# Looping images

• December 4th, 2011, 04:10 PM
SkyAphid
Looping images
So, I made a tile engine so I can draw different parts of a map easily, but for some reason I get a strange lag when I do it.
The tile render code looks as such:

Code :

public void drawMap(Graphics g, Image tileImages[]){
int drawDistanceX = 60;
int drawDistanceY = 60;

if (keyStatus == "DOWN"){
switch(key){
case 100:
if (mapX < mapWidth-20){mapX++; break;}
case 97:
if (mapX > 0){mapX--; break;}
}
}

if (mapWidth > 20){drawDistanceX = 20;}else{drawDistanceX = mapWidth;}
if (mapHeight > 15){drawDistanceY = 15;}else{drawDistanceY = mapHeight;}

for (int y = 0; y < drawDistanceY;y++){
for (int x = 0; x < drawDistanceX; x++){
if (mapTiles[x+(int)mapX][y+(int)mapY][0] > 0){
g.drawImage(tileImages[mapTiles[x+(int)mapX][y+(int)mapY][0]],x*16,y*16,null);
}
}
}
}

Now, I don't think it's the image drawing itself, but I could be wrong. Something this simple really shouldn't bother java as much as it is.
• December 4th, 2011, 05:19 PM
Norm
Re: Looping images
if (keyStatus == "DOWN"){
One problem here: Use the equals() method when comparing Strings, not ==

Quote:

I get a strange lag when I do it.
Please explain. Also add debugging printlns to your code that print out the current time from the System class to show what the lags are you are talking about.
• December 4th, 2011, 05:27 PM
SkyAphid
Re: Looping images
It just generally lags when a decent amount of tiles are being displayed on the buffer.
Here are some of the lines printed by the debug for the time.

1323041044269 <---Clear area. Thread is updating fast.
1323041044289
1323041044309
1323041044329
1323041044349
1323041044369
1323041044389
1323041044409
1323041044429
1323041044449
1323041044469
1323041044489
1323041044509
1323041044529
1323041044549
1323041044569
1323041044589
1323041044609
1323041044629
1323041044649
1323041044669
1323041044689
1323041044709
1323041044729
1323041044749
1323041044769
1323041044789
1323041044809
1323041044829
1323041044849
1323041044870 <---Moving into the tiled area. Notice how less frequently it's updated.
1323041044891
1323041044914
1323041044937
1323041044961
1323041044986
1323041045012
1323041045038
1323041045066
1323041045095
1323041045124
1323041045153
1323041045178
1323041045204
1323041045245
1323041045292
1323041045343
1323041045396
• December 4th, 2011, 06:46 PM
SkyAphid
Re: Looping images
Bump!
• December 4th, 2011, 06:55 PM
Norm
Re: Looping images
I didn't see any time lags in the list of times you posted.
Where do you see the lags?

To make the time lags easier to see, save the last time and next time you are there subtract it from the next current time and print that and then save that time as the last for the next iteration.
• December 4th, 2011, 07:56 PM
SkyAphid
Re: Looping images
I made one of those, but it was more messy, and I couldn't tell where the lags were myself. But I think we need to get off the subject of measuring the lags because that isn't going to get us anywhere with this. The lags are noticeable, and the occur when there are a lot of tiles being displayed, whereas when there isn't a large amount, it speeds back up. It's graphical lag, it stutters, and speeds up, and stutters. That kind of thing. I just want to know if the above lines of code would seem too much of a memory hog.
• December 4th, 2011, 08:03 PM
Norm
Re: Looping images
I suppose it depends on how many times a draw method is called.
Is there a way to call the draw method fewer times?
Can some of the drawing be reused? Draw the non moving part to a BufferedImage and only draw over a small part of it before drawing the updated image on the GUI.
• December 4th, 2011, 08:09 PM
SkyAphid
Re: Looping images
Quote:

Originally Posted by Norm
I suppose it depends on how many times a draw method is called.
Is there a way to call the draw method fewer times?
Can some of the drawing be reused? Draw the non moving part to a BufferedImage and only draw over a small part of it before drawing the updated image on the GUI.

I don't understand what you're getting at. Could you be more descriptive please?
• December 4th, 2011, 08:14 PM
Norm
Re: Looping images
If you call the draw method 1000+ times it will be slower than if you call it 10 times.

If some of the images the draw method is drawing are in the same location that they were drawn the last time the draw method was called, why bother drawing them again if there will be no change. Skipping a call to the draw method should speed up the code. Only redraw what has changed.
• December 4th, 2011, 08:17 PM
SkyAphid
Re: Looping images
So, a good way to do this is just to check if the key has changed, and if so, then go ahead and redraw? I gotcha.
• December 4th, 2011, 08:21 PM
Norm
Re: Looping images
It could involve keeping track of what has moved and what hasn't.
Only Redraw where needed.