Question

Using JAVA FX how would I combine the two programs below into one interface that allows...

Using JAVA FX how would I combine the two programs below into one interface that allows the user to pick which specific program they would like to play. They should be able to choose which one they want to play and then switch between them if necesary. All of this must be done in JAVA FX code

Black jack javafx - first game program

package application;

import java.util.Arrays;

import java.util.ArrayList;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.application.Application;

import javafx.geometry.Pos;

import javafx.geometry.Insets;

import javafx.scene.Scene;

import javafx.scene.layout.HBox;

import javafx.scene.layout.GridPane;

import javafx.scene.control.Label;

import javafx.scene.control.Labeled;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.stage.Stage;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

public class Main extends Application {

public String btHitY;

public int NumberPlayerCards;

public int NumberDealerCards;

public int NUMBER_OF_CARDS;

public int PlayerCards[];

public int DealerCards[];

public Image imagesP[];

public Image imagesD[];

public int deck[];

public String URLBase;

public GridPane pane;

public Main() {

this.btHitY = " ";

this.btHitY = new String();

}

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

public void start(Stage primaryStage) {

//Create array deck, suit string, and rank string

deck = new int[52];

String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};

String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

int NUMBER_OF_CARDS = 4;

//Initialize the cards

for (int i = 0; i < deck.length; i++)

deck[i] = i;

//Shuffle the cards

for (int i = 0; i < deck.length; i++) {

//Generate an index randomly

int index = (int)(Math.random() * deck.length);

int temp = deck[i];

deck[i] = deck[index];

deck[index] = temp;

}

int NumberPlayerCards = 2;

int NumberDealerCards = 2;

int[] PlayerCards = new int[50];

int[] DealerCards = new int[50];

//Display the first cards

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

String suit = suits[deck[i] / 13];

String rank = ranks[deck[i] % 13];

System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit);

}

for (int i = 0; i < NumberPlayerCards; i++)

PlayerCards[i] = deck[i * 2];

for (int i = 0; i < NumberDealerCards; i++)

DealerCards[i] = deck[i * 2 + 1];

// Create a pane to hold the image views

GridPane pane = new GridPane();

pane.setAlignment(Pos.CENTER);

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

pane.setHgap(5);

pane.setVgap(5);

Image[] imagesP = new Image[50];

Image[] imagesD = new Image[50];

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

int cardForPrint = PlayerCards[i] + 1;

System.out.println(URLBase + cardForPrint + ".png");

imagesP[i] = new Image(URLBase + cardForPrint + ".png");

}

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

int cardForPrint = DealerCards[i] + 1;

System.out.println(URLBase + cardForPrint + ".png");

imagesD[i] = new Image(URLBase + cardForPrint + ".png");

}

//rotate flag image to cover dealer card

Image flag = new Image("http://www.cs.armstrong.edu/liang/common/image/us.gif");

ImageView imageFlag = new ImageView(flag);

imageFlag.setRotate(90);

imageFlag.setFitHeight(75);

imageFlag.setFitWidth(95);

pane.add(new Label("Player Cards"), 0, 0);

pane.add(new ImageView(imagesP[0]), 1, 0);

pane.add((imageFlag), 1, 1);

pane.add(new Label("Dealer Cards"), 0, 1);

pane.add(new ImageView(imagesP[1]), 2, 0);

pane.add(new ImageView(imagesD[1]), 2, 1);  

Button btHit = new Button("Hit");

Button btStay = new Button("Stay");

pane.add(btHit, 1, 2);

pane.add(btStay, 2, 2);

// Create a scene and place it in the stage

Scene scene = new Scene(pane, 1200, 700);

primaryStage.setTitle("Black Jack"); // Set the stage title

primaryStage.setScene(scene); // Place the scene in the stage

primaryStage.show(); // Display the stage

HitHandlerClass handlerHit = new HitHandlerClass();

btHitY = " ";

btHit.setOnAction(handlerHit);

