Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that displays four cards randomly selected from a deck of 52 if

ID: 3918037 • Letter: W

Question

Write a program that displays four cards randomly selected from a deck of 52 if the Refresh button is clicked, shown below. The card image files are named 1.png, 2.png … 52.png and stored in the image/card directory. All four cards are distinct and selected randomly. The card files are provided in Blackboard.

Hint: You can select random cards by storing the numbers 1-52 to an array list, perform a random shuffle learned in Section 11.12, and use the first four numbers in the array list as the file names for the image. The other way is to use the static shuffle method in the java.util.Collections class, for example: java.util.Collections.shuffle(list) where list is an array list.

Directions

Create a class named CardRefreshButton extends Application.

Create a new array list, then use a for loop and add() method to add 52 cards to the list.

Revoke shuffle() method for the list to shuffle the cards.

Create an instance of HBox and add four card images by using getChildren().add() method. Make sure specify image file path and use array list’s get() method to get four card image files.

Create a Button and labeled with “Refresh”

Create and register an event handler.

To handle event: once the button is clicked, call shuffle() method to shuffle the cards, then clear previous images by using getChildren.clear() method, and use add() to add four card images.

Create a new BorderPane to set up layout for the images and button.

Create a scene with a specific size.

Set title to “Shuffling cards” and display the stage.

The output should look like the screen below.

Provide appropriate Java comments.

Explanation / Answer

// CardRefreshButton.java

import java.util.ArrayList;

import java.util.Collections;

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

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 {

    //replace the location variable's value with your image path.

    //dont remove the prefix 'file:'

    static String location = "file:C:\Users\user\Downloads\CARDPHOTO\", fileExt = ".png";

    //declaring an arraylist of images

    ArrayList<Image> images;

    //container to show images

    HBox imageViews;

    @Override

    public void start(Stage primaryStage) {

        //initializing arraylist

        images = new ArrayList<Image>();

       //loading images

       for (int i = 0; i < 52; i++) {

            /* Assuming that you placed all of your 52 images in a folder, in proper names starting from 1.png, 2.png etc.. upto 52.png. Please replace 'location' variable with your image path. DONT REMOVE the prefix 'file:', it is very important */

            Image im = new Image(location + (i + 1) + fileExt, true);

            images.add(im);

        }

        //defining hbox to arrange elements horizontally

        imageViews = new HBox();

        imageViews.setAlignment(Pos.CENTER);//aligning center

        //shuffling cards

        shuffle();

        //adding to the imageViews HBox

        add();

        // Defining refresh button and registering event handler

        Button refresh = new Button("Refresh");

        refresh.setOnAction(new EventHandler<ActionEvent>() {

            @Override

            public void handle(ActionEvent event) {

                //shuffling and adding again

                shuffle();

                add();

            }

        });

        // Defining a BorderPane to arrange images and button

        BorderPane pane = new BorderPane();

        pane.setCenter(imageViews);

        pane.setBottom(refresh);

        // Defining a Scene and displaying

        Scene scene = new Scene(pane, 300, 200);

        primaryStage.setScene(scene);

        primaryStage.setTitle("Shuffling cards");

        primaryStage.show();

    }

     // method to clear current children of imageViews Hbox and add 4 ImageViews into it

    public void add() {

       //clearing the current images

        imageViews.getChildren().clear();

        //creating four imageviews with random card images        

        for (int i = 0; i < 4; i++) {

            ImageView im = new ImageView(images.get(i));

            imageViews.getChildren().add(im);

        }

    }

     //method to shuffle the images list    

    public void shuffle() {

        //shuffling the cards list

        Collections.shuffle(images);

    }

    public static void main(String[] args) {

        launch(args);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote