Sherrie Tang
Sherrie Tang

Reputation: 3

How do I create a class (e.g Triangle) that extends Polygon in JavaFX?

I keep getting errors, or just a blank scene. Below is the compiling code that shows nothing. I think the problem is in my class Triangle, but I have a limited understanding of JavaFX and extends classes.

public class Board extends Application {

@Override
public void start(Stage stage){
    stage.setTitle("Board");
    Group root = new Group();
    Scene scene = new Scene(root, 600, 519);
    stage.setScene(scene);

    Triangle t1 = new Triangle(300,259.5,200);
    t1.draw();
    t1.setFill(Color.LIGHTGRAY);
    root.getChildren().add(t1);
    stage.show();
}

class Triangle extends Polygon {
    double x;
    double y;
    double side;

    Triangle(double x, double y, double side){
        this.x = x;
        this.y = y;
        this.side = side;
    }

    Triangle draw(){
        Triangle triangle = new Triangle(x,y,side);
        triangle.setLayoutX(x);
        triangle.setLayoutY(y);
        triangle.getPoints().addAll(
                x, (-(Math.sqrt((side*side)-((side/2)*(side/2)))/2)),
                (side/2), Math.sqrt((side*side)-((side/2)*(side/2)))/2,
                (-(side/2)), Math.sqrt((side*side)-((side/2)*(side/2)))/2);
        return triangle;
    }
}

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

Upvotes: 0

Views: 2763

Answers (2)

fabian
fabian

Reputation: 82461

Your draw method sets the values on a new object that you ignore in your code.

If you extend the class and only pass the values to the constructor, this functionality should be done in the constructor and the changes should be done on the object isself, not on some other object:

class Triangle extends Polygon {
    double x;
    double y;
    double side;

    Triangle(double x, double y, double side){
        this.x = x;
        this.y = y;
        this.side = side;

        setLayoutX(x);
        setLayoutY(y);
        getPoints().addAll(
                x, (-(Math.sqrt((side*side)-((side/2)*(side/2)))/2)),
                (side/2), Math.sqrt((side*side)-((side/2)*(side/2)))/2,
                (-(side/2)), Math.sqrt((side*side)-((side/2)*(side/2)))/2);
    }
}

However since you do not really add functionality to the Polygon class, it would be better to simply create a Polygon representing a triangle instead of introducing a new class for this:

public static Polygon createTriangle(double x, double y, double side) {
    Polygon triangle = new Polygon();
    triangle.setLayoutX(x);
    triangle.setLayoutY(y);
    triangle.getPoints().addAll(
            x, (-(Math.sqrt((side*side)-((side/2)*(side/2)))/2)),
            (side/2), Math.sqrt((side*side)-((side/2)*(side/2)))/2,
            (-(side/2)), Math.sqrt((side*side)-((side/2)*(side/2)))/2);
    return triangle;
}
Polygon t1 = createTriangle(300,259.5,200);

Upvotes: 2

Uwe
Uwe

Reputation: 844

The following code should work:

public class Board extends Application {

@Override
public void start(Stage stage) {
    stage.setTitle("Board");
    Group root = new Group();
    Scene scene = new Scene(root, 600, 519);
    stage.setScene(scene);

    Triangle t1 = new Triangle(300, 259.5, 200);
    t1.draw();
    t1.setFill(Color.LIGHTGRAY);
    root.getChildren().add(t1);
    stage.show();
}

class Triangle extends Polygon {
    double x;
    double y;
    double side;

    Triangle(double x, double y, double side) {
        this.x = x;
        this.y = y;
        this.side = side;
    }

    void draw() {

        setLayoutX(x);
        setLayoutY(y);
        getPoints().addAll(
                x, -(Math.sqrt(side * side - side / 2 * (side / 2)) / 2),
                side / 2, Math.sqrt(side * side - side / 2 * (side / 2)) / 2,
                -(side / 2), Math.sqrt(side * side - side / 2 * (side / 2)) / 2);

    }
}

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

The problem with your original code was that you created a second Triangle instance within the draw() method of the Triangle object you wanted to show in the scene. So the Triangle created in your application's start() method was never layed out.

Upvotes: 1

Related Questions