Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming I need help, please. This is the 5th time I am posting the same assignment. Help,Help, please. Thanks My button is not displaying

Java Programming

I need help, please. This is the 5th time I am posting the same assignment. Help,Help, please. Thanks

My button is not displaying the cards

Assignment:

Using the Assignment Cards Zip File attached above (this is the same zip file used in Module 7), write a program that displays four playing cards. In addition, your program should meet the following requirements:

Below the cards, there will be a refresh button that will then display four different cards.

All cards will be displayed through random selection.

Here is my code

/*Yawa Hallo 2/23/2023 Module 10 assignment 2

*

*/

package Cards;

import javafx.application.Application;

import java.util.*;

import java.util.ArrayList;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Pos;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.control.Button;

import javafx.scene.layout.HBox;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.StackPane;

import javafx.stage.Stage;

import java.util.Random;

public class ButtonDisplayCards extends Application {

//private static final Object[] imageViews = null;

@Override

public void start(Stage primaryStage) throws Exception {

//Store image of urls in arraylist

//You need to store all images in the given directory

//with files.

ArrayList imageUrls = new ArrayList<>();

for(int i = 1; i<=52; i++)

{

imageUrls.add(String.format("/Cards/%d.png",i));

}

//shuffle the images using shuffle()

Collections.shuffle(imageUrls);

//set title

primaryStage.setTitle("Random Cards Generator");

//mention number of images to display

int numOfCardsToDisplay = 4;

Image[] images = new Image[numOfCardsToDisplay];

ImageView[] imageViews = new ImageView[numOfCardsToDisplay];

int imageStartX = 25;

int imageStartY = 25;

int sceneWidth = 700;

//loop to display number of cards side by side

for(int i = 0;i

{

if(imageStartX + 150 >= sceneWidth)

{

imageStartX = 25;

imageStartY += 270;

}

images[i] = new Image(imageUrls.get(i));

imageViews[i] = new ImageView(images[i]);

imageViews[i].setX(imageStartX);

imageViews[i].setY(imageStartY);

imageViews[i].setFitWidth(150);

imageViews[i].setFitHeight(250);

imageViews[i].setPreserveRatio(true);

}

Group root = new Group(imageViews);

// create button on the pane

HBox hBox = new HBox();

hBox.setAlignment(Pos.CENTER);

hBox.setSpacing(10);

Button buttonRefresh= new Button("Refresh");

hBox.getChildren().add(buttonRefresh);

root.getChildren().add(hBox);

//create and register the handler

buttonRefresh.setOnAction(new RefreschHandler());

//create Pane and put the hBox and button

BorderPane borderPane = new BorderPane();

borderPane.setCenter(root);

borderPane.setBottom(hBox);

borderPane.setAlignment(hBox, Pos.BOTTOM_CENTER);

Scene scene = new Scene(borderPane, 700,500);

primaryStage.setScene(scene);

primaryStage.show();

}

//Event handler

class RefreschHandler implements EventHandler {

public void handle(ActionEvent e){

refreshCards();

}

}

//Randomly display the cards with refresh button

Random random = new Random();

private void refreshCards() {

//Randomly select 4 cards

ArrayList imageUrls = new ArrayList<>();

for(int i = 1; i<=52; i++)

{

imageUrls.add(String.format("/Cards/%d.png",i));

}

//shuffle the images using shuffle()

Collections.shuffle(imageUrls);

int[] cardIndices = new int[4];

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

cardIndices[i] = random.nextInt(52);

Image image = new Image(imageUrls.get(cardIndices[i]));

ImageView imageView = new ImageView();

imageView.setImage(image);

}

}

public static void main(String[] args) {

Application.launch(args);

}

}

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

Recommended Textbook for

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

More Books

Students also viewed these Databases questions

Question

2. Compare the sales and service departments at Auto World.

Answered: 1 week ago