[Solved] Java – What’s the most efficient way of drawing large number of pixels on the screen


The process of showing an image in memory to the screen-buffer is done deep inside the Java API by a process called blitting. The details differ between the type of image you use and even between Java versions. Images in Java can be ‘accelerated’ which means they can be anything between an image compatible with your hardware and an image actually residing the GPU’s memory. If you instantiate a BufferedImage, in my experience it’s never ‘accelerated’ but still very fast.

If you want to write pixels into an array and show them quickly, use the following method:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image.setAccelerationPriority(0); // Just to be sure
int[] array = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

then just paint the image as usual after manipulating the array.

This has more performance than using .setRGB() since no color space conversion is done.

2

solved Java – What’s the most efficient way of drawing large number of pixels on the screen