I'm following a couple of Java2D tutorials, and am now trying to draw a simple PNG on a Canvas.
I create a BufferedImage
and then retrieve the pixel data with pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
.
Then, I load my PNG in a similar fashion:
spriteImage = ImageIO.read(Shape.class.getResourceAsStream(path));
width = spriteImage .getWidth();
height = spriteImage .getHeight();
spritePixels = spriteImage .getRGB(0, 0, width, height, null, 0, width);
However, I now have to write the contents from spritePixels
into pixels
, and am stumped on the formula:
Assuming the height and width of spriteImage
will always be less than image
, and that I'll always be drawing into position 0,0 I believe my formula will look something like this:
for (int spriteIndex = 0; spriteIndex < spritePixels.length; spriteIndex ++)
pixels[spriteIndex + offset] = spritePixels[x];
However, I can't seem to figure out how to calculate the offset. What's the formula, here?