Your program shall implement three classes, GameField and ScoreBoard to represent the two windows, and a main class GameApplication. GameField and Score Board communicate both ways: the click on 'Random Coloring' in the Game Field window and the 'Next Player' click in the Score Board window effect both windows.
I can't figure out how to have my click on the 'Next Player' cause the 'Activate Buttons' button on my Game Field GUI to be enabled. The same is true for when I click on 'Random Coloring' and enable the 'Next Player' button on my Score Board GUI. Any help would be greatly appreciated.
Main method class
import javax.swing.*;
import java.awt.*;
public class GameApplication {
/**
* This class contains the main method.
* It gathers the probability input
*
*/
public static void main(String[] args) {
String input;
double p;
int p1Score, p2Score;
boolean correct = true;
do{
input = JOptionPane.showInputDialog(null, "Enter the probability"
+ " of yellow! Range[0, 0.3]", "input", JOptionPane.QUESTION_MESSAGE);
//check for valid input
if (input.equals(null)&& input == "")
System.exit(0);
try {
p = Double.parseDouble(input);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,"Wrong input program will exit.");
System.exit(0);
}
p = Double.parseDouble(input);
if(p >= 0 && p <= .3){
correct = false;
}
else correct = true;
}while(correct);
do{
input = JOptionPane.showInputDialog(null, "Enter the first player's "+
"starting score (range 0 - 1000)","Player 1 Score", JOptionPane.QUESTION_MESSAGE);
//check for valid input
try {
p1Score = Integer.parseInt(input);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,"Wrong input program will exit.");
System.exit(0);
}
p1Score = Integer.parseInt(input);
if(p1Score >= 0 && p1Score <= 1000){
correct = false;
}
else correct = true;
}while(correct);
do{
input = JOptionPane.showInputDialog(null, "Enter the second player's "+
"starting score (range 0 - 1000)", "Player 2 Score", JOptionPane.QUESTION_MESSAGE);
//check for valid input
try {
p2Score = Integer.parseInt(input);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,"Wrong input program will exit.");
System.exit(0);
}
p2Score = Integer.parseInt(input);
if(p2Score >= 0 && p2Score <= 1000){
correct = false;
}
else correct = true;
}while(correct);
ScoreBoard board = new ScoreBoard();
GameField field = new GameField();
field.setBoard(board);
board.setField(field);
board.setScore(p1Score, p2Score);
field.setYellow(p);
field.open();
board.open();
}
}
Game Field GUI Class
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;
public class GameField extends JFrame {
//class variables
private final int WINDOW_WIDTH = 500;
private final int WINDOW_HEIGHT = 256;
private double prob;
private int colorNumber1, colorNumber2;
private boolean match = true;
private boolean player = true;
private boolean buttonFunction = false;
private int round = 1;
//class components
public JButton activate = new JButton("Activate Buttons");
private JButton randomColor = new JButton("Random Coloring");
private JButton redButton = new JButton("Choose Red");
private JButton blueButton = new JButton("Choose Blue");
private JButton stopButton = new JButton("Stop the Game");
private JLabel rLabel = new JLabel("Match this field");
private JLabel bLabel = new JLabel("This is your color choice");
private JPanel randomPanel = new JPanel();
private JPanel buttonPanel = new JPanel();
//object to interact with ScoreBoard Class.
private ScoreBoard board;
public void setBoard(ScoreBoard board1) {
board1 = board;
}
public void setYellow(double p) {
prob = p;
}
public void open() {
//Set the title
setTitle("Game Field");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setBounds(333, 200, WINDOW_WIDTH, WINDOW_HEIGHT);
activate.setBorder(new LineBorder(Color.green, 3));
//Middle Panel
JPanel middlePanel = new JPanel();
randomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
randomPanel.setBorder(new LineBorder(Color.black, 3));
buttonPanel.setBorder(new LineBorder(Color.black, 3));
rLabel.setBackground(Color.WHITE);
rLabel.setOpaque(true);
rLabel.setSize(30, 40);
bLabel.setBackground(Color.WHITE);
bLabel.setOpaque(true);
randomPanel.add(rLabel);
buttonPanel.add(bLabel);
JPanel nButtonPanel = new JPanel();
nButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
nButtonPanel.add(randomColor);
nButtonPanel.add(redButton);
nButtonPanel.add(blueButton);
middlePanel.setLayout(new FlowLayout());
middlePanel.add(randomPanel);
middlePanel.add(buttonPanel);
//middlePanel.add(nButtonPanel);
//bottom Panel
JPanel bottomPanel = new JPanel();
JPanel sButtonPanel = new JPanel();
bottomPanel.setLayout(new BorderLayout());
sButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
sButtonPanel.add(stopButton);
bottomPanel.add(nButtonPanel, BorderLayout.NORTH);
bottomPanel.add(sButtonPanel, BorderLayout.SOUTH);
//build content pane
add(activate, BorderLayout.NORTH);
add(middlePanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
//disable all buttons on first open
activate.setEnabled(true);
randomColor.setEnabled(false);
blueButton.setEnabled(false);
redButton.setEnabled(false);
//set the button colors
blueButton.setForeground(Color.RED);
redButton.setForeground(Color.BLUE);
blueButton.setBackground(Color.BLUE);
redButton.setBackground(Color.RED);
// Register an event listener with all 5 buttons.
blueButton.addActionListener(new ButtonListener());
redButton.addActionListener(new ButtonListener());
randomColor.addActionListener(new ButtonListener());
activate.addActionListener(new ButtonListener());
stopButton.addActionListener(new ButtonListener());
pack();
setVisible(true);
}
//this private class contains of the classes button commands
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Get the action command.
String actionCommand = e.getActionCommand();
double randomVariable = Math.random();
// Determine which button was clicked.
if (actionCommand.equals("Choose Blue"))
{
buttonPanel.setBackground(Color.blue);
blueButton.setEnabled(false);
redButton.setEnabled(false);
randomColor.setEnabled(true);
colorNumber1 = 2;
}
else if (actionCommand.equals("Choose Red"))
{
buttonPanel.setBackground(Color.red);
blueButton.setEnabled(false);
redButton.setEnabled(false);
randomColor.setEnabled(true);
colorNumber1 = 3;
}
else if (actionCommand.equals("Stop the Game"))
{
JOptionPane.showMessageDialog(null,"Thanks for Playing!");
System.exit(0);
}
else if (actionCommand.equals("Random Coloring"))
{
if(randomVariable <= prob){
colorNumber2 = 1;
randomPanel.setBackground(Color.yellow);
}
else if (randomVariable > prob && randomVariable <= ((1 - prob)/2)){
colorNumber2 = 2;
randomPanel.setBackground(Color.blue);
}
else {
colorNumber2 = 3;
randomPanel.setBackground(Color.red);
}
buttonFunction = true;
randomColor.setEnabled(false);
board.nextPlayerBut.updateUI();
System.out.println(getbuttonFunction());
}
else if (actionCommand.equals("Activate Buttons"))
{
blueButton.setEnabled(true);
redButton.setEnabled(true);
reset();
}
}
}//end actionListener
//getter and setter methods to manipulate activate
public boolean getbuttonFunction(){
return buttonFunction;
}
//reset is used to reset the gamefield
public void reset(){
rLabel.setBackground(Color.WHITE);
bLabel.setBackground(Color.WHITE);
if (player == true){
player = false;
setTitle("Game Field Round " + round +
" --- First Player's Turn");
}
else if (player == false){
player = true;
setTitle("Game Field Round " + round +
" --- Second Player's Turn");
round++;
}
}
public boolean matchColor(){
if(colorNumber2 == 1){
match = false;
}
else if (colorNumber1==colorNumber2)
{
match = true;
}
else match = false;
//System.out.println(match);
//System.out.println(colorNumber1 + " " + colorNumber2);
return match;
}
public boolean getPlayer(){
return player;
}
}
Score Board GUI Class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.LineBorder;
public class ScoreBoard extends JFrame{
private final int WINDOW_WIDTH = 500;
private final int WINDOW_HEIGHT = 256;
private int p1Score, p2Score;
private String p1ScoreStr, p2ScoreStr;
JButton nextPlayerBut = new JButton("Next Player");
JTextField p1TotalTF = new JTextField();
JTextField p2TotalTF = new JTextField();
JTextField p1WagerTF = new JTextField();
JTextField p2WagerTF = new JTextField();
private GameField field;
public void setField(GameField field1) {
field = field1;
}
public void setScore(int x, int y)
{
p1Score = x;
p2Score = y;
System.out.println(p1Score);
p1ScoreStr = "" + p1Score;
p2ScoreStr = "" + p2Score;
System.out.println(p1ScoreStr);
}
public void open() {
//Set the title
setTitle("Score Board");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setLocation(300, 400);
//Top Panel
JPanel topPanel = new JPanel();
String labelStr = "Click 'Next player' then make your "
+ "bet and push 'Enter' on the keyboard";
JLabel topLabel = new JLabel(labelStr);
topLabel.setForeground(Color.BLUE);
topPanel.setBackground(Color.GREEN);
topPanel.add(topLabel);
//Middle Panel
JPanel middlePanel = new JPanel();
middlePanel.setLayout(new GridLayout(2, 4, 15, 15));
//4 labels for middle panel
JLabel p1Label = new JLabel("Player 1 wager:");
JLabel p2Label = new JLabel("Player wager:");
JLabel p1Total = new JLabel("Total:");
JLabel p2Total = new JLabel("Total:");
p1Label.setOpaque(true);
p2Label.setOpaque(true);
p1Total.setOpaque(true);
p2Total.setOpaque(true);
p1Label.setBackground(Color.red);
p2Label.setBackground(Color.blue);
p2Label.setForeground(Color.yellow);
p1Label.setBorder(new LineBorder(Color.black, 3));
p2Label.setBorder(new LineBorder(Color.black, 3));
p1Total.setBackground(Color.red);
p2Total.setBackground(Color.blue);
p2Total.setForeground(Color.yellow);
p2WagerTF.setBorder(new LineBorder(Color.black, 3));
p1TotalTF.setBorder(new LineBorder(Color.black, 3));
p2TotalTF.setBorder(new LineBorder(Color.black, 3));
middlePanel.add(p1Label);
middlePanel.add(p1WagerTF);
middlePanel.add(p1Total);
middlePanel.add(p1TotalTF);
middlePanel.add(p2Label);
middlePanel.add(p2WagerTF);
middlePanel.add(p2Total);
middlePanel.add(p2TotalTF);
//build bottom panel
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bottomPanel.add(nextPlayerBut);
//build main content pane
add(topPanel, BorderLayout.NORTH);
add(middlePanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
// Register an event listener with all 5 buttons.
/*
nextPlayerBut.addActionListener(new ButtonListener());
p1WagerTF.addActionListener(new TFListener());
p2WagerTF.addActionListener(new TFListener());
*/
System.out.println(field.getbuttonFunction());
p1TotalTF.setText(p1ScoreStr);
p2TotalTF.setText(p2ScoreStr);
nextPlayerBut.setEnabled(field.getbuttonFunction());
System.out.println(field.getbuttonFunction());
p1WagerTF.setEnabled(false);
p2WagerTF.setEnabled(false);
p1TotalTF.setEditable(false);
p2TotalTF.setEditable(false);
pack();
setVisible(true);
}
/*
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Get the action command.
String actionCommand = e.getActionCommand();
if(actionCommand.equals("Next Player")&&
field.getPlayer() == true){
p1WagerTF.setEnabled(true);
p1WagerTF.setBorder(new LineBorder(Color.black, 3));
}
else if(actionCommand.equals("Next Player")&&
field.getPlayer() == false){
p2WagerTF.setEnabled(true);
p2WagerTF.setBorder(new LineBorder(Color.black, 3));
}
}
}
private class TFListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Get the action command.
String actionCommand = e.getActionCommand();
if (actionCommand.equals("\n") &&
field.getPlayer() == true){
Integer.parseInt(p1WagerTF.getText());
}
}
}
*/
public int updateScore(int playerScore) {
int score;
score =+ playerScore;
return score;
}
}
Thank you

New Topic/Question
Reply




MultiQuote




|