How to create a JavaFX texture


Question

I can make a rectangle with JavFX like this:

Rectangle node2 = RectangleBuilder.create()
            .x(-100)
            .y(-100)
            .width(200)
            .height(200)
            .fill(Color.GREEN)
            .build();

but how can I make it use a texture instead of just a color?

TIA

1
2
1/17/2013 10:31:51 PM

Accepted Answer

Set the fill to an ImagePattern

Rectangle node2 = 
  RectangleBuilder.create()
    .x(-100)
    .y(-100)
    .width(200)
    .height(200)
    .fill(
      new ImagePattern(
        new Image("file:flower.png"), 0, 0, 1, 1, true
      )
    )
    .build();

There are additional samples in the ImagePattern javadoc.

For JavaFX8, you will also be able to do this via css.

3
1/17/2013 10:45:10 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon