Elif
Elif

Reputation: 355

Can't show an instance of an inherited Node class

public void start(Stage primaryStage) {
    Figure figure = new Figure(50,50,100,100,400,400);
    GridPane pane = new GridPane();
    pane.getChildren().addAll(figure);

    Scene scene = new Scene(pane,500,700);

    primaryStage.setTitle("Main");
    primaryStage.setScene(scene);
    primaryStage.show();
}


public class Figure extends Node {
private Circle circle ;
private Line boldLine ;
private Line thinLine ;
private Line prepenLine ;

public Figure(int centerX, int centerY, int startX, int startY, int endX, int endY){
    Circle circle = new Circle(centerX,centerY,10);
    circle.setStroke(Color.GRAY);
    circle.setFill(Color.DARKGRAY);
   prepenLine = new Line(startX,startY,endX,endY);
}

Hi, I was trying to write a class to use my custom nodes like a combo of line and circle but an error keep occuring in show() statement. I can't find what is wrong with the code. Any advice?

Edit: provided errors to make it more clear

Exception in thread "JavaFX Application Thread" java.lang.UnsupportedOperationException: Applications should not extend the Node class directly.
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.getHelper(NodeHelper.java:78)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.transformsChanged(NodeHelper.java:121)
    at javafx.graphics/javafx.scene.Node.nodeResolvedOrientationInvalidated(Node.java:6540)
    at javafx.graphics/javafx.scene.Node.parentResolvedOrientationInvalidated(Node.java:6517)
 etc.

Upvotes: 1

Views: 1397

Answers (1)

James_D
James_D

Reputation: 209553

According to the documentation:

An application should not extend the Node class directly. Doing so may lead to an UnsupportedOperationException being thrown.

In fact, if I try to run your code, the stack trace explicitly says this too:

Exception in thread "JavaFX Application Thread" java.lang.UnsupportedOperationException: Applications should not extend the Node class directly.
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.getHelper(NodeHelper.java:78)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.transformsChanged(NodeHelper.java:121)
    at javafx.graphics/javafx.scene.Node.nodeResolvedOrientationInvalidated(Node.java:6540)
    at javafx.graphics/javafx.scene.Node.parentResolvedOrientationInvalidated(Node.java:6517)
 etc. ...

Your Figure class really doesn't do anything anyway: it just creates some shapes (Circles and Lines), which are private (so inaccessible). Those shapes are never actually displayed by your Node class. There is no public API that allows you to determine how a node is painted.

You should probably subclass Region (or perhaps Pane), and add the shapes as child nodes:

public class Figure extends Region {

    private Circle circle ;
    private Line boldLine ;
    private Line thinLine ;
    private Line prepenLine ;

    public Figure(int centerX, int centerY, int startX, int startY, int endX, int endY){
        circle = new Circle(centerX,centerY,10);
        circle.setStroke(Color.GRAY);
        circle.setFill(Color.DARKGRAY);
        prepenLine = new Line(startX,startY,endX,endY);
        getChildren().addAll(circle, prepenLine);
    }

}

Upvotes: 3

Related Questions