Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...