FoldFence
FoldFence

Reputation: 2802

How to set a picture on a Gridpane

How can i set a Picture to the centre of a gridPane at the moment I generate first a random Labyrinth and set on top of the Labyrinth a Circle Shape

here is the generation Code:

    //add the pictures to the gridpane
    for (int j = 2; j < 7; j++)
    {
        for (int k = 2; k < 7; k++)
        {
            int randomNumber = new Random().nextInt(21);
            imageField = new HBox();
            Image im =new Image(zahlenplaettchenName.get(randomNumber));
            imageField.getChildren().add(new ImageView(new Image(zahlenplaettchenName.get(randomNumber),30,30,false,false)));
            if(gridpane != null){
                gridpane.add(imageField, j, k);
            }
        }
    }

}


//add the pictures to the pane 
private void fillPictureList(){
    pictureName.add("/images/Eck-Kreuzung-links-oben.PNG");
    pictureName.add("/images/Eck-Kreuzung-links-unten.PNG");
    pictureName.add("/images/Eck-Kreuzung-rechts-oben.PNG");
    pictureName.add("/images/Eck-Kreuzung-rechts-unten.PNG");
    pictureName.add("/images/Gerade-horizontal.PNG");
    pictureName.add("/images/T-Kreuzung-oben.PNG");
    pictureName.add("/images/T-Kreuzung-rechts.png");
    pictureName.add("/images/T-Kreuzung-unten.PNG");
    pictureName.add("/images/Gerade-vertikal.PNG");
    pictureName.add("/images/T-Kreuzung-links.PNG");

    for (int j = 1; j < 8; j++)
    {
        for (int k = 1; k < 8; k++)
        {
            int randomNumber = new Random().nextInt(10);
            imageField = new HBox();
            imageField.getChildren().add(new ImageView(new Image(pictureName.get(randomNumber))));
            if(gridpane != null){
                        gridpane.add(imageField, j, k);
            }
        }
    }
}

After this the Picture looks like this:

enter image description here

How can I centre the circleShapes ?

Upvotes: 0

Views: 1510

Answers (2)

Texas
Texas

Reputation: 589

You should try this to center your elements in each cell.

imageField.setAlignment(Pos.CENTER);

Upvotes: 0

Steven Van Impe
Steven Van Impe

Reputation: 1173

You can set the individual alignments of the nodes within their cells using GridPane.setHalignment(circle, HPos.CENTER) and GridPane.setValignment(circle, VPos.CENTER).

You could also do it on a row or column basis using row and column constraints.

Upvotes: 1

Related Questions