Delpux
Delpux

Reputation: 137

JavaFX no image - but ImageView is working

Image images[];
 ImageView imageViews[];
int count = 0;


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

 public void diceImages() {

        images = new Image[6];          

        images[0] = new Image("file:d1.png");

        images[1] = new Image("file:d2.png");

        images[2] = new Image("file:d3.png");

        images[3] = new Image("file:d4.png");

        images[4] = new Image("file:d5.png");

        images[5] = new Image("file:d6.png");
    }
  public void roll() {

        Random random = new Random();

        for (int i = 0; i < imageViews.length; i++) {  

            int index = random.nextInt(6);         
            imageViews[i].setImage(images[index]);
        }

    }

@Override
public void start(Stage primaryStage) throws Exception {        
     diceImages();//read images
     imageViews = new ImageView[5];

     for (int i = 0; i < imageViews.length; i++) {
            imageViews[i] = new ImageView();

            imageViews[i].setFitHeight(150);
            imageViews[i].setFitWidth(150);
        }
    Image test = new Image("File: C:\\Users\\halli\\eclipse-workspace\\1DV507\\src\\d1.png"); 
    ImageView test1 = new ImageView(test);
    test1.setFitHeight(150);
    test1.setFitWidth(150);

    VBox root = new VBox();

    GridPane layout = new GridPane();
    layout.setAlignment(Pos.CENTER);
    layout.setHgap(1);
    layout.setVgap(5);

    VBox titleText = new VBox (0);
    Text title = new Text("Yahtzee");       
    title.setFont(Font.font ("Verdana",30));
    titleText.getChildren().add(title);
    titleText.setAlignment(Pos.CENTER_LEFT);
    titleText.setPadding(new Insets(10,0,0,0));

    HBox dices1 = new HBox();       
    dices1.setAlignment(Pos.CENTER);
    dices1.setSpacing(10);  
    dices1.getChildren().add(test1);
    roll();


    VBox button = new VBox (0);
    HBox checkboxes = new HBox();
    CheckBox d1 = new CheckBox();
    CheckBox d2 = new CheckBox();
    CheckBox d3 = new CheckBox();
    CheckBox d4 = new CheckBox();
    CheckBox d5 = new CheckBox();
    checkboxes.setSpacing(60);
    checkboxes.setAlignment(Pos.CENTER);
    checkboxes.setPadding(new Insets(5,5,10,5));
    checkboxes.getChildren().addAll(d1,d2,d3,d4,d5);        
    button.setAlignment(Pos.CENTER);


    Button theButton = new Button("Roll the dice!");
    final Label result1 = new Label();
    final StringBuilder result = new StringBuilder();               
    result1.setPadding(new Insets(5,5,5,5));
    button.getChildren().addAll(checkboxes, theButton, result1);        


    theButton.setOnAction(e -> {

    });     


    layout.add(titleText, 10, 5);
    layout.add(dices1, 10, 8);
    layout.add(button, 10,10);

    root.getChildren().addAll(layout);
    Scene scene= new Scene(root,450,250);
    primaryStage.setTitle("Yahtzee");
    primaryStage.setScene(scene);
    primaryStage.show();
}

}

I am working on a yahtzee game, part of it, and I am getting very confused why my pictures won't appear. I tried this with java11 and now java13, both on linux and windows and I always get the same result.

When I run it, the ImageView is working, because it pushes the title and button away - but the image its self does not appear. I have the images, .png, in the src folder, have tried different type of images and both File: .png and File: location.png. I have no idea what is going on. In code above I tried a test image, but it also does not work when I use the methods and add them to HBox dices1

Upvotes: 0

Views: 130

Answers (1)

Paweł
Paweł

Reputation: 212

I have added maven support to JavaFX project and below is working example:

Image image = new Image("images/logomedium.png")
ImageView logoView =  new ImageView();
logoView.setImage(image);
container.getChildren().add(logoView);

with following catalog structure:

enter image description here

Upvotes: 2

Related Questions