Hier ist eine kleine beispielhafte Minianwendung mit einem Canvas, welches sich automatisch der Fenstergröße anpasst:
↔ 
Das größenveränderliche Canvas:
package com.sowas.javawiki.javafx;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class ResizeableCanvas extends Canvas {
public ResizeableCanvas() {
widthProperty().addListener(e -> draw());
heightProperty().addListener(e -> draw());
}
@Override
public boolean isResizable() {
return true;
}
@Override
public double prefWidth(double height) {
return getWidth();
}
@Override
public double prefHeight(double width) {
return getHeight();
}
public void draw() {
double w = getWidth();
double h = getHeight();
GraphicsContext gc = getGraphicsContext2D();
gc.setFill(Color.RED);
gc.fillRect(0, 0, w, h);
gc.setFill(Color.YELLOW);
gc.fillRect(w/4, h/4, w/2, h/2);
}
}
Eine Minianwendung, welche das resizeable Canvas verwendet:
package com.sowas.javawiki.javafx; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.stage.Stage; public class MyFxApplication extends Application { @Override public void start(Stage primaryStage) { primaryStage.setTitle("JavaFX-Resizeable-Canvas"); Pane pane = new Pane(); ResizeableCanvas canvas = new ResizeableCanvas(); // Größenänderungen ans Canvas binden: canvas.widthProperty().bind(pane.widthProperty()); canvas.heightProperty().bind(pane.heightProperty()); pane.getChildren().add(canvas); Scene scene = new Scene(pane); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
Stichworte:
JavaFX Canvas mit automatischer Größenanpassung, resizeable canvas, Beispielanwendung, Example