user2999473
user2999473

Reputation: 33

javafx: build your own rectangle

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.

Upvotes: 3

Views: 1180

Answers (2)

denhackl
denhackl

Reputation: 1115

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.

Upvotes: 2

tomsontom
tomsontom

Reputation: 5897

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

Upvotes: 0

Related Questions