JavaFX Scene always empty; displaying empty stage

d0rf47 :

I am Working on a JavaFX project to display a solution to the 8 Queens problem. I have run into a strange issue wherein my scene is always rendering empty and I cannot seem to resolve the issue.

I have tried rendering other simple things in the project & they display properly which suggests to me that something is wrong with the way i am adding my labels to the GridPane. But I copied this solution directly from a textbook so it should work I cannot grasp why nothing is showing. The title is set and is visible but where the gridpane should be is just empty.

package sample;

import com.sun.glass.ui.Size;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;



public class Main extends Application {
    public static final int SIZE = 8;
    private int [] queens = { -1,-1,-1,-1,-1,-1,-1,-1};


    @Override
    public void start(Stage primaryStage) throws Exception{

        search();// look for a solution

        GridPane chessBoard = new GridPane();
        chessBoard.setAlignment(Pos.CENTER);
        Label[][] labels = new Label[SIZE][SIZE];

        for(int i = 0; i < SIZE; i++)
        {
            for(int j = 0; j < SIZE; j++)
            {
                chessBoard.add(labels[i][j] = new Label(), j, i);
                labels[i][j].setStyle("−fx−border−color: black");
                labels[i][j].setPrefSize(55,55);
            }
        }
        Image img = new Image("queen.png");

        for(int i = 0; i < SIZE; i++)
        {
            labels[i][queens[i]].setGraphic(new ImageView(img));
        }

        Scene scenes = new Scene(chessBoard, 55 * SIZE, 55 * SIZE);
        primaryStage.setTitle("Eight Queens");
        primaryStage.setScene(scenes);
        primaryStage.show();
    }
    private boolean search()
    {
        int k = 0;
        while( k >= 0 && k < SIZE)
        {
            int j = findPosition(k);
            if(j < 0)
            {
                queens[k] = -1;
                k--;
            }else
            {
                queens[k]=j;
                k++;
            }
        }
        if(k == -1)
            return false;
        else
            return true;
    }

    public int findPosition(int k)
    {
        int start = queens[k]+1;

        for(int j = start; j < SIZE; j++)
        {
            if(isValid(k,j))
                return  j;
        }
        return -1;
    }
    public boolean isValid(int row, int col)
    {
        for (int i = 1; i <= row; i++)
        {
            if( queens[row - i] == col ||
                queens[row - i] == col - i ||
                queens[row - i] ==  col + i
            )
            {
                return false;
            }
        }
        return true;
    }


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

And Yes I have looked at the other topics with similar titles, but those issues stem from a difference cause.

Any insight you can offer is appricicated!

lowzhao :

I tested your code with different sizes of images.

width and height at 64px are able to display properly as you increase to 256px though no image can be shown

Directly adding ImageView into the GridPane can solve your problem, you might need to find a way to draw the border.

for(int i = 0; i < SIZE; i++)
{
    for(int j = 0; j < SIZE; j++)
    {
        chessBoard.add(new ImageView(isqueen(i,j)?queenimg:img), i, j);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=344835&siteId=1