Fast pixel building using SWT?

I am looking for a quick and easy way to build randomly colored pixels in a SWT canvas. So far, I'm using something like this:

// 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();

It is terribly slow. It takes about one and a half seconds to fill the canvas with 300x300 pixels. I could create an image overs, set pixels to it, and then draw an image. It will be faster, but I specifically want the gradual effect of drawing an image pixel by pixel on the canvas.

+3
source share
3 answers

, 300x300. , .

+1

, , , 90 000 Color. , SWT Color , dispose() . , , Color, JVM .

Color 300x300, ? , , , .

+4

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 buffer image:

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

If you discover setRGB () slowly, you can directly access the bitmap data:

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

and later

raster[y * 300 + x] = int_rgb;
-3
source

Source: https://habr.com/ru/post/1705589/


All Articles