/* if (btHitY.equals("Hit")); {

NumberPlayerCards = NumberPlayerCards + 1;

NUMBER_OF_CARDS = NUMBER_OF_CARDS + 1;

PlayerCards[NumberPlayerCards - 1] = deck[NUMBER_OF_CARDS - 1];

for (int j = 0; j < NumberPlayerCards; j++){

System.out.println(PlayerCards[j]);

}

System.out.println(NumberPlayerCards);

int CardForPrint2 = PlayerCards[NumberPlayerCards - 1] + 1;

imagesP[NumberPlayerCards - 1] = new Image(URLBase + CardForPrint2 + ".png");   

pane.add(new ImageView(imagesP[NumberPlayerCards - 1]), NumberPlayerCards, 0);

btHitY = " ";

primaryStage.show();

} */

}

/**

* The main method is only needed for the IDE with limited

* JavaFX support. Not needed for running from the command line.

* @param args

*/

public static void main(String[] args) {

launch(args);

}

class HitHandlerClass implements EventHandler<ActionEvent> {

@Override

public void handle(ActionEvent e) {

NumberPlayerCards = NumberPlayerCards + 1;

NUMBER_OF_CARDS = NUMBER_OF_CARDS + 1;

PlayerCards[NumberPlayerCards - 1] = deck[NUMBER_OF_CARDS - 1];

for (int j = 0; j < NumberPlayerCards; j++){

System.out.println(PlayerCards[j]);

}

System.out.println(NumberPlayerCards);

int CardForPrint2 = PlayerCards[NumberPlayerCards - 1] + 1;

imagesP[NumberPlayerCards - 1] = new Image(URLBase + CardForPrint2 + ".png");   

pane.add(new ImageView(imagesP[NumberPlayerCards - 1]), NumberPlayerCards, 0);

btHitY = " ";

}

}

}

Next game

import java.util.Calendar;
import java.util.GregorianCalendar;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class MyCalendar extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();

CalendarPane calendarPane = new CalendarPane();
pane.setCenter(calendarPane);

Button btPrior = new Button("Prior");
Button btNext = new Button("Next");
HBox hBox = new HBox(5);
hBox.getChildren().addAll(btPrior, btNext);
pane.setBottom(hBox);
hBox.setAlignment(Pos.CENTER);

// Create a scene and place it in the stage
Scene scene = new Scene(pane, 600, 300);
primaryStage.setTitle("My Calendar"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage

btPrior.setOnAction(e -> {
int currentMonth = calendarPane.getMonth();
if (currentMonth == 0) { // The previous month is 11 for Dec
calendarPane.setYear(calendarPane.getYear() - 1);
calendarPane.setMonth(11);
}
else {
calendarPane.setMonth((currentMonth - 1) % 12);
}
});

btNext.setOnAction(e -> {
int currentMonth = calendarPane.getMonth();
if (currentMonth == 11) // The next month is 0 for Jan
calendarPane.setYear(calendarPane.getYear() + 1);

calendarPane.setMonth((currentMonth + 1) % 12);
});
}

/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}

class CalendarPane extends BorderPane {
private String[] monthName = {"January", "Feburary", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};

// The header label
private Label lblHeader = new Label();

// Maximum number of labels to display day names and days
private Label[] lblDay = new Label[49];

private Calendar calendar;
private int month; // The specified month
private int year; // The specified year

public CalendarPane() {
// Create labels for displaying days
for (int i = 0; i < 49; i++) {
lblDay[i] = new Label();
lblDay[i].setTextAlignment(TextAlignment.RIGHT);
}

lblDay[0].setText("Sunday");
lblDay[1].setText("Monday");
lblDay[2].setText("Tuesday");
lblDay[3].setText("Wednesday");
lblDay[4].setText("Thursday");
lblDay[5].setText("Friday");
lblDay[6].setText("Saturday");

GridPane dayPane = new GridPane();
dayPane.setAlignment(Pos.CENTER);

dayPane.setHgap(10);
dayPane.setVgap(10);
for (int i = 0; i < 49; i++) {
dayPane.add(lblDay[i], i % 7, i / 7);
}

// Place header and calendar body in the pane
this.setTop(lblHeader);
BorderPane.setAlignment(lblHeader, Pos.CENTER);
this.setCenter(dayPane);

// Set current month and year
calendar = new GregorianCalendar();
month = calendar.get(Calendar.MONTH);
year = calendar.get(Calendar.YEAR);
updateCalendar();

// Show calendar
showHeader();
showDays();
}

public void showHeader() {
lblHeader.setText(monthName[month] + ", " + year);
}

/**
* Display days
*/
public void showDays() {
// Get the day of the first day in a month
int startingDayOfMonth = calendar.get(Calendar.DAY_OF_WEEK);

// before month dates are printed in red colour
Calendar cloneCalendar = (Calendar) calendar.clone();
cloneCalendar.add(Calendar.DATE, -1); // Becomes preceding month
int daysInPrecedingMonth = cloneCalendar.getActualMaximum(
Calendar.DAY_OF_MONTH);

for (int i = 0; i < startingDayOfMonth - 1; i++) {
lblDay[i + 7].setTextFill(Color.RED);
lblDay[i + 7].setText(daysInPrecedingMonth
- startingDayOfMonth + 2 + i + "");
}

// current month date is in BLUE colour
int daysInCurrentMonth = calendar.getActualMaximum(
Calendar.DAY_OF_MONTH);
for (int i = 1; i <= daysInCurrentMonth; i++) {
lblDay[i - 2 + startingDayOfMonth + 7].setTextFill(Color.BLUE);
lblDay[i - 2 + startingDayOfMonth + 7].setText(i + "");
}

// after month dates are printed in green colour
int j = 1;
for (int i = daysInCurrentMonth - 1 + startingDayOfMonth + 7;
i < 49; i++) {
lblDay[i].setTextFill(Color.GREEN);
lblDay[i].setText(j++ + "");
}
}

/**
* Set the calendar to the first day of the specified month and year
*/
public void updateCalendar() {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, 1);
}

