I was wondering if it would be more efficient to render a single BufferedImage instead of rendering a bunch of BufferedImages in a double for loop?

I have an idea of how to do it, I just don't know what methods in BufferedImage you would use to create the bigger image. I'm assuming it has to do with setRGB() but what would the pseudo code look like when using getRGB on all the smaller images?

The reason I was thinking it would be more efficient to render that way is because you can render only a smaller portion of it (using getSubImage()) and only edit the image when it needs to.

Here's my Rendering code right now.

public void render(Graphics g, int x, int y, int width, int height)
    int renderX = 0;
    int renderY = 0;
    for (int i = x; i < width; i++)
        for (int j = y; j < height; j++)
            if(layerData[i][j] != null)
            g.drawImage(layerData[i][j].getImage(), renderX * Tile.SIZE, renderY * Tile.SIZE, null);

    renderY = 0;

layerData is the Maps Tile Data and getImage() returns that Tile's BufferedImage.

PS: I am using a TileSheet.

Edited by pritaeas: Moved to software dev.

2 Years
Discussion Span
Last Post by JamesCherrill

IF you really do have a lot of drawing that is repeated without being changed and if that is consuming a significant amout of resources, then yes, there MAY be an advantage in pre-rendering it.
If so, just create a BufferedImage, and call its createGraphics() to get a Graphics2D that allows you to use your existing drawing code to draw directly to the BufferedImage.

.. but we've spoken before about premature optimisation... ;) J

Votes + Comments
yes, we did :P
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.