Results 1 to 4 of 4
  1. #1
    ruben381 is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default What's the best way to paint a tile grid?

    Hello everyone.



    I have a triple-layered grid system (3 2D byte arrays, e.g byte[][]), and I'm currently drawing it by running through the bytes in the array, grabbing the GridBlock object linked to the ID (the byte in the array), then getting the BufferedImage within that GridBlock object, and using Graphics.drawImage() to draw it in the grid. The issue is that drawing hundreds of BufferedImages can cause some major frame drops. Does anyone know a decent alternative, or an improvement I could make?

    Here's the code:

    Java Code:
    		g.setColor(Util.transparent);
    		for(int i = scrollX/tileSize; i < (scrollX+Properties.width)/tileSize+1; ++i){
    			for(int j = scrollY/tileSize; j < (scrollY+Properties.height)/tileSize+1; ++j){
    				for(int k = 0; k < (renderForeground?3:2); ++k){
    					byte[][] core = (k == 0 ? scene.getCore() : k == 1 ? grid.getCore() : fore.getCore());
    					if(i > 0 && i < core[0].length && j > 0 && j < core[1].length){
    						if(core[i][j]>0&&GridBlock.all[core[i][j]]!=null){
    							if(GridBlock.all[core[i][j]].getImage()!=null){
    								g.drawImage(GridBlock.all[core[i][j]].getImage(), i*tileSize-scrollX, j*tileSize-scrollY, this.tileSize, this.tileSize, null);
    								if(k==0){
    									g.fillRect(i*tileSize-scrollX, j*tileSize-scrollY, this.tileSize, this.tileSize);
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    Note: getCore() returns the 2D byte array associated with the layer, scrollX is the X distance scrolled by the player, and scrollY is the Y distance scrolled by the player. The transparent rectangle is drawn over layer 0 (the background) to simulate distance. Objects fore, grid and scene are instances of my class 'Grid' which essentially houses the 2D array.



    Thanks everyone.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: What's the best way to paint a tile grid?

    Have you tried drawing it to another BufferedImage and the drawing the entire image using drawImage? Also, if you would provide a complete, compilable, executable example, (Short, Self Contained, Correct Example) it would be easier for us to see the problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    ruben381 is offline Member
    Join Date
    Jan 2012
    Posts
    13
    Rep Power
    0

    Default Re: What's the best way to paint a tile grid?

    Quote Originally Posted by jim829 View Post
    Have you tried drawing it to another BufferedImage and the drawing the entire image using drawImage? Also, if you would provide a complete, compilable, executable example, (Short, Self Contained, Correct Example) it would be easier for us to see the problem.

    Regards,
    Jim


    I don't think that would be practical in this case. That would mean moving around 3 huge images, with the added issue that the grids are dynamic, and if these huge images are updated every time the grid is, that would mean constant, big freezes.


    My question, essentially, is: How can I mass-draw images or mass paint pixels the most efficiently?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: What's the best way to paint a tile grid?

    You need to provide the requested Short, Self Contained, Correct Example so we can both visualize and help with the problem. I have done quite a bit of graphics (some of it rather intense) and i never had a performance issue.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Paint is invalid type for variable paint.
    By minibronya in forum New To Java
    Replies: 3
    Last Post: 05-25-2012, 05:52 AM
  2. Tile Map
    By Lundager in forum Java 2D
    Replies: 5
    Last Post: 05-19-2012, 08:09 PM
  3. Open Grid Scheduler/Grid Engine
    By java software in forum Java Software
    Replies: 0
    Last Post: 03-27-2012, 07:00 PM
  4. Help tile TimeScrambler game
    By DrCooksAlot in forum New To Java
    Replies: 8
    Last Post: 01-25-2012, 03:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •