import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.text.TextAlignment; import javafx.stage.Stage; import javafx.util.Duration; public class NullCanvasTextAlign extends Application { public static void main(String argv[]) { launch(argv); } void render(GraphicsContext gc, double hue) { gc.setFill(Color.hsb(hue, 1.0, 1.0)); gc.fillRect(0, 0, 300, 300); gc.setFill(Color.BLACK); gc.fillText("Null test", 20, 20); } @Override public void start(Stage stage) { Canvas c = new Canvas(300, 300); final GraphicsContext gc = c.getGraphicsContext2D(); render(gc, 0.0); final DoubleProperty hue = new SimpleDoubleProperty(); hue.addListener(new InvalidationListener() { public void invalidated(Observable observable) { render(gc, hue.get()); } }); Scene scene = new Scene(new Group(c)); scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() { public void handle(MouseEvent event) { if (gc.getTextBaseline() != null) { System.out.println("setting to null"); gc.setTextAlign(null); } else { System.out.println("setting to LEFT"); gc.setTextAlign(TextAlignment.LEFT); } } }); stage.setScene(scene); stage.show(); KeyValue kv = new KeyValue(hue, 360.0); KeyFrame kf = new KeyFrame(Duration.seconds(5), kv); Timeline tl = new Timeline(kf); tl.setCycleCount(Timeline.INDEFINITE); tl.play(); } }