Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

Java - What's the most efficient way of drawing large number of pixels on the screen

I've started working on a program in which I want to completely manipulate how pixels are being drawn to the screen. So I thought of storing the pixels data in an int or byte array and draw each pixel on the screen like every 1 millisecond, but from past experiences I know that this is extremely inefficient and the fps for a 600x550 canvas drops below 5... So, how come this does not happen when it comes to drawing an image with the same number of pixels? How does this Graphics method work? And are there even more efficient ways of doing it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...