绘制后Javafx FXML帆布空

 2023-02-16    282  

问题描述

我正在使用Intellij Idea进行JAVAFX FXML开发.我使用以下代码简单地绘制矩形.但是,它从未出现过.

main.java

绘制后Javafx FXML帆布空

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);
    }
}

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 to black");
        gc.fillRect(50, 50, 100, 100);
        System.out.println("draw rectangle");
    }
}

我在按钮onaction和初始化方法中都使用了setFill()和fillRect().但这仍然没有绘制矩形.

sample.fxml

<BorderPane 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="sample.Controller">
   <top>
      <ToolBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <items>
          <Button mnemonicParsing="false" onAction="#drawCanvas" text="draw" />
        </items>
      </ToolBar>
   </top>
   <bottom>
      <AnchorPane prefHeight="41.0" prefWidth="600.0" BorderPane.alignment="CENTER">
         <children>
            <Label fx:id="co" layoutX="14.0" layoutY="9.0" text="co" />
            <Label fx:id="rgb" layoutX="144.0" layoutY="13.0" text="RGB" />
            <Label fx:id="zoom" layoutX="245.0" layoutY="9.0" text="zoom" />
         </children>
      </AnchorPane>
   </bottom>
   <center>
      <ScrollPane fx:id="img_pane" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <content>
            <Canvas fx:id="img" height="286.0" width="355.0" />
         </content>
      </ScrollPane>
   </center>
</BorderPane>

推荐答案

在这个论坛上有几个类似的问题,但是我无法快速搜索.

您应该 永远不要 初始化注释@FXML的字段. @FXML的全部要点是,该字段是在加载FXML文件的过程中初始化的.

因此,在您的类初始化器中,您可以创建Canvas,然后将gc分配给其图形上下文:

@FXML public Canvas img = new Canvas(300,300);
public GraphicsContext gc = img.getGraphicsContext2D();

然后,当您加载fxml文件时,FXMLLoader如FXML所述创建一个新的Canvas,将新画布分配给字段img,然后将新画布放在场景图中(哪个画布(然后您在舞台上显示).但是,gc仍然是原始Canvas的图形上下文,您从未显示.因此,在gc上的任何图形操作都将永远无法实现.

您需要

public class Controller implements Initializable {

    @FXML private Canvas img ;

    private GraphicsContext gc ;

    @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 = img.getGraphicsContext2D();
        gc.setFill(Color.BLACK);
        System.out.println("color set to black");
        gc.fillRect(50, 50, 100, 100);
        System.out.println("draw rectangle");
    }

}

以上所述是小编给大家介绍的绘制后Javafx FXML帆布空,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/33850.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。