JavaFX:无法创建自定义画布

时间:2019-06-14 14:00:48

标签: java javafx scenebuilder

我正在尝试使用FXML和Scene Builder创建我的第一个JavaFX程序。 因此一切正常,直到我尝试扩展Canvas并创建自定义画布为止。

我的代码:

package View;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;


public class mazeDisplayer extends Canvas {
int[][] mazeData;



public void setMazeData(int[][] mazeData) {
    this.mazeData = mazeData;
}

public void redraw() {

    if(mazeData != null) {
        double W = getWidth();
        double H = getHeight();
        double w = W/mazeData[0].length;
        double h = H/mazeData.length;

        GraphicsContext gc = getGraphicsContext2D();

        for(int i=0;i<mazeData.length;i++)
            for(int j=0;j<mazeData[i].length;j++) {
                if(mazeData[i][j]!=0) {
                    gc.fillRect(w*j,h*i,w,h);
                }
            }

    }
}



}

在我的主视图上,这是我的代码:

public class MainWindowController implements Observer {

ViewModel vm;

@FXML
Pane manualPane,autoPilotPane,homePane;
@FXML
TextArea textArea;
@FXML
TextField ipTextX,portTextY;
@FXML
Circle smallCircle,bigCircle;
@FXML
Slider rudderSlider,throttleSlider;
@FXML
mazeDisplayer mazeDisplayer;

DoubleProperty aileron,elevator;


public MainWindowController() {
    aileron = new SimpleDoubleProperty();
    elevator = new SimpleDoubleProperty();
}


public void setViewModel(ViewModel vm) {
    this.vm = vm;
    vm.ipText.bind(ipTextX.textProperty());
    vm.portText.bind(portTextY.textProperty());
    vm.textAreaAutoPilot.bind(textArea.textProperty());
    vm.throttle.bind(throttleSlider.valueProperty());
    vm.rudder.bind(rudderSlider.valueProperty());
    vm.aileron.bind(aileron);
    vm.elevator.bind(elevator);
}

public void draw() {

    mazeDisplayer.setMazeData(vm.drawCanvas());
    mazeDisplayer.redraw();

}


public void moveJoystick() {
    smallCircle.setOnMouseDragged((MouseEvent e)-> {
        if(e.getX() < (bigCircle.getRadius() - smallCircle.getRadius())
                && e.getX() > -(bigCircle.getRadius() - smallCircle.getRadius())
                && e.getY() < (bigCircle.getRadius() - smallCircle.getRadius())
                && e.getY() > -(bigCircle.getRadius() - smallCircle.getRadius()))
        {
           smallCircle.setCenterX(e.getX());
           smallCircle.setCenterY(e.getY());
           aileron.set(e.getX()/bigCircle.getRadius());
           elevator.set(e.getY()/bigCircle.getRadius());
           vm.moveAileron();
           vm.moveElevator();
        }
    });
    smallCircle.setOnMouseReleased((MouseEvent e)->{
        smallCircle.setCenterX(0);
        smallCircle.setCenterY(0);
    });
}



public void moveThrottleSlider() {

    vm.moveThrottle();

}

public void moveRudderSlider() {
    vm.moveRudder();
}




public void onConnect() {


    vm.connect();

}

public void displayHomePane() {

    autoPilotPane.setVisible(false);
    manualPane.setVisible(false);
    homePane.setVisible(true);


}

public void displayManualPane() {

    autoPilotPane.setVisible(false);
    homePane.setVisible(false);
    manualPane.setVisible(true);


}

public void displayAutoPilotlPane() {


    homePane.setVisible(false);
    manualPane.setVisible(false);
    autoPilotPane.setVisible(true);


}
public void loadScriptDATA() {

    FileChooser fc = new FileChooser();
    fc.setTitle("open CSV file");
    fc.setInitialDirectory(new File("./resources"));
    File chosen = fc.showOpenDialog(null);
    //showText(chosen);

}

public void loadScriptAutoPilot() {

    FileChooser fc = new FileChooser();
    fc.setTitle("open CSV/text file");
    fc.setInitialDirectory(new File("./resources"));
    File chosen = fc.showOpenDialog(null);
    showText(chosen);
   // chosen.renameTo(new File("cmd.txt"));


}

public void onStartAutoPilot() {
    vm.startAutoPilot();
}

    public void showText(File choose) {

            textArea.setWrapText(true);
        try (Scanner s = new Scanner(choose)) {

            while(s.hasNextLine()) {

                textArea.appendText(s.nextLine()+"\n");

            }

        } catch (FileNotFoundException e) {e.printStackTrace();}



    }

/**
 * This method is called whenever the observed object is changed. An
 * application calls an <tt>Observable</tt> object's
 * <code>notifyObservers</code> method to have all the object's
 * observers notified of the change.
 *
 * @param o   the observable object.
 * @param arg an argument passed to the <code>notifyObservers</code>
 */
@Override
public void update(Observable o, Object arg) {

}
}

这使我的程序抛出异常,我在此附加这些异常。 希望有人能知道为什么我不能制作自定义Canvas并正确运行它。

    "C:\Program Files\Java\jdk1.8.0_211\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=53689:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_211\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_211\jre\lib\rt.jar;C:\projectIntel\mile_stone_5-master\out\production\mile_stone_5;C:\projectIntel\mile_stone_5-master\out\production\my_mile_stone_4" View.Main
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$159(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
unknown path

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
    at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
    at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
    at View.Main.start(Main.java:16)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(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$152(WinApplication.java:177)
    ... 1 more
Caused by: java.lang.ClassNotFoundException
    at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2899)
    at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
    ... 14 more
Exception running application View.Main

Process finished with exit code 1

这是我在fxml文件中创建带有ID的自定义Canvas的行:

            <mazeDisplayer fx:id="mazeDisplayer" height="300" width="300" BorderPane.alignment="CENTER" />

主应用程序中的异常:

        Parent root = fxl.load(getClass().getResource("sample.fxml").openStream());

0 个答案:

没有答案