小编Mic*_*iao的帖子

绘图后javafx FXML Canvas为空

我正在使用Intellij IDEA进行javafx FXML开发.我使用以下代码简单地绘制一个矩形.然而,它从未出现过.

Main.java

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

Controller.java

public class Controller implements Initializable {
public Mat image;
@FXML public Canvas img = new Canvas(300,300);
public GraphicsContext gc = img.getGraphicsContext2D();
@FXML private void drawCanvas(ActionEvent event) {
    gc.setFill(Color.AQUA);
    gc.fillRect(10,10,100,100);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
        gc.setFill(Color.BLACK);
        System.out.println("color set …
Run Code Online (Sandbox Code Playgroud)

javafx canvas

3
推荐指数
1
解决办法
6359
查看次数

标签 统计

canvas ×1

javafx ×1