/**
* Return month
*/
public int getMonth() {
return month;
}

/**
* Set a new month
*/
public void setMonth(int newMonth) {
month = newMonth;
updateCalendar();
showHeader();
showDays();
}

/**
* Return year
*/
public int getYear() {
return year;
}

/**
* Set a new year
*/
public void setYear(int newYear) {
year = newYear;
updateCalendar();
showHeader();
showDays();
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi Sir, I am suggesting you an easy way to launch either of the two JavaFX Game Application based on user Input. Create a Java Class "LaunchJavaFXGame.java", and copy the copy below:-

1. LaunchJavaFXGame.java

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.GridPane;

import javafx.stage.Stage;

public class LaunchJavaFXGame extends Application {

public Button b0, b1;

@Override

public void start(Stage primaryStage) {

GridPane grid = new GridPane();

b0 = new Button("Launch MyCalendar Application");

b0.setId("0");

b0.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);

grid.add(b0, 0, 1);

b1 = new Button("Launch BlackJack Application");

b1.setId("1");

b1.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);

grid.add(b1, 1,1);

Scene scene = new Scene(grid, 400, 100);

primaryStage.setScene(scene);

primaryStage.setResizable(false);

primaryStage.setTitle("JavFX Game Launcher");

primaryStage.show();

b0.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent event) {

//create an object of the class you wish to invoke its start() method:

MyCalendar myCalendarObj = new MyCalendar();

// Then call its start() method in the following way:

myCalendarObj.start(primaryStage);

}// End handle(ActionEvent event)

});// End anonymous class

b1.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent event) {

//create an object of the class you wish to invoke its start() method:

Main mainObj = new Main();

// Then call its start() method in the following way:

mainObj.start(primaryStage);

}// End handle(ActionEvent event)

});// End anonymous class

}

public static void main(String[] args) {

launch(args);

}

}

The output is attached below:-

? JavFX Game Launcher Launch MyCalendar Application Launch BlackJack Application

On clicking any game that you want to launch from this Game Chooser JavaFX, the corresponding application will open:-

Please let me know in case of any clarifications required. Thanks!

