In javaFX to resize a canvas there is no such method to do that, the only solution is to extends from Canvas.
class ResizableCanvas extends Canvas {
pub
this solution work only if we don't want to use FXML
I am not sure about the merits of a resizable canvas itself, neither with/without FXML. Generally you want to redraw something on it, and then you do not have a canvas (which has no content on its own), but you are back to application-specific code, just as like the question iself and most answers around do contain some re/draw()
method.
Then you could throw away the separate class, do four bindings in FXML:
and implement only redraw()
in Java:
package test;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class TestController {
@FXML
private Canvas canvas;
@FXML
private void redraw() {
double w=canvas.getWidth();
double h=canvas.getHeight();
GraphicsContext gc=canvas.getGraphicsContext2D();
gc.clearRect(0, 0, w, h);
gc.beginPath();
gc.rect(10, 10, w-20, h-20);
gc.stroke();
}
}
(If needed, find a suitable main class and module-info in https://stackoverflow.com/a/58915071/7916438)