For what I'm doing, I need to be able to draw a buffered image directly into a larger buffered image. I've searched around a little and still have failed to find a better way, but my current way is extremely inefficient and I would like a better way if it's possible. Any improvements onto this design would also be great, even if it's not a totally separate way.
public static final void drawOntoBI(final BufferedImage on, final BufferedImage draw, final int x, final int y)
{
// Calls method instead of needing to call them every loop
final int width = draw.getWidth(), height = draw.getHeight(), onwidth = on.getWidth(), onheight = on.getHeight();
// Stores temporary variables so they wont be recalculated
int x3, y3;
// Goes through all of the pixels in the drawn image to copy them
for(int x2 = 0; x2 < width; x2++)
{
x3 = x + x2; // Prevents having to do the same math 3 times
for(int y2 = 0; y2 < height; y2++)
{
y3 = y + y2;
// Makes sure the image to be drawn is not out of bounds, if it is, go to the next location
if(x3 > onwidth - 1 || x3 < 1 || y3 > onheight - 1 || y3 < 1) break;
// Draws the pixel on the image where it needs to be from the x and y looped in the image
on.setRGB(x3, y3, draw.getRGB(x2, y2));
}
}
}
This is the second biggest bottleneck on my program, so it really is important to be improved. It's called all the time, and is rather slow. Memory isn't an issue, so memory-using methods can be considered.