Could you please help on this Java program? I did the program in eclipse. Howeve
ID: 3603721 • Letter: C
Question
Could you please help on this Java program? I did the program in eclipse. However, it doesn’t 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<Integer> cards = new 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")));
}
}
}
Explanation / Answer
import javafx.application.Application;
import javafx.geometry.Insets;
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 Exercise_01 extends Application {
public void start(Stage primaryStage) {
HBox hBox = new HBox();
hBox.setAlignment(Pos.CENTER);
hBox.setSpacing(10);
setRandomCards(hBox);
HBox buttons = new HBox();
buttons.setAlignment(Pos.BOTTOM_CENTER);
buttons.setSpacing(10);
Button btRefresh = new Button("Refresh");
btRefresh.setOnAction(e -> setRandomCards(hBox));
buttons.getChildren().add(btRefresh);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(hBox);
borderPane.setBottom(buttons);
BorderPane.setMargin(buttons, new Insets(10, 10, 10, 10));
borderPane.setPadding(new Insets(10, 10, 0, 10));
borderPane.autosize();
Scene scene = new Scene(borderPane, borderPane.getPrefWidth(), borderPane.getPrefHeight());
primaryStage.setTitle("3 Random Cards");
primaryStage.setScene(scene);
primaryStage.show();
}
private void setRandomCards(HBox pane){
boolean[] usedCards = new boolean[52];
// choose 3 random distinct cards from the deck
int count = 0;
pane.getChildren().clear();
while (count < 4) {
int card = (int) (Math.random() * 52);
if (!usedCards[card]) {
usedCards[card] = true;
// card png files start from 1, so increment card by one for the correct file name.
pane.getChildren().add(new ImageView(new Image("image/card/" + (++card) + ".png")));
count++;
}
}
}
public static void main(String[] args) {
Application.launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.