Question: Add 2 more cards and a button. When the button is clicked five cards will display. Hint: Take your program from Assignment #5 and modify

Add 2 more cards and a button. When the button is clicked five cards will display.

Hint:

Take your program from Assignment #5 and modify it:

  • Add 2 cards (5 total) to the pane (I recommend HBox)
  • Add a button call it anything you like (e.g. btShuffle)
  • Register an Anonymous Handler using Lambda Expression in the buttons setOnAction method
  • In the Lamda Expression
    • shuffle the list
    • clear the pane (e.g. cardPane.getChildren.clear();)
    • add the five card images (e.g. add(new ImageView("image/card/" + list.get(0) + ".png");

here is the code that i have:

import javafx.application.Application;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.layout.GridPane;

import javafx.stage.Stage;

public class Cards extends Application {

@Override

public void start(Stage primaryStage) {

GridPane pane = new GridPane();

pane.setAlignment(Pos.CENTER);

boolean[]usedCards = new boolean [52];

int count = 0;

while (count < 3) {

int card = (int)(Math.random()*52);

if(!usedCards[card]) {

usedCards[card]=true;

pane.add(new ImageView(new Image("card/"+(++card)+".png" )), count, 0);

count++;

}

}

Scene scene = new Scene (pane, 250, 150);

primaryStage.setTitle("3 Random Cards");

primaryStage.setScene(scene);

primaryStage.show();

}

public static void main(String[] args) {

Application.launch(args);

}

}

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 Programming Questions!