The java program at the bottom of this post creates a GUI that allows me to enter the multiplier/target values by clicking radio buttons. The GUI has a 'Get Strategy' button. When I click this button, it does four things. It runs a program called magic.exe that generates the appropriate strategy. It prints the output of that program to the console with color coding for S/D/H/P/R decisions. It increments the game counter so I can keep track of how many games I have played in the session. The program logs the multiplier and target to hard-coded file location.
You could replace magic.exe with a program that would throw up a JPG viewer with the right screenshot of the appropriate DK strategy chart. Or you could write a program to generate a better strategy yourself. That is what I did using Eric Farmer's code as a starting point.
I tried to use ChatGPT to describe the workings of the program. I never tried this before and it it pretty good at writing documentation and formatting the code.
This Java code creates a simple graphical user interface (GUI) application that is centered around a "Bingo BJ Multiplier and Target" game. Let's break down what each part does:
Components:
- TextField (mtgDisplay): This field displays the current multiplier (myMult), target (myTarg), and the game count. It's set to be non-editable and displayed in a large font.
- Buttons (btnStrat and btnQuit):
- btnStrat triggers the logic for getting a strategy (which involves running an external process).
- btnQuit exits the application when clicked.
- Radio Buttons: The GUI features two sets of radio buttons:
- One set allows the user to select a multiplier (2, 5, 10, 25, 50, or 100).
- The other set allows the user to select a target value (from 12 to 21).
- Panel and Layouts: The code uses a GroupLayout and other panels to organize components on the window.
Functionality:
- Multiplier and Target Selection:
- Users can select a multiplier from a set of predefined values (2, 5, 10, 25, 50, 100).
- Users can select a target from the range 12 to 21.
- The selected multiplier and target values are displayed in the mtgDisplay TextField, along with the current game count (gameCount).
- Strategy Button (btnStrat):
- When clicked, it runs an external process (likely a command to run a program) using Runtime.getRuntime().exec().
- The command it tries to run is: "D:/cygwin64/bin/magic.exe + multStr + targStr", where multStr and targStr are the selected multiplier and target, respectively.
- It also logs this action in a CSV file (bbjLog.csv) for each game and increments the game count.
- The console logs the results of this external process.
- Quit Button (btnQuit):
- When clicked, the application waits for a second and then closes the application using System.exit(0).
Miscellaneous:
- Game Count: Each time the "Get Strategy" button is clicked, the game count is incremented.
- Thread Sleep: The application waits for a second at various places, likely to ensure smooth execution of processes and actions.
Main Program Flow:
- The main method initializes and displays the GUI.
- The GUI includes components for selecting a multiplier and target, displaying the current game state, and interacting with external programs.
- Action listeners are used to handle button clicks, where one executes a strategy and the other quits the program.
Potential Use Case:
This application appears to be part of a game or simulation, where the user selects a multiplier and target, and a strategy is generated by an external process (magic.exe). The log of these selections and results is saved for later analysis. The interface is very basic and consists mostly of buttons and radio buttons.
Code:
package bbj;
import java.awt.*;
import java.awt.event.*; // Using AWT event classes and listener interfaces
import java.io.*;
import javax.swing.*;
import javax.swing.border.Border;
public class Gui {
int myMult = 2;
int myTarg = 12;
String multStr;
String targStr;
int gameCount = 1; // Counter's value
boolean loopThreadRunning = false;
TextField mtgDisplay; // Declare a TextField component
Button btnStrat; // Declare a Button component
Button btnQuit; // Declare a Button component
Label lblCount = new Label("Counter"); // construct the Label component
Font font = new Font("Arial", Font.BOLD, 20); // Change the font name, style
private void createGUI(String args[]) {
JFrame jf = new JFrame("Bingo BJ Multiplier and Target");
JPanel panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
panel.setLayout(layout);
jf.add(panel);
mtgDisplay = new TextField("Mult Targ Game #"); // TextField
mtgDisplay.setFont(new Font("Arial", Font.BOLD, 36));
mtgDisplay.setPreferredSize(new Dimension(10, 10));
mtgDisplay.setForeground(new Color(255, 0, 0));
mtgDisplay.setEditable(false); // set to read-only
btnStrat = new Button("Get Strategy"); // construct the Button component
btnStrat.setFont(font);
btnStrat.setForeground(new Color(0, 90, 90));
StratLstn Strat = new StratLstn();
btnStrat.addActionListener(Strat);
btnQuit = new Button("Quit"); // construct the Button component
btnQuit.setForeground(new Color(155, 0, 90));
btnQuit.setFont(font);
QuitLstn quit = new QuitLstn();
btnQuit.addActionListener(quit);
// Create the radio buttons
JPanel game = new JPanel(new GridLayout(7, 0));
JPanel mult = new JPanel(new GridLayout(6, 0));
JPanel targ = new JPanel(new GridLayout(10, 0));
JPanel cnt = new JPanel(new GridLayout(0, 5));
JPanel chooser = new JPanel();
// Arrange horizontally
chooser.setLayout(new BoxLayout(chooser, BoxLayout.X_AXIS));
game.add(btnStrat);
game.add(mtgDisplay);
game.add(btnQuit);
ActionListener mal = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton aButton = (AbstractButton) actionEvent.getSource();
try {
myMult = Integer.parseInt(aButton.getText());
} catch (NumberFormatException ex) {
System.out.println("Invalid number format: " + ex.getMessage());
}
// Convert int to String
mtgDisplay.setText(myMult + " " + myTarg + " Game # " + gameCount);
multStr = aButton.getText();
}
};
ActionListener tal = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton aButton = (AbstractButton) actionEvent.getSource();
try {
myTarg = Integer.parseInt(aButton.getText());
} catch (NumberFormatException ex) {
System.out.println("Invalid number format: " + ex.getMessage());
}
mtgDisplay.setText(myMult + " " + myTarg + " Game # " + gameCount);
targStr = aButton.getText();
}
};
String[] mArray = {"2", "5", "10", "25", "50", "100"};
ButtonGroup MBG = new ButtonGroup();
for (int i = 0; i < 6; i++) {
String val = String.valueOf(mArray[i]);
JRadioButton radio = new JRadioButton(val);
radio.setPreferredSize(new Dimension(30, 30));
radio.addActionListener(mal);
radio.setFont(font);
MBG.add(radio);
mult.add(radio);
}
ButtonGroup TBG = new ButtonGroup();
for (int i = 12; i < 22; i++) {
String val = String.valueOf(i);
JRadioButton radio = new JRadioButton(val);
radio.addActionListener(tal);
radio.setPreferredSize(new Dimension(10, 10));
radio.setFont(font);
TBG.add(radio);
targ.add(radio);
}
chooser.add(mult);
chooser.add(targ);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(chooser)
.addComponent(game)
.addComponent(cnt)
)
)
)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(chooser)
.addComponent(cnt)
.addComponent(game)
);
Border blackline = BorderFactory.createLineBorder(Color.BLUE);
mult.setBorder(blackline);
targ.setBorder(blackline);
jf.pack();
jf.setVisible(true);
}
// Define an inner class to handle the "Strat" button-click
private class StratLstn implements ActionListener {
// ActionEvent handler - Called back upon button-click.
@Override
public void actionPerformed(ActionEvent evt) {
String sort = "D:/cygwin64/bin/magic.exe + multStr + targStr";
try {
Runtime runtime = Runtime.getRuntime();
Process child = runtime.exec(sort);
InputStream chIn = child.getInputStream();
InputStreamReader isr = new InputStreamReader(chIn);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
Writer output = new BufferedWriter(new FileWriter("D:/bingo/bbjLog.csv", true));
output.append(multStr + "\t" + targStr + "\t" + gameCount + "\n");
output.close();
gameCount++;
} catch (IOException e) {
e.printStackTrace();
System.out.println("ReturnThread: " + e.getMessage());
}
System.out.println(sort);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// Define an inner class to handle the "Quit" button-click
private class QuitLstn implements ActionListener {
// ActionEvent handler - Called back upon button-click.
@Override
public void actionPerformed(ActionEvent evt) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(0);
}
}
public static void main(String args[]) throws IOException {
Gui decoder = new Gui();
decoder.createGUI(args);
}
}
Bookmarks