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

Thread: Drawing many Tiles on screen

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Drawing many Tiles on screen

    Hi, I've been working on my game for a while now, and I've started making levels. Unfortunately, when I have a lot of tiles on screen, some tiles will lag behind on updating the position they're drawn on by a frame. Reducing the frame rate doesn't help this, so is there any solution that doesn't involve making the tile size bigger?

    Also in case you're wondering, they're drawn at x-Frame.sx, y-Frame,sy. With Frame.sx and sy being the scroll value.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Drawing many Tiles on screen

    I'm assuming the problem might be that I'm using Image Icons instead of Buffered Images. Could anyone here tell me how to use Buffered Images in a way that will compile to a .jar?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Drawing many Tiles on screen

    Your question appears to be missing a lot of information such as what graphics/gui library you're working with.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Drawing many Tiles on screen

    Quote Originally Posted by Gravity Games View Post
    Hi, I've been working on my game for a while now, and I've started making levels. Unfortunately, when I have a lot of tiles on screen, some tiles will lag behind on updating the position they're drawn on by a frame. Reducing the frame rate doesn't help this, so is there any solution that doesn't involve making the tile size bigger?

    Also in case you're wondering, they're drawn at x-Frame.sx, y-Frame,sy. With Frame.sx and sy being the scroll value.
    Assuming you are using paintComponent (guessing from previous questions) include that method so we can see what is happening. If you are using something else (in addition to or in place of) then show what ever you are using to draw as a minimum.

  5. #5
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Drawing many Tiles on screen

    Ok.

    public void render(Graphics g, Component c){
    		//g.setColor(Color.red);
    		//g.fillRect(x-Frame.sx,y-Frame.sy,16,16);
    		if (TileId==1){
    		tileset1groundlefttop.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==2){
    			//g.fillRect(x-Frame.sx,y-Frame.sy,16,16);
    			tileset1groundtop.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==3){
    			tileset1groundrighttop.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==4){
    			tileset1groundleft.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==5){
    			tileset1ground.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==6){
    			tileset1groundright.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==7){
    			tileset1groundbotleft.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==8){
    			tileset1groundbot.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==9){
    			tileset1groundbotright.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==10){
    			tileset1grounddrytop.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==11){
    			tileset1groundsides.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==12){
    			tileset1trunkslim.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==13){
    			tileset1groundlintersectleft.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==14){
    			tileset1groundlintersect.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==15){
    			tileset1groundlintersectright.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==16){
    			tileset1groundrintersectleft.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==17){
    			tileset1groundrintersect.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==18){
    			tileset1groundrintersectright.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==19){
    			tileset1spike1.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==20){
    			tileset1spike2.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==21){
    			tileset1groundsteepslopel.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==23){
    			tileset1brick.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==24){
    			tileset1door1.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    		if (TileId==25){
    			tileset1door2.paintIcon(c,g,x-Frame.sx,y-Frame.sy);
    		}
    	}
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Drawing many Tiles on screen

    I don't quite see the big picture from just the render method.

    Explain more about what is lagging behind. Is it always lagging at a certain point? Is it always the same tile or same type or same location?
    Include more code related to the drawing and explain exactly how you plan to draw the entire screen, when/how render is called.

  7. #7
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Drawing many Tiles on screen

    Well, that's the whole render method, at least for the tiles. It also happens at random when a lot is on screen, no patterns whatsoever. Also, its not that the tiles don't actually draw, its that they lag when I update the position. Say a tile is initially drawn at 400-sx for x, 300-sy for y and 0 for sx and sy. The character will move 3 pixels to the right, and increase sx by 3 as well. So x is now 397, y is still 300, sx is now 3, and sy is still 0. Sometimes for a single frame, the tile will still be drawn at 400 for x.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Drawing many Tiles on screen

    I am thinking the problem is in how and when render is called. There is just not enough code here to offer specific advice.

    Try some debugging and see where the bottleneck is. The drawing should start and complete without much else taking place.

    Are you drawing an image off screen, and painting that image on the screen or drawing directly to the image on the display? So many questions could be asked... so many possible things can cause the problem described.

  9. #9
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Drawing many Tiles on screen

    Found the problem, and it was a dumb one too... I apparantly was calling the repaint method too much, and it was trying to update the screen 3 times over...
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Drawing many Tiles on screen

    Found the problem, and it was a dumb one too...
    One day all problems look dumb. Then you realize anyone can make mistakes and you quit worrying about dumb problems and just call them bugs...
    It is the new thing. All the cool kids do it.

  11. #11
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Drawing many Tiles on screen

    Agree but again a suggestion, next time please give a more complete description of your problem including that you're using Swing, etc.

  12. #12
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Drawing many Tiles on screen

    Quote Originally Posted by curmudgeon View Post
    Agree but again a suggestion, next time please give a more complete description of your problem including that you're using Swing, etc.
    Sorry, I didn't know things like that the little things like that could actually affect the issue...
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  13. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Drawing many Tiles on screen

    Quote Originally Posted by Gravity Games View Post
    Sorry, I didn't know things like that the little things like that could actually affect the issue...
    There are many ways to do most things. The more we know about 'how' you are trying to accomplish something, the easier it is to see where things went off track...

Similar Threads

  1. Drawing Objects on Screen
    By Gravity Games in forum Object Oriented Programming
    Replies: 7
    Last Post: August 27th, 2012, 02:37 PM
  2. Drawing in Full Screen
    By MizukiTHPS in forum AWT / Java Swing
    Replies: 7
    Last Post: March 27th, 2012, 12:48 PM
  3. Replies: 4
    Last Post: July 20th, 2010, 08:15 AM
  4. Struts 1.3.8 and tiles
    By jsnx7 in forum Web Frameworks
    Replies: 3
    Last Post: July 21st, 2009, 04:29 AM
  5. Drawing "Hello world" on screen
    By shikh_albelad in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 04:21 AM