package bugsfiling; import javafx.application.Application; import javafx.builders.GroupBuilder; import javafx.builders.RectangleBuilder; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Region; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; /** * * @author Alexander Kouznetsov */ public class BackgroundImage extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws Exception { final Rectangle rectangle = new RectangleBuilder() .width(100) .height(100) .fill(Color.TRANSPARENT) .stroke(Color.RED) .build(); Region pane = new Region() { { getChildren().add(rectangle); setTranslateX(20); setTranslateY(20); setMaxSize(100, 100); setStyle("-fx-background-image: url(\"http://farm4.static.flickr.com/3148/2466200617_3e2d28f8c0.jpg\"); " + "-fx-background-image-size: contain; " + "-fx-background-repeat: no-repeat;"); setOnMouseClicked(new EventHandler() { public void handle(MouseEvent t) { Region pane = (Region) t.getSource(); System.out.println("pane.size = " + pane.getWidth() + ", " + pane.getHeight()); } }); } }; Group root = new GroupBuilder().children(pane).build(); Scene scene = new Scene(root, 600, 600); stage.setScene(scene); stage.setVisible(true); } }