Question

i'm newbie in java/javafx. anyway everything is fine and working when the talk is about standard features. today i tried to create my own rectangle with specific behavior and was failed.

code is very simple:

package sample;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main2 extends Application {

    //@Override
    public void start(Stage primaryStage) throws Exception {
        Group root = new Group();
        Rectangle myRest1 = new Rectangle(0, 0, 12, 12);
        myRest rect2 = new myRest();

        HBox hBox = new HBox();
        final VBox vBox = new VBox();

        vBox.setPrefSize(500, 500);
        vBox.setStyle("-fx-background-color: yellowgreen");

        vBox.getChildren().add(hBox);
        vBox.getChildren().add(myRest1);
        vBox.getChildren().add(rect2);

        primaryStage.setTitle("Rectangles");
        root.getChildren().add(vBox);
        primaryStage.setScene(new Scene(root, 500, 500));
        primaryStage.show();
    }

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

class myRest extends Rectangle {

    Rectangle rect = new Rectangle(0, 0, 100, 100);
}

why the standard rectangle working fine, but my own (myRest) doesn't work at all. i'm sorry for such a stupid question, but i really don't know why so.

have a nice day and thank you for helping beforehand.

Was it helpful?

Solution

myRest rect2 = new myRest();

This line calls the default constructor of myRest. Because you have not specified a constructor in your own class, it calls up the default constructor of Rectangle. Override this constructor in your class like this

class myRest extends Rectangle {

   public myRest()
   {
       super(100,100); //for fixed dimensions
   }

}

Your line Rectangle rect = new Rectangle(0, 0, 100, 100); creates an rectangle as an attribute of myRest.

OTHER TIPS

You are not setting an dimensions the inner rectangle you create in my myRest is no put any where!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top