Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Looping images

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Looping images

    if (keyStatus == "DOWN"){
    One problem here: Use the equals() method when comparing Strings, not ==

    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.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looping images

    Bump!
    I need help. Please?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

  8. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looping images

    Quote Originally Posted by Norm View Post
    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?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

  10. #10
    Junior Member
    Join Date
    Aug 2011
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Looping images

    It could involve keeping track of what has moved and what hasn't.
    Only Redraw where needed.

Similar Threads

  1. Need help with looping!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 04:09 PM
  2. Help in looping
    By endframe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 28th, 2010, 03:24 PM
  3. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM
  4. Images not going in email
    By anjali09s in forum Java SE APIs
    Replies: 3
    Last Post: August 2nd, 2009, 06:06 PM
  5. What are the best way of placing images in GUI?
    By Ciwan in forum AWT / Java Swing
    Replies: 5
    Last Post: February 26th, 2009, 05:19 PM