在Java中绘制像素的最快方法是什么 [英] 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.
每次迭代循环,移动所有粒子,在我的jpanel上调用重绘。
Each iteration through a loop, I move all the particles, and call repaint on my jpanel.
对于1,000个粒子,我每秒大约需要20到30帧。我计划最终拥有100,000到1,000,000个粒子。
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.
在绘画中,如果窗口的大小发生变化,我只会创建一个新的bufferedimage。我将像素绘制到bufferedimage,然后调用drawImage来显示图像。
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.
我尝试过g.drawline(x,y,x + 1,y),img。 setRGB(x,y,color),通过调用img.getRaster()获取像素数组.getDataBuffer()。getData(),然后设置pixelData [y * width + x] = color。
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.
这是我的问题:什么是最快的画像素的方法?是bufferedimage甚至可以去吗?
Here's my question: What is the fastest way to draw pixels? Is bufferedimage even the way to go?
谢谢。
推荐答案
使用img.getRaster()。getDataBuffer()。getData()时,您应该在标准计算机上获得更快的帧速率。我知道这是因为我可以以每秒20-30帧的速度绘制整个屏幕,屏幕总共有1,000,000像素。我通过将渲染例程切成两半并使用两个线程来获得此速度。我的CPU是1.5ghz。
You should be getting a much faster frame rate on a standard computer when using the img.getRaster().getDataBuffer().getData(). I know this for a fact because I can paint the entire screen in 20-30 frames per second and the screen has 1,000,000 pixels total. I obtained this speed by cutting the rendering routine in two and using two threads. My CPU is 1.5ghz.
出于这个原因,我认为你可能在移动像素时出现了编码错误。请记住:创建新对象的操作比添加操作长100倍。另外,看看你是否可以删除任何if语句。
For this reason, I think that you may have made a coding error in moving the pixels. Remember: creating a new object is a 100x times longer operation than addition. Also see if you can cut out any if statements.
此外,这可能很愚蠢,但我假设你只是调用img.getRaster()。getDataBuffer()。 getData()每帧一次?
Also, this may be silly, but I assume you are only calling img.getRaster().getDataBuffer().getData() once per frame?
在相关的说明中,绘制多像素粒子自然需要很长时间。
On a related note, Drawing multi pixel particles will naturally take a long time.
这篇关于在Java中绘制像素的最快方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!