Atychip Atychip - 4 months ago 17
Java Question

How to call an array of Images

So, I am assigned a problem where I have to display, using JavaFX, 4 random cards and a "Refresh" button that shuffles and displays the cards.

The problem stems when I need to display the cards.
I'm not entirely sure how to show/display the images. I consulted the textbook and I can't figure out another path to solving this problem as well as consulting a classmate.

I have the deck of card images in another source file (peer's suggestion) within the java project.

Here is what I have so far:

package Images;

import java.util.Arrays;
import java.util.Collections;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class CardRefreshButton extends Application{

Image[] deck = new Image[51];

public void add() {
for(int i = 0; i < deck.length; i++){
deck[i] = new Image(i + ".png");
}
}
public void shuffle() {
Collections.shuffle(Arrays.asList(deck));

}

@Override
public void start(Stage primaryStage) {

primaryStage.setTitle("Shuffling Deck Program");

BorderPane bPane1 = new BorderPane();
HBox pane1 = new HBox(10);
HBox pane2 = new HBox(10);

Button refresh = new Button("Refresh");

refresh.setOnAction((event) -> {
add();
shuffle();

pane1.getChildren().clear();
start(primaryStage);
});

pane1.setAlignment(Pos.CENTER);
pane2.setAlignment(Pos.CENTER);

Image card1 = deck[0];
Image card2 = deck[1];
Image card3 = deck[2];
Image card4 = deck[3];
ImageView view1 = new ImageView(card1);
ImageView view2 = new ImageView(card2);
ImageView view3 = new ImageView(card3);
ImageView view4 = new ImageView(card4);

pane1.getChildren().addAll(view1,view2,view3,view4);
pane2.getChildren().add(refresh);

bPane1.setCenter(pane1);
bPane1.setBottom(pane2);


Scene scene1 = new Scene(bPane1, 600,650);
primaryStage.setScene(scene1);
primaryStage.show();


}
}

Answer

Here's a modified version of your code:

  • first you load the images
  • then you create the ImageView nodes and put them in the HBox
  • in your refresh code you shuffle the list and assign the images to the existing ImageView nodes

The code:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class CardRefreshButton extends Application {

    List<Image> deck = new ArrayList<>();

    public void load() {
        for (int i = 0; i < 52; i++) {
            deck.add(new Image(getClass().getResource(i + ".png").toExternalForm()));
        }
    }

    @Override
    public void start(Stage primaryStage) {

        load();

        primaryStage.setTitle("Shuffling Deck Program");

        BorderPane bPane1 = new BorderPane();
        HBox pane1 = new HBox(10);
        HBox pane2 = new HBox(10);

        ImageView view0 = new ImageView(deck.get(0));
        ImageView view1 = new ImageView(deck.get(1));
        ImageView view2 = new ImageView(deck.get(2));
        ImageView view3 = new ImageView(deck.get(3));

        Button refresh = new Button("Refresh");

        refresh.setOnAction((event) -> {

            Collections.shuffle( deck);

            view0.setImage(deck.get(0));
            view1.setImage(deck.get(1));
            view2.setImage(deck.get(2));
            view3.setImage(deck.get(3));

        });

        pane1.setAlignment(Pos.CENTER);
        pane2.setAlignment(Pos.CENTER);

        pane1.getChildren().addAll(view0, view1, view2, view3);
        pane2.getChildren().add(refresh);

        bPane1.setCenter(pane1);
        bPane1.setBottom(pane2);

        Scene scene1 = new Scene(bPane1, 600, 650);
        primaryStage.setScene(scene1);
        primaryStage.show();

    }

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