Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The fastest filling one pixel in JavaFX

I use canvas in JavaFX library. I need to draw many pixels as fast as possible. I execute this :

canvas.getGraphicsContext2D().setFill(color);
canvas.getGraphicsContext2D().fillRect(x, y, 1, 1);

I suppose it is very slow. Can I use faster way? Maybe should I use another way/method to fill one pixel?

like image 971
karoluch Avatar asked Feb 09 '15 19:02

karoluch


People also ask

Does JavaFX use GPU?

Graphics Support For JavaFX applications to take advantage of the new hardware acceleration pipeline provided by JavaFX, your system must feature one of a wide range of GPUs currently available in the market.

What is graphics context in JavaFX?

Class GraphicsContext. This class is used to issue draw calls to a Canvas using a buffer. Each call pushes the necessary parameters onto the buffer where they will be later rendered onto the image of the Canvas node by the rendering thread at the end of a pulse.

What is a canvas in JavaFX?

Canvas class is a part of JavaFX. Canvas class basically creates an image that can be drawn on using a set of graphics commands provided by a GraphicsContext. Canvas has a specified height and width and all the drawing operations are clipped to the bounds of the canvas.


1 Answers

You can use the PixelWriter from an Image. As @jewelsea said use canvas.getGraphicsContext2D().getPixelWriter()

With the PixelWriter you can paint any Pixel you want:

pixelWriter.setColor(int x, int y,Color c)

I think this is the best way for only Paint pixels.

You can also waive the Canvas and use an ImageView with an WriteAbleImage in it.

In this case you can use writableImage.getPixelWriter() in the same way as the Canvas.

If this is still to slow you can Execute the pixelWriter.setColor(..) in a background Thread so the Application don't stop.

See also:

canvas performance

I hope this answer helps you.

like image 157
Marcel Avatar answered Sep 21 '22 11:09

Marcel