尝试从另一个 class 添加 JavaFX 形状并在我的主 class 中创建它。 Class 无法转换为节点

Trying to add a JavaFX shape from another class and create it in my main class. The Class cannot be converted to node

我对 Java 很陌生。过去两个月我一直在使用它。我的代码的最终目标是尝试创建一个形状并将其添加到 window,而不必在主“视图”class.

中设置形状的所有属性

当我尝试添加正在进行的工作 Castle class 时,Intellij 告诉我需要提供一个节点。下面我将提供 class 我正在设置形状属性的地方和主要 class 我停止尝试将其添加到我的 window.

的地方
public class Castle {
    private int width = 1280; // width of the window
    private int height = 720; // height of the window
    private double x, y, size;
    private int denizens;
    private Color color;
    private String name;
    private Random rn = new Random();

    // constructor
    Castle() {
        x = 50;
        y = 50;
        size = 10;
        color = Color.GRAY;
        name = "Anthony's Castle";
        denizens = rn.nextInt();
    }
    // GraphicsContext method
    protected void draw(GraphicsContext gc) {
        Rectangle rec = new Rectangle(x, y);
        rec.setX(100);
        rec.setY(100);
        rec.setWidth(100);
        rec.setHeight(100);
        rec.setFill(Color.RED);
    }
    // get methods
    public int getDenizens() {
        return denizens;
    }
    public double getSize() {
        return size;
    }
}
public class TwoDomains extends Application {
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 1280, 720, Color.BEIGE);

        Castle castle = new Castle();

        root.getChildren().add(castle);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Village");
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

也许你想要:

public class Castle extends Group {
    private int width = 1280; // width of the window - does EACH castle need a separate copy of this info?
    private int height = 720; // height of the window
    private double x, y, size;
    private int denizens;
    private Color color;
    private String name;
    private Random rn = new Random();  // This probably should be static, or somewhere else.

    // constructor
    Castle() {
        x = 50;
        y = 50;
        size = 10;
        color = Color.GRAY;
        name = "Anthony's Castle";
        denizens = rn.nextInt();
        makeShape();
    }

    protected void makeShape() {
        Rectangle rec = new Rectangle(x, y);
        rec.setX(100);
        rec.setY(100);
        rec.setWidth(100);
        rec.setHeight(100);
        rec.setFill(Color.RED);
        getChildren().add(rec);
    }
    // get methods
    public int getDenizens() {
        return denizens;
    }
    public double getSize() {
        return size;
    }
}

场景图只能添加Node,你的Castleclass不是节点

您的 draw() 方法看起来很奇怪。它创建了一个 Rectangle(这是一个 Node),但不对其执行任何操作。它还需要一个 GraphicsContext2D 作为参数,看起来你要在 canvas 上绘制,但你没有 canvas.

您只需要组织代码,以便您的 Castle class 以某种方式提供某种 Node.

一种方法是将您的 draw 方法重构为

public Node draw() {
    Rectangle rec = new Rectangle(x, y);
    rec.setX(100);
    rec.setY(100);
    rec.setWidth(100);
    rec.setHeight(100);
    rec.setFill(Color.RED);
    return rec ;
}

然后替换

    root.getChildren().add(castle);

    root.getChildren().add(castle.draw());

请注意,使用此设置,每次您在单个 Castle 实例上调用 draw() 时都会创建一个 new Node,它可能是也可能不是你想要的,但如果它不是你想要的,重构它是一个非常简单的任务。