将画布分层到GridPane

时间:2019-04-05 17:09:41

标签: java javafx

我正在尝试创建一个州与州之间的箭头。 我有一个边框,并在中间设置了状态/箭头。使用网格窗格创建状态并将其放置在中心。但是,在将画布放在网格窗格的顶部(虽然仍在中心)以绘制箭头时遇到了问题。尝试将ContextGraphics传递给arrow类时,它无法编译,表示start方法出错。

错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at sample.State.setupStates(State.java:33)
    at sample.Main.start(Main.java:102)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Exception running application sample.Main

Process finished with exit code 1

箭头类:

public GraphicsContext draw(GraphicsContext gc, int startX, int startY, int endX, int endY)
    {
        double distanceX = endX- startX;
        double distanceY = endY - startY;
        gc.beginPath();
        gc.moveTo(startX, startY + length / 2);
        gc.lineTo(startY + width / 2, startY - length /2);
        gc.lineTo(startY - width / 2, startY - length /2);
        gc.setFill(Color.BLACK);
        gc.closePath();
        gc.strokeLine(startX, startY, endX, endY);

        return gc;
    }

状态类:

public void setupStates()
    {
        //create specific states 
        drawState(count, "Start", true, false, 1, 0);
        sampleArr.draw(main.getGC(), 1, 0, 3, 0);  //Draws arrow here
        drawState(count,"End", false, false, 3, 0);
}

主要:

 public void start(Stage primaryStage) throws Exception
    {
        //canvas is declared before start method
        State stateBuilder = new State();
        graphicsContext = canvas.getGraphicsContext2D();

        //set up view
        primaryStage.setResizable(false);

        //set borderpane
        setTopSide();
        setLeftSide();
        setCenter();

        //create states
        stateBuilder.setupStates();

        addCenterLayers();


        //create scene object
        primaryStage.setScene(new Scene(borderpane, 1800, 900));
        primaryStage.show();

    }

 public void addCenterLayers()
    {
        //layering
        centerPane.getChildren().add(grid);
        centerPane.getChildren().add(canvas);
        canvas.toFront();
        borderpane.setCenter(centerPane);
    }

它完美地产生了网格,并且在不尝试添加画布时运行良好。任何见识将不胜感激

0 个答案:

没有答案