如何在JavaFX中向BorderPane添加画布?

时间:2017-01-29 16:09:34

标签: java canvas

在我的应用程序中,我有一个BorderPane,它作为我的GUI的布局。在中心窗格中,我想添加一个画布,我可以在其上绘制形状。在我当前的代码中,我收到错误说"没有找到适合添加的方法(Canvas)。方法collection.add(Node)不适用。"我做错了什么?

package MyGame;

import java.awt.Canvas;
import javafx.scene.paint.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.stage.Stage;

public class MyGame extends Application {

    public Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    public double windowWidth = screenSize.getWidth() * .75;
    public double windowHeight = screenSize.getHeight() * .75;

    @Override
    public void start(Stage primaryStage) {

        BorderPane root = new BorderPane();

        Button regenerate = new Button("Regenerate Board");
        regenerate.setPrefWidth(windowWidth * .15 * .80);
        regenerate.getStyleClass().add("spButton");

        Button settings = new Button("Game Settings");
        settings.setPrefWidth(windowWidth * .15 * .80);
        settings.getStyleClass().add("spButton");

        Button start = new Button("Start Game");
        start.setPrefWidth(windowWidth * .15 * .80);
        start.getStyleClass().add("spButton");

        VBox bpLeft = new VBox(20);
        bpLeft.setPrefWidth(windowWidth * .15);
        //bpLeft.resize(((screenSize.getWidth() * .75) *.20), screenSize.getHeight() * .75);
        bpLeft.getStyleClass().add("bpLeft");
        bpLeft.getChildren().addAll(regenerate, settings, start);

        root.setLeft(bpLeft);

        double originX = (windowWidth * .15) + ((windowWidth * .85) / 2);
        double originY = (windowHeight / 2);

        Pane wrapperPane = new Pane();
        root.setCenter(wrapperPane);
        // Put canvas in the center of the window
        Canvas canvas = new Canvas();
        canvas.setBounds((int)originX, (int)originY, (int)Math.round(windowWidth * .85), (int)Math.round(windowHeight));
        wrapperPane.getChildren().add(canvas);

        //root.getChildren().add(btn);
        Scene scene = new Scene(root, windowWidth, windowHeight);

        scene.getStylesheets().add(HelloWorld.class.getResource("Catan.css").toExternalForm());
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
        }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

1 个答案:

答案 0 :(得分:0)

您意外导入了java.awt.Canvas而不是javaFX画布。