Add a comment
Know the answer?
Add Answer to:
Using JAVA FX how would I combine the two programs below into one interface that allows...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Intro to Java - Assignment JAVA ASSIGNMENT 13 - Object Methods During Event Handling Assignment 13...

    Intro to Java - Assignment JAVA ASSIGNMENT 13 - Object Methods During Event Handling Assignment 13 Assignment 13 Preparation This assignment will focus on the use of object methods during event handling. Assignment 13 Assignment 13 Submission Follow the directions below to submit Assignment 13: This assignment will be a modification of the Assignment 12 program (see EncryptionApplication11.java below). Replace the use of String concatenation operations in the methods with StringBuilder or StringBuffer objects and their methods. EncryptTextMethods.java ====================== package...

  • Java Programming Assignment (JavaFX required). You will modify the SudokuCheckApplication program that is listed below. Start...

    Java Programming Assignment (JavaFX required). You will modify the SudokuCheckApplication program that is listed below. Start with the bolded comment section in the code below. Create a class that will take string input and process it as a multidimensional array You will modify the program to use a multi-dimensional array to check the input text. SudokuCheckApplication.java import javafx.application.*; import javafx.event.*; import javafx.geometry.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; public class SudokuCheckApplication extends Application { public void start(Stage primaryStage)...

  • (5 points) Analyze the following codes. b) import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import...

    (5 points) Analyze the following codes. b) import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class Test extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { Button btOK = new Button("OK"); Button btCancel = new Button("Cancel"); EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { System.out.println("The OK button is clicked"); } }; btOK.setOnAction(handler); btCancel.setOnAction(handler); HBox pane = new HBox(5); pane.getChildren().addAll(btOK, btCancel); Scene...

  • use this code of converting Km to miles , to create Temperature converter by using java...

    use this code of converting Km to miles , to create Temperature converter by using java FX import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.geometry.Pos; import javafx.geometry.Insets; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.control.Button; import javafx.event.EventHandler; import javafx.event.ActionEvent; /** * Kilometer Converter application */ public class KiloConverter extends Application { // Fields private TextField kiloTextField; private Label resultLabel; public static void main(String[] args) { // Launch the application. launch(args); } @Override public void start(Stage primaryStage) { //...

  • Can someone please comment this on what the javafx do and how they are implemented? import...

    Can someone please comment this on what the javafx do and how they are implemented? import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Paint; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; public class TextPanel extends Application { GridPane grid = null; Text scenetitle = null; Label tDrawLabel = null; Label bsc345Label = null; HBox THBox = null; VBox mVBox = new VBox(2); Stage primaryStage = null; @Override public void start(Stage...

  • JAVAFX ONLY PROGRAM!!!!! SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS. DIRECTIONS ARE BELOW: DIRECTIONS: The main...

    JAVAFX ONLY PROGRAM!!!!! SORTING WITH NESTED CLASSES AND LAMBDA EXPRESSIONS. DIRECTIONS ARE BELOW: DIRECTIONS: The main point of the exercise is to demonstrate your ability to use various types of nested classes. Of course, sorting is important as well, but you don’t really need to do much more than create the class that does the comparison. In general, I like giving you some latitude in how you design and implement your projects. However, for this assignment, each piece is very...

  • Please Help, JavaFX assignment. This assignment will focus on the use anonymous inner class handlers to...

    Please Help, JavaFX assignment. This assignment will focus on the use anonymous inner class handlers to implement event handling. Assignment 12 Assignment 12 Submission Follow the directions below to submit Assignment 12: This assignment will be a modification of the Assignment 11 program. This program should use the controls and layouts from the previous assignment. No controls or layouts should be added or removed for this assignment. Add event handlers for the three buttons. The event handlers should be implemented...

  • Introduction to Java Programming Question: I’m doing a java game which user clicks on two different...

    Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically looking for codes that could make my memory match game more difficult or more interesting.(a timer, difficulty level, color, etc.) GUI must be included in the code. Here is what I got so far: package Card; import javax.swing.JButton; public class Card extends JButton{    private int id;   ...

  • Java program that creates a photo album application.    Which will load a collection of images...

    Java program that creates a photo album application.    Which will load a collection of images and displays them in a album layout. The program will allow the user to tag images with metadata: •Title for the photo .      Limited to 100 characters. •Description for the photo . Limited to 300 characters. •Date taken •Place taken Functional Specs 1.Your album should be able to display the pictures sorted by Title, Date, and Place. 2.When the user clicks on a photo,...

  • / Finish the code to make it work import static javafx.application.Application.launch; import java.io.File; import javafx.application.Application; import...

    / Finish the code to make it work import static javafx.application.Application.launch; import java.io.File; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.media.AudioClip; import javafx.stage.Stage; public class JukeBox extends Application { private ChoiceBox<String> choice; private AudioClip[] tunes; private AudioClip current; private Button playButton, stopButton;    //----------------------- // presents an interface that allows the user to select and play // a tune from a drop down box //-----------------------   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT