Question: Could you please help on this Java program? I did the program in eclipse. However, it doesnt run. I am sure I did a mistake
Could you please help on this Java program? I did the program in eclipse. However, it doesnt run. I am sure I did a mistake linking images (I placed all the cards in the bin folder)- please see below my program. Please help me, I really appreciate your help.
(pick 4 cards) Write a program that lets the user click the Refresh button to display four cards from a deck of 52 cards.
Hint: Write a program that displays three cards randomly selected from a deck of 52, (three cards should show in line). The card image files are named 1.png.2.png. 52.png and stored in the image/card directory. All three cards are distinct and selected randomly. Hint: You can select random cards by storing the numbers 1-52 to an array list, perform a random shuffle and use the first three numbers in the array list as the file names for the image.
import javafx.application.Application;
import javafx.stage.Stage;
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.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import java.util.ArrayList;
public class Exercise_15_01 extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a VBox
VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER);
vBox.setPadding(new Insets(5, 5, 5, 5));
// Create a HBox
HBox hBox = new HBox(5);
hBox.setAlignment(Pos.CENTER);
hBox.setPadding(new Insets(5, 5, 5, 5));
getCards(hBox);
// Create a button
Button btRefresh = new Button("Refresh");
// Process events
btRefresh.setOnAction(e -> getCards(hBox));
// Place nodes in vbox
vBox.getChildren().addAll(hBox, btRefresh);
// Create a scene and place it in the stage
Scene scene = new Scene(vBox);
primaryStage.setTitle("Exercise_15_01"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/** Returns a list with numbers 1-52 stored in random order */
private void getCards(HBox pane) {
pane.getChildren().clear();
// Create a deck of card
ArrayList
for (int i = 0; i < 52; i++) {
cards.add(i + 1);
}
// shuffle deck
java.util.Collections.shuffle(cards);
// Add nodes to pane
for (int i = 0; i < 4; i++) {
pane.getChildren().add(new ImageView(new
// I think problem is directing png images//
Image("file:C:\\Users\\eclipse-workspace\\exercise15_1\\bin/" + cards.get(i) + ".png")));
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
