Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

14.3 (Display three cards) * Write a program that displays three cards randomly selected from * a deck of 52, as shown in Figure 14.43c.

14.3 (Display three cards)

* Write a program that displays three cards randomly selected from

* a deck of 52, as shown in Figure 14.43c. 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 152 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.

Can you show me how Create a list of card numbers, to Create a HBox pane, Add nodes to pane....

The java code is below:

/*********************************************************************************

* (Display three cards) Write a program that displays three cards randomly *

* selected from a deck of 52, as shown in Figure 14.43c. 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 152 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. *

*********************************************************************************/

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 Exercise14_03 extends Application {

@Override // Override the start method in the Application class

public void start(Stage primaryStage) {

// Create a list of card numbers

ArrayList cards = getCards();

// Create a HBox pane

HBox pane = new HBox(5);

pane.setPadding(new Insets(5, 5, 5, 5));

// Add nodes to pane

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

pane.getChildren().add(new ImageView(new Image("image/card/" +

cards.get(i) + ".png")));

}

// Create a scene and place it in the stage

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

}

/** Returns a list with numbers 1-52 stored in random order */

private ArrayList getCards() {

ArrayList cards = new ArrayList<>();

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

cards.add(i + 1);

}

java.util.Collections.shuffle(cards);

return cards;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

1. What might have led to the misinformation?

Answered: 1 week ago

Question

2. How will you handle the situation?

Answered: 1 week ago

Question

3. Write a policy statement to address these issues.

Answered: 1 week ago