/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ package textimagepatternbug; import com.sun.javafx.runtime.VersionInfo; import javafx.application.Application; import javafx.geometry.Bounds; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.image.Image; import javafx.scene.paint.Color; import javafx.scene.paint.ImagePattern; import javafx.scene.shape.Rectangle; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.scene.text.TextBoundsType; /** * * @author Aleksandr Sakharuk */ public class TextImagePatternNotBug extends Application { /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { Image image = new Image("file:resources/square.png"); ImagePattern pattern = new ImagePattern(image, 0, 0, 0.5, 0.5, true); Group root = new Group(); Text text = new Text("Text"); text.setX(100); text.setY(100); text.setBoundsType(TextBoundsType.VISUAL); // Note that using a CSS style will not give the right results for // getLayoutBounds() below because it requires a CSS pass to occur // to update the styles, so we hard-code the font to equivalent // values instead - which yields correct layout bounds on the first // try... // text.setStyle("-fx-font-size: 120; -fx-font-family: sans-serif; -fx-font-weight: bold;"); Font f = Font.font("sans-serif", FontWeight.BOLD, 120); text.setFont(f); text.setFill(pattern); root.getChildren().add(text); Bounds b = text.getLayoutBounds(); double x0 = b.getMinX(); double y0 = b.getMinY(); double x1 = b.getMaxX(); double y1 = b.getMaxY(); System.out.println("bounds = "+x0+", "+y0+" => "+x1+", "+y1); ImagePattern pattern2 = new ImagePattern(image, x0, y0+300, (x1-x0)/2.0, (y1-y0)/2.0, false); Rectangle r = new Rectangle(0, 300, 800, 300); r.setFill(pattern2); root.getChildren().add(r); Text text2 = new Text("Text"); text2.setX(100); text2.setY(400); text2.setBoundsType(TextBoundsType.VISUAL); // text2.setStyle("-fx-font-size: 120; -fx-font-family: sans-serif; -fx-font-weight: bold;"); text2.setFill(Color.GREEN); text2.setFont(f); root.getChildren().add(text2); Scene scene = new Scene(root, 800, 600); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); System.out.println(VersionInfo.getRuntimeVersion()); } }