1

This should be a relatively simple problem, but it is driving me insane. I am trying to create Mine Sweeper in JavaFX (mostly just for practice) but I can not get even a simple rectangle to display. I had the game running once before, but I am trying to make the game more abstract, and hence easier to code, but I am running into the issue of nothing being displayed.

I eliminated all extraneous code so it is as simple as possible. I am basically trying to create a Rectangle with a certain color and size called Box, add the box to the pane, and display the pane. In order to make Box a node that can be displayed on the pane, I made the Box class extend Rectangle, so that a Box would have the same properties as a Rectangle. But when I run the code, it gives just an empty pane with no box in it.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Minesweeper extends Application {

    @Override
    public void start(Stage stage) {

        Pane pane = new Pane();

        Box box = new Box();

        pane.getChildren().addAll(box);     

        // Create the scene
        Scene scene = new Scene(pane);
        stage.setTitle("Minesweeper");
        stage.setScene(scene);
        stage.show();

    }

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

}

import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class Box extends Rectangle {

    public Box() {

        Rectangle box = new Rectangle(100, 100, 100, 100);

        box.setFill(Color.BLUE);

    }

}

I realized if I put the code from Box into the main Minesweeper class, it will display the box. But Box will have a ton of other properties and therefore needs to be a class on its own.

What am I doing wrong that does not allow the box to be displayed?

Thanks in advance for your help and consideration.

1
  • You're creating a new Rectangle object in the Box constructor, but not storing it anywhere or doing anything with it. Is there some paint method you should override? Commented May 14, 2016 at 15:14

1 Answer 1

2

You create a new Rectangle in your Box class. This Rectangle is not added to any Parent container, so it's not visible. Change your code to:

    public Box() {
        super(100, 100, 100, 100);
        setFill(Color.BLUE);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

That's perfect. Thank you very much. Makes perfect sense too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.