无法从 fxml 获取图形上下文 canvas
Unable to get graphics context from fxml canvas
我创建了一个带有网格的基本 FXML 布局,canvas 并排放置。我可以访问 canvas 对象 (drawArea),但是在调用 getGraphicsContext 之后,我的 gt 变量始终为 null。
控制器类
public class controls {
private GraphicsContext gc;
@FXML private Canvas drawArea;
public void start() {
gc = drawArea.getGraphicsContext2D();
}
@FXML protected void Step(ActionEvent event) {
//TODO code to update positions of boids.
System.out.print("Step triggered ");
gc.fillOval(100, 100, 10, 10);
}
}
FXML 显示
<SplitPane dividerPositions="0.29797979797979796"
maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.controls" >
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<GridPane hgap="15.0" prefHeight="90.0" prefWidth="175.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Flock Size:" />
<Button id="buttonStart" mnemonicParsing="false" text="Start" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
<Button id="buttonStep" mnemonicParsing="false" onAction="#flockStep" text="Step" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
<Button mnemonicParsing="false" text="-" GridPane.columnIndex="1" GridPane.halignment="LEFT" />
<Button mnemonicParsing="false" text="+" GridPane.columnIndex="1" GridPane.halignment="RIGHT" />
<Button id="buttonStop" mnemonicParsing="false" text="Stop" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
</children>
</GridPane>
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Canvas fx:id="drawArea" height="200.0" width="200.0" />
</children></AnchorPane>
在控制器中,start()
永远不会被调用。如果您定义了一个 initialize()
方法(或者只是将 start()
更改为 initialize()
),该方法将由 FXMLLoader
.
自动调用
我创建了一个带有网格的基本 FXML 布局,canvas 并排放置。我可以访问 canvas 对象 (drawArea),但是在调用 getGraphicsContext 之后,我的 gt 变量始终为 null。
控制器类
public class controls {
private GraphicsContext gc;
@FXML private Canvas drawArea;
public void start() {
gc = drawArea.getGraphicsContext2D();
}
@FXML protected void Step(ActionEvent event) {
//TODO code to update positions of boids.
System.out.print("Step triggered ");
gc.fillOval(100, 100, 10, 10);
}
}
FXML 显示
<SplitPane dividerPositions="0.29797979797979796"
maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.controls" >
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<GridPane hgap="15.0" prefHeight="90.0" prefWidth="175.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Flock Size:" />
<Button id="buttonStart" mnemonicParsing="false" text="Start" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
<Button id="buttonStep" mnemonicParsing="false" onAction="#flockStep" text="Step" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
<Button mnemonicParsing="false" text="-" GridPane.columnIndex="1" GridPane.halignment="LEFT" />
<Button mnemonicParsing="false" text="+" GridPane.columnIndex="1" GridPane.halignment="RIGHT" />
<Button id="buttonStop" mnemonicParsing="false" text="Stop" GridPane.halignment="CENTER" GridPane.rowIndex="2" />
</children>
</GridPane>
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Canvas fx:id="drawArea" height="200.0" width="200.0" />
</children></AnchorPane>
在控制器中,start()
永远不会被调用。如果您定义了一个 initialize()
方法(或者只是将 start()
更改为 initialize()
),该方法将由 FXMLLoader
.