2

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

sproketboy
  • 8,967
  • 18
  • 65
  • 95

1 Answers1

3

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.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Thanks. How does it find the image? Where would I put the image for it to find it? It seems to need a full path. Can it find using the normal resource path? – sproketboy Jan 17 '13 at 23:21
  • 1
    Ah yes, my one and only question on StackOverflow to date: [Where does javafx.scene.image.Image(“flower.png”) look for flower.png?](http://stackoverflow.com/questions/10575410/where-does-javafx-scene-image-imageflower-png-look-for-flower-png) – jewelsea Jan 17 '13 at 23:24