This is the JavaFX program which suppose to show player cards but it shows a blank window. I tried to clean and build but nothing has happened. Also, there no error showing as well. I checked The image path and it's totally correct. In that case, I wonder why it didn't show the cards in the window. There is no other classes other than this one.
Images are named as 1.png, 2.png, 3.png... until 54.png
https://www.dropbox.com/sh/bzl2jya6plldvc0/AADMbPhYalS7sgpeDZdoRKlWa?dl=0
import java.util.ArrayList;
import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author Ramitha
*/
public class JavaFXAssignment extends Application{
//Creating class JavaFXCardGUI which is extending Application class
//Now JavaFXCardGUI class will act as an JavaFX Application
//Overriding start method of Application class which is always a starting point of any JavaFX Application
@Override
public void start(Stage primaryStage) throws Exception {
//Creating a VBox object
VBox vBox=new VBox();
//Setting its spacing and padding
vBox.setSpacing(50);
vBox.setPadding(new Insets(20, 40, 40, 60));
//Creating a HBox object which will store the first row cards and setting its spacing property
HBox firstRowHBox=new HBox();
firstRowHBox.setSpacing(50);
//Creating a HBox object which will store the second row cards and setting its spacing property
HBox secondRowHBox=new HBox();
secondRowHBox.setSpacing(50);
//Creating a HBox object which will store the third row cards and setting its spacing property
HBox thirdRowHBox=new HBox();
thirdRowHBox.setSpacing(50);
//Creating a arraylist of unique 52 cards which will store the each card number
//If that card number will be already present in the arraylist then that card will not be added
ArrayList<Integer> lstUnique52Cards=new ArrayList<Integer>();
//Creating a random class object
Random random=new Random();
//This loop will add the three cards in first row
while(lstUnique52Cards.size()<3) {
//Generate a randomNumber between 1 and 52 both inclusive
//Since random.NextInt(52) will return number from 0 to 51 Hence adding 1 to
//make it from 1 to 52
int randomNumber=random.nextInt(52)+1;
//If the current random generated is not present in the array list lstUnique52Cards
//it means that the random number is unique then add that random number in array list lstUnique52Cards
//then create the correpsonding ImageView with the card number represented by randomNumber
//Then add the image to the firstRowHBox
if(!lstUnique52Cards.contains(randomNumber)) {
lstUnique52Cards.add(randomNumber);
ImageView imgViewCard=new ImageView(new Image("file:C:\\Users\\JavaFXCards\\card"+randomNumber+".png"));
firstRowHBox.getChildren().add(imgViewCard);
}
}
//This loop will add the three joker cards in second row
//Here the uniqueness doesn't matter for the card
for(int i=1;i<=3;i++) {
//Below statement random.nextInt(2) will generate two random numbers 0 and 1 only as we are having
//only two joker cards. Since two joker cards images number are 53.png and 54.png.
//Hence adding 53 to get that correpsonding joker card number
int randomNumber=random.nextInt(2)+53;
//Creating ImageView object with the given card number image
ImageView imgViewCard=new ImageView(new Image("file:C:\\Users\\JavaFXCards\\card"+randomNumber+".png"));
//If the value of i is 1 i.e. it is first card then set the rotation angle as 45
if(i==1)
imgViewCard.setRotate(45);
//If the value of i is 2 i.e. it is second card then set the rotation angle as 90
if(i==2)
imgViewCard.setRotate(90);
//If the value of i is 3 i.e. it is third card then set the rotation angle as 135
if(i==3)
imgViewCard.setRotate(135);
//Add the imgViewCard in secondRowHBox
secondRowHBox.getChildren().add(imgViewCard);
}
//This loop will add the three more unique cards
//Initializing cardNumber with 1
int cardNumber=1;
while(lstUnique52Cards.size()<6) {
//Generate a randomNumber between 1 and 52 both inclusive
//Since random.NextInt(52) will return number from 0 to 51 Hence adding 1 to
//make it from 1 to 52
int randomNumber=random.nextInt(52)+1;
//If the current random generated is not present in the array list lstUnique52Cards
//it means that the random number is unique then add that random number in array list lstUnique52Cards
//then create the correpsonding ImageView with the card number represented by randomNumber
//Also set the correpsonding angle to the cardNumber of this thirdRowHBox
//Then add the image view card to the thirdRowHBox
if(!lstUnique52Cards.contains(randomNumber)) {
lstUnique52Cards.add(randomNumber);
ImageView imgViewCard=new ImageView(new Image("file:C:\\Users\\JavaFXCards\\card"+randomNumber+".png"));
if(cardNumber==1)
imgViewCard.setRotate(135);
if(cardNumber==2)
imgViewCard.setRotate(90);
if(cardNumber==3)
imgViewCard.setRotate(45);
thirdRowHBox.getChildren().add(imgViewCard);
cardNumber++; //Increment cardNumber by 1
}
}
//Add all the three HBox objects in the vBox
vBox.getChildren().add(firstRowHBox);
vBox.getChildren().add(secondRowHBox);
vBox.getChildren().add(thirdRowHBox);
//Create a scene object with the child as vBox
Scene scene = new Scene(vBox ,450, 450);
//Setting the primaryStage title as Cards
primaryStage.setTitle("Cards");
//Setting the scene to the primaryStage
primaryStage.setScene(scene);
//Showing the primaryStage
primaryStage.show();
}
//Main method it will invoke start method of Application class
public static void main(String[] args) {
launch(args);
}
}