Search code examples
javajavafxmethodsconstructorinner-classes

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


I'm very new to Java. I've been using it for the last two months. The end goal of my code is to try and create a shape and add it to the window without having to set all the properties of the shape in the main "view" class.

When I attempt to add my work in progress Castle class, Intellij is telling me I need to provide a node instead. Below I will provide the class where I'm setting the properties of the shape and the main class where I get halted trying to add it to my 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);
    }
}

Solution

  • You can only add Nodes to the scene graph, and your Castle class is not a node.

    Your draw() method looks strange. It creates a Rectangle (which is a Node), but doesn't do anything with it. It also takes a GraphicsContext2D as a parameter, which looks like you're going to draw on a canvas, but you don't have a canvas.

    You just need to organize the code so that, one way or another, your Castle class provides some kind of Node.

    One way is to refactor your draw method as

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

    and then replace

        root.getChildren().add(castle);
    

    with

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

    Note that with this setup, each time you call draw() on a single Castle instance will create a new Node, which may or may not be what you want, but it is a very simple task to refactor it if it's not what you want.