Write a program that lets the user click the Refresh button to display four card
ID: 3600614 • Letter: W
Question
Write a program that lets the user click the Refresh button to display four cards from a deck of 52 cards, (see the hint in Programming below on how to obtain four random cards.)
Write a program that displays three cards randomly selected from a deck of 52,(three random cards). 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 introduced in section 11.12 and use the first three numbers in the array list as the file names for the image.
Explanation / Answer
Please find the below code
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.geometry.Insets;
import java.util.ArrayList;
public class Exercise_14_03 extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a number of card numbers
ArrayList<Integer> cards = getCards();
// Create a HBox pane
HBox pane = new HBox(5);
pane.setPadding(new Insets(5, 5, 5, 5));
for (int i = 0; i < 3; i++) {
pane.getChildren().add(new ImageView(new Image("image/card/" +
cards.get(i) + ".png")));
}
Scene scene = new Scene(pane);
primaryStage.setTitle("Exercise_14_03"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
private ArrayList<Integer> getCards() {
ArrayList<Integer> cards = new ArrayList<>();
for (int i = 0; i < 52; i++) {
cards.add(i + 1);
}
java.util.Collections.shuffle(cards);
return cards;
}
}
Please find the below code also
package Chapter_15;
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
{
@Override
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];
int count = 0;
pane.getChildren().clear();
while (count < 4) {
int card = (int) (Math.random() * 52);
if (!usedCards[card]) {
usedCards[card] = true;
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.