Question: HERE IS THE ORGINAL QUESTION; Programming Problem 3 Shuffling Cards Write a program that displays four cards randomly selected from a deck of 52 if

HERE IS THE ORGINAL QUESTION;

Programming Problem 3 Shuffling Cards

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 lists 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.

import javafx.application.Application;

import javafx.stage.Stage;

import javafx.scene.Scene;

import java.util.Random;

import javafx.scene.layout.*;

import javafx.scene.image.ImageView;

import javafx.scene.control.Button;

import javafx.geometry.Pos;

//creating class CardRefreshButton which extends Application

public class CardRefreshButton extends Application {

public static final int ASIZE = 54;

public static final int ROWS = 6;

public static final int COLS = 9;

public void start(Stage primaryStage)

{

int aCardDeck[] = new int[ASIZE]; //54 cards in a deck

for(int i =0; i

aCardDeck[i] = i + 1; //populate with elements 1-54

GridPane gPane = new GridPane(); //Instantiate Grip for cards in rows/cols

for(int i=0, k=0; i

for(int j=0; j

gPane.add(new ImageView("image/card" + aCardDeck[k++]+".png"), j, i);

}

}

// creating button called Shuffle

Button btShuffle = new Button("Shuffle");

btShuffle.setOnAction

( e -> //Lamda Event Handler

{ // Like anonymous inner class

int r, c, n;

shuffle(aCardDeck); //Random shuffle

gPane.getChildren().clear();

for(r=n=0; r

for (c=0; c

gPane.add(new ImageView("~/../public/image/card/" + aCardDeck[n++]+ ".png"), c, r);

}

});

HBox hBox = new HBox(5); //Row of Buttons

hBox.getChildren().add(btShuffle); //Add button to box

BorderPane pane = new BorderPane();

pane.setCenter(gPane); // Layout rows of cards in center

pane.setBottom(hBox); //layout buttons at bottom

BorderPane.setAlignment(hBox, Pos.CENTER);

Scene scene = new Scene(pane, 650, 600); //Create scene, place in stage

primaryStage.setTitle("Shuffling Cards");

primaryStage.setScene(scene);

primaryStage.show();

}

public void ascendSort (int [] a){}

public void descendSort1 (int [] a){}

public void ascendRankSort (int [] a){}

public void descendSort (int [] a){}

public void shuffle(int [] a){

int r, tmp;

Random rand = new Random();

long seed = System.currentTimeMillis();

rand.setSeed(seed);

for( int i =0; i

r = rand.nextInt(ASIZE);

tmp = a[r];

a[r] = a[i];

a[i] = tmp;

}

}

public static void main(String[] args)

{

launch(args);

}

}

would you fix the error please it says Synax error, I already have the 52 carsd uploaded to the class but it does not work.

here what it give me;

Exception in Application start method

java.lang.reflect.InvocationTargetException

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.base/java.lang.reflect.Method.invoke(Unknown Source)

at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)

at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.base/java.lang.reflect.Method.invoke(Unknown Source)

at java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)

Caused by: java.lang.RuntimeException: Exception in Application start method

at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)

at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)

at java.base/java.lang.Thread.run(Unknown Source)

Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found

at javafx.graphics/javafx.scene.image.Image.validateUrl(Unknown Source)

at javafx.graphics/javafx.scene.image.Image.(Unknown Source)

at javafx.graphics/javafx.scene.image.ImageView.(Unknown Source)

at CardRefreshButton.start(CardRefreshButton.java:27)

at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)

at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)

at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)

at java.base/java.security.AccessController.doPrivileged(Native Method)

at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)

at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)

at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)

at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)

... 1 more

Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found

... 13 more

Exception running application CardRefreshButton

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!