me@michellealzoladesign.com

Mon - Fri: 8AM - 5PM MDT

Introduction

JavaFX is a powerful software development kit (SDK) for creating desktop applications with rich graphical user interfaces (GUI). With JavaFX, developers can easily create and deploy high-quality and visually appealing applications that run smoothly on various platforms.

This article will explore a JavaFX project called Slot Machine Simulator, which simulates a slot machine game. The game allows users to insert a certain amount of money, spin the reels, and win prizes based on the combination of symbols that appear on the reels.

The Code

The Slot Machine Simulator project contains several Java classes, including the main class, which extends the JavaFX Application class. The main class defines the GUI components, such as buttons, labels, text fields, and image views, and their behaviors when users interact with them.

The project uses images of slot machine symbols, such as cherries, sliced pomelo, and apple, to simulate the slot machine reels. The images are loaded into the application using the getImage() method, which takes the file path of the image and returns an Image object that can be displayed in an ImageView component.

// Image view 1
int value1 = rand.nextInt(3);			
String image1 = spinImage(value1);
slotImage1 = getImage(image1);
imageView1 = getImageView_(slotImage1, imageView1);

The spinImage() method takes a randomly generated value between 0 and 2 and returns the file path of the corresponding image. The getImageView_() method takes an Image object and an ImageView object and sets the image and size properties of the image view.

When the user clicks the Spin button, the spin.setOnAction() method generates three random values between 0 and 2, retrieves the corresponding images, and sets the images to the three image views. The method then calculates the amount of money won based on the combination of symbols and updates the Amount Won this Spin and Total Amount Won labels.

spin.setOnAction(event->
{
       // statements.....

       resultAmountWon.setText(String.format("$%,.2f", won));
       double totalwon = accumulateTotal(won);
       
       resultTotalAmountWon.setText(String.format("$%,.2f", totalwon));
}

The accumulateTotal() method keeps track of the total amount of money won by adding the amount won in each spin to the static variable totalWon.

The GUI

The Slot Machine Simulator GUI consists of three image views that simulate the three reels of a slot machine. The user can input the amount of money to be inserted into the machine using the Amount Inserted text field.

After the user clicks the Spin button, the images in the image views spin and stop at a random position. If 2 of the reels matched, the amount won is doubled the amount inserted, and if all three reels matched, the prize is 3 times the amount inserted.

See my Code

Java_Projects_Practice/SlotMachineSimulator.java at main · michellealzola/Java_Projects_Practice (github.com)

See the App in Action

Conclusion

The Slot Machine Simulator project demonstrates the power and flexibility of JavaFX in creating desktop applications with rich graphical user interfaces. The project also introduces basic Java programming concepts, such as file I/O, event handling, and GUI design.

By exploring and modifying the code, developers can learn how to create more complex JavaFX applications and customize the GUI to suit their needs. With JavaFX, the possibilities are endless, and the only limit is the developer’s imagination.

Reference

T. Gaddis and G. Muganda, Starting Out with Java – From Control Structures through Data Structures, 4th Edition, 330 Hudson Street, NY NY 10013: Pearson, 2019.

Recommended Articles