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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With