使用 SWT 进行快速像素绘图?

发布于 2024-07-16 06:37:08 字数 367 浏览 4 评论 0原文

我正在寻找一种快速、简单的方法来在 SWT 画布中绘制任意颜色的像素。 到目前为止,我正在使用类似的东西:

// initialization:
GC gc = new GC(canvas);

// inside the drawing loop:
Color cc = new Color(display, r, g, b);
gc.setForeground(cc);
gc.drawPoint(x, y);
cc.dispose();

这非常慢。 用像素填充 300x300 画布大约需要一秒半的时间。 我可以在屏幕外创建一个图像,在其中设置像素,然后绘制图像。 这会更快,但我特别想要在画布上逐像素绘制图像的渐进绘画效果。

I'm looking for a fast and easy way to plot arbitrarily colored pixels in an SWT Canvas.
So far I'm using something like that:

// initialization:
GC gc = new GC(canvas);

// inside the drawing loop:
Color cc = new Color(display, r, g, b);
gc.setForeground(cc);
gc.drawPoint(x, y);
cc.dispose();

This is horribly horribly slow. it takes about a second and a half to fill a 300x300 canvas with pixels.
I could create an image off-screen, set the pixels in it and then draw the image. This will be faster but I specifically want the gradual painting effect of plotting the image pixel by pixel on the canvas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埋葬我深情 2024-07-23 06:37:09

创建一个 BufferedImage 对象:

BufferedImage bi = new new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);

在绘图循环中设置像素:

bi.setRGB(x, y, int_rgb);
...

最后显示缓冲图像:

g.drawImage(bi, 0, 0, null); 

如果您发现 setRGB() 慢,您可以直接访问位图数据:

int[] raster = ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();

然后

raster[y * 300 + x] = int_rgb;

Create a BufferedImage object:

BufferedImage bi = new new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);

inside the drawing loop set your pixels:

bi.setRGB(x, y, int_rgb);
...

and finally display the buffered image:

g.drawImage(bi, 0, 0, null); 

If you find setRGB() slow, you can access the bitmap data directly:

int[] raster = ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();

and later

raster[y * 300 + x] = int_rgb;
西瓜 2024-07-23 06:37:08

我敢打赌,影响性能的是分配和释放 90,000 个 Color 对象。 请记住,在 SWT 中,每个 Color 对象都会分配本机资源,这就是您必须 dispose() 它的原因。 这意味着每次分配和处置 Color 对象时,都必须在 JVM 和本机代码之间来回转换。

您可以在 300x300 像素循环中缓存 Color 实例,然后在循环后处理这些对象吗? 您需要一个有点智能的缓存,它只能容纳最多这么多的对象,然后将处理其中的一些条目,但这应该会大大加快速度。

I bet that what is killing performance is allocating and releasing 90,000 Color objects. Remember, in SWT, each Color object allocates native resources, which is why you have to dispose() it. This means each time you allocate and dispose a Color object, you have to transition from the JVM to native code and back.

Can you cache your Color instances while in the 300x300 pixel loop and then dispose of the objects after your loop? You'd need a somewhat intelligent cache that only holds a maximum of so many objects, and after that will dispose of some of its entries, but this should speed things up greatly.

吻安 2024-07-23 06:37:08

您可以绘制多个屏幕外图像,逐渐填充 300x300 区域。 这样您就可以控制图像显示的速度。

You could draw several offscreen images where you gradually fill the 300x300 area. This way you can control how fast the image should appear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文