Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest way to draw pixels in Java

I have some code that generates particles at random locations, and moving in random directions and speed.

Each iteration through a loop, I move all the particles, and call repaint on my jpanel.

For 1,000 particles, I'm getting around 20 to 30 frames per second. I plan to eventually have 100,000 to 1,000,000 particles.

In paint, I only create a new bufferedimage if the window has changed size. I draw the pixels to the bufferedimage, and then call drawImage to display the image.

Each particle is a single pixel, and I have determined that all the time is taken up actually drawing the pixels. So, increasing the number of particles will drastically reduce the frame rate.

I've tried g.drawline(x,y,x+1,y), img.setRGB(x,y,color), getting an array of pixels by calling img.getRaster().getDataBuffer().getData(), then setting pixelData[y*width+x] = color.

I only get a small difference in the frame rate with these different ways of drawing the pixels.

Here's my question: What is the fastest way to draw pixels? Is bufferedimage even the way to go?

Thanks.

like image 803
Lance Wentz Avatar asked Nov 30 '12 03:11

Lance Wentz


1 Answers

I think the direct pixelmanipulation via the databuffer of the bufferedimage is the fastest way to draw something with the standard-library because you reduce the graphics object overhead to a minimum.

But as Perception said if you want to display 100'000 particles or more you should consider the GPU programming with OpenCl.

LWJGL for a small and easy to use Java-OpenGL/CL/AL binding

like image 115
ioboi Avatar answered Oct 05 '22 06:10

ioboi