0

Been at this for a pretty long time, been trying all types of alternatives, so I've come here for help. I need 16 squares to fill an 800x800 pixel area. I have completed that task. However, now that I want each square to have a circle centered ontop of it, I thought of making an array of StackPanes. Each stackPane in the array will hold the square, then the circle (so that circle is on top of this square). This is what I've got.

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Circle;

/**
 * @author Dominic Barbuto
 * @date 03/28/2020
 *
 */
public class DAB_ShapeGrid extends Application
{
    //Scene dimensions
    final int SCENE_WIDTH = 1000, SCENE_HEIGHT = 800;
    //Square Dimensions
    double width = 200, height = 200;

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

    public void start(Stage primaryStage)
    {
        StackPane[] stackPane = new StackPane[16];
        DAB_Square[] squareArray= new DAB_Square[16];

        int index = 0;
        for(int posX = 0; posX < SCENE_WIDTH-200; posX += 200)
        {
            for(int posY = 0; posY < SCENE_HEIGHT; posY += 200)
            {
                squareArray[index] = new DAB_Square(posX, posY, width, height);
                stackPane[index].getChildren().addAll(squareArray[index], new DAB_Circle(posX+100, posY +100, 100.0));
                index++;
            }
        }

        Pane pane = new Pane(stackPane[0], stackPane[1], stackPane[2], stackPane[3],
                                        stackPane[4], stackPane[5], stackPane[6], stackPane[7],
                                        stackPane[8], stackPane[9], stackPane[10], stackPane[11],
                                        stackPane[12], stackPane[13], stackPane[14], stackPane[15]);

        RadioButton radioButton = new RadioButton("Test");
        VBox rightVBox = new VBox(radioButton);

        BorderPane borderPane = new BorderPane();
        borderPane.setLeft(pane);
        borderPane.setRight(rightVBox);

        Scene scene = new Scene(borderPane, SCENE_WIDTH, SCENE_HEIGHT);

        primaryStage.setScene(scene);
        primaryStage.setTitle("Filling the Grid With Shapes Program");
        primaryStage.show();
    }
}

I am getting a NullPointerException pointing to this line: stackPane[index].getChildren().addAll(squareArray[index], new DAB_Circle(posX+100, posY +100, 100.0)); and I can't for the life of me find out why.

1 Answers1

0

Change

StackPane[] stackPane = new StackPane[16];

to

StackPane[] stackPane = new StackPane[16];
for(int i = 0; i < stackPane.length; i++)
{
    StackPane tempStackPane = new StackPane();
    stackPane[i] = tempStackPane;    
}

You never added any StackPanes to the array of StackPanes.

Side Note: I would recommend using a List over Array in this case.

Sedrick
  • 10,209
  • 3
  • 35
  • 49
  • @fabian thanks for pointing that out. I really haven't use arrays much lately and it shows. lol – Sedrick Mar 31 '20 at 06:53