2

I'm working with JavaFx and i'm looking for an equivalent of the AWT BufferedImage. I saw that I can used SwingFXUtils to used an awt BufferedImage with JavaFx but I don't want to used awt.

In fact I'm looking for a structure to display a table of pixel witch is associated to a ColorModel.

Does anybody know some equivalent with JavaFx ?

Thanks a lot.

pes502
  • 1,597
  • 3
  • 17
  • 32
Djeko
  • 21
  • 1
  • 5

1 Answers1

3

The closest you get to a BufferedImage in JavaFX is javafx.scene.image.WritableImage. It is a subclass of javafx.scene.image.Image, and was introduced in JavaFX 2.2.

Depending on your use case, javafx.scene.canvas.Canvas and javafx.scene.canvas.GraphicsContext (similar to a Graphics2D Java2D) might be a better fit.

To paint on a Canvas node and get the contents in a WritableImage, use (adapted from the Canvas JavaDoc):

// Create canvas
Canvas canvas = new Canvas(250, 250);
GraphicsContext gc = canvas.getGraphicsContext2D();

// Paint on it
gc.setFill(Color.BLUE);
gc.fillRect(75, 75, 100, 100);

// NOTE: The canvas must be part of a Scene for the following to work properly, omitted for brevity

// Obtain a snapshot of the canvas
WritableImage image = canvas.snapshot(null, null);

See Working with Canvas from he JavaFX tutorials for more information.

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • Thanks for your answer. I've tried WritableImage but it's not enough performant. If I want to change the color of each pixel, I've to do a "for" on each pixel. It's very slow. Do you know if there is an other way ? – Djeko Jun 26 '14 at 07:53
  • WriteableImage performs very well in my (limited) experience. See my answer [here, reading an image from file in 40-60ms](http://stackoverflow.com/a/24135889/1428606). But it may not suit your use case. Maybe `javafx.scene.canvas.Canvas` and `javafx.scene.canvas.GraphicsContext` is more what you are looking for? – Harald K Jun 26 '14 at 08:18
  • In fact I have a table of value (Each element of the table correspond to one pixel). And I want to associate each value to a color and then display the table. But the values of the table change everytime. So I have to change the color of each pixel in real-time. – Djeko Jun 26 '14 at 08:51
  • I think I have answered your question as accurately as I can. If you want help with something else, please post a new question, describing what you want to solve, and the code you have so far. It sounds to me like your best bet is `Canvas`, other people might have other/better ideas. :-) – Harald K Jun 26 '14 at 08:59
  • Thank you, I'll post a new question for more details. – Djeko Jun 27 '14 at 13:16