Creating shapes n stuff out of the BufferedImage..

I know, I’m going back to this subject again

I see a post by BurntPizza when searching on google about the bufferedimage which dated back to 2012

so lets say you set up this:

BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
int[] pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();

then you create the packed interger (which is what ive learnt and understood from this forum)

public static int pack(int r, int g, int b) {
   //clamp to range [0, 255]
   if (r < 0)
      r = 0;
   else if (r > 255) r = 255;
   if (g < 0)
      g = 0;
   else if (g > 255) g = 255;
   if (b < 0)
      b = 0;
   else if (b > 255) b = 255;
   
   //pack it together
   return (255 << 24) | (r << 16) | (g << 8) | (b);
}

then you can unpack it or whatever

But… with setting the pixels to be manipulated, how would I then go about creating shapes to use in an update method?

Also one last Q, what benefit does this have adding / import seperate PNG type sprites into this?

I’m finding it difficult this last few months with this bufferedimage thing, and its something I wana get to terms with — and yes, I do use OpenGL so no engine thing here please

Thanks peeps!!

I am not entirely sure what you are asking…

If you are wanting to use standard shapes e.g. circle, rect etc then using the Graphics2D object from the BufferedImage is the easiest approach:


Graphics2D g2 = (Graphics2D) img.getGraphics();
g2.setColor(new Colour(pack(r,g,b)));
g2.fillOval(20,20,20,20);
g2.dispose();

if you are wanting custom shapes and effects then it will be per pixel manipulation:


		int imgWidth = img.getWidth();
		int imgHeight = img.getHeight();
		for (int y = 0; y < imgHeight; y++)
		{
			for (int x = 0; x < imgWidth; x++)
			{
				int temp = (x + y) % 255;
				pixels[y * imgWidth + x] = pack(255 - temp, temp, temp ^ 255);
			}
		}
		ImageIO.write(img, "png", new File("out.png"));