/**
* @author ShadeDarkan
*/
package Hangman.GUI;//The package this class belongs to
import Hangman.GameLogic;//Needed so we can evaluate the game, get new words, set and get player stats, etc
import java.awt.*;//Needed for many of the GUI elements we are using
import java.awt.event.ActionEvent;//Needed so we can make and use ActionEvent for our ActionListener
import java.awt.event.ActionListener;//Needed so we can tell when a button or menu item is clicked or changed
import javax.swing.*;//Needed for many of the GUI elements we are using
public class MainFrame extends JFrame
{
//Create global variables and objects
GameLogic gameLogic = new GameLogic();//Create a new GameLogic object so we can use its methods
private String sPlayerName = "";//We need this because the GUI is going to be getting the player's name and passing it to GameLogic
private String sGameStatus = "";//We need this to keep track of the game's win, lose or progressing status
//Create menu bars and items
private JMenuItem jmiNewGame = new JMenuItem("New Game");
private JMenuItem jmiChoosePlayer = new JMenuItem("Choose Player");
private JMenu jmDifficulty = new JMenu("Difficulty");
private JMenuItem jmiExit = new JMenuItem("Exit");
private JRadioButtonMenuItem jrbmiEasy = new JRadioButtonMenuItem("Easy", true);
private JRadioButtonMenuItem jrbmiMedium = new JRadioButtonMenuItem("Medium");
private JRadioButtonMenuItem jrbmiHard = new JRadioButtonMenuItem("Hard");
//Create stats and message text boxes
private JLabel jlDisplayWord = new JLabel();
private JTextArea jtaStats = new JTextArea();
private JTextArea jtaMessage = new JTextArea();
//Create the alphabet buttons
private JButton jbtA = new JButton("A");
private JButton jbtB = new JButton("B");
private JButton jbtC = new JButton("C");
private JButton jbtD = new JButton("D");
private JButton jbtE = new JButton("E");
private JButton jbtF = new JButton("F");
private JButton jbtG = new JButton("G");
private JButton jbtH = new JButton("H");
private JButton jbtI = new JButton("I");
private JButton jbtJ = new JButton("J");
private JButton jbtK = new JButton("K");
private JButton jbtL = new JButton("L");
private JButton jbtM = new JButton("M");
private JButton jbtN = new JButton("N");
private JButton jbtO = new JButton("O");
private JButton jbtP = new JButton("P");
private JButton jbtQ = new JButton("Q");
private JButton jbtR = new JButton("R");
private JButton jbtS = new JButton("S");
private JButton jbtT = new JButton("T");
private JButton jbtU = new JButton("U");
private JButton jbtV = new JButton("V");
private JButton jbtW = new JButton("W");
private JButton jbtX = new JButton("X");
private JButton jbtY = new JButton("Y");
private JButton jbtZ = new JButton("Z");
//Class constructor
public MainFrame()
{
//Create menu bar and items
JMenuBar jmbMenu = new JMenuBar();
JMenu jmGameOptions = new JMenu("Game Options");
//Create panel for GUI elements
JPanel jpMenu = new JPanel();
JPanel jpLabel = new JPanel();
JPanel jpImagesText = new JPanel();
JPanel jpImages = new JPanel();
JPanel jpText = new JPanel();
JPanel jpButtons = new JPanel();
//Create the button group for difficulties
ButtonGroup bgDifficulty = new ButtonGroup();
//Create dimension objects for use with JLabel to set a preferred size for the GUI element
Dimension dLabel = new Dimension();
Dimension dImagesText = new Dimension();
//Set the layout for panels
setLayout(new GridBagLayout());
jpMenu.setLayout(new FlowLayout(FlowLayout.LEFT));
jpLabel.setLayout(new FlowLayout(FlowLayout.CENTER));
jpImagesText.setLayout(new GridLayout(1,2,5,0));
jpText.setLayout(new GridLayout(2, 1, 0, 5));
jpButtons.setLayout(new GridLayout(2, 13, 0, 0));
//Set component properties
jtaStats.setEditable(false);
jtaStats.setLineWrap(true);
jtaMessage.setEditable(false);
jtaMessage.setLineWrap(true);
jlDisplayWord.setFont(jlDisplayWord.getFont().deriveFont(46.0f));
dLabel.height = 80;
dImagesText.height = 300;
jpLabel.setPreferredSize(dLabel);
jpImagesText.setPreferredSize(dImagesText);
//Add menu items to the menu
jmbMenu.add(jmGameOptions);
jmGameOptions.add(jmiNewGame);
jmGameOptions.add(jmiChoosePlayer);
jmGameOptions.add(jmDifficulty);
jmGameOptions.add(jmiExit);
jmDifficulty.add(jrbmiEasy);
jmDifficulty.add(jrbmiMedium);
jmDifficulty.add(jrbmiHard);
bgDifficulty.add(jrbmiEasy);
bgDifficulty.add(jrbmiMedium);
bgDifficulty.add(jrbmiHard);
//Create GUI constraints
GridBagConstraints cMenu = new GridBagConstraints();
cMenu.anchor = GridBagConstraints.LINE_START;
cMenu.gridwidth = GridBagConstraints.REMAINDER;
cMenu.gridx = 0;
cMenu.gridy = 0;
GridBagConstraints cLabel = new GridBagConstraints();
cLabel.fill = GridBagConstraints.BOTH;
cLabel.gridwidth = GridBagConstraints.REMAINDER;
cLabel.gridy = GridBagConstraints.RELATIVE;
GridBagConstraints cImagesText = new GridBagConstraints();
cImagesText.fill = GridBagConstraints.BOTH;
cImagesText.gridwidth = GridBagConstraints.REMAINDER;
cImagesText.gridy = GridBagConstraints.RELATIVE;
GridBagConstraints cButtons = new GridBagConstraints();
cButtons.fill = GridBagConstraints.BOTH;
cButtons.gridwidth = GridBagConstraints.REMAINDER;
cButtons.gridy = GridBagConstraints.RELATIVE;
//Add the panels to the MainFrame
this.add(jpMenu, cMenu);
jpMenu.add(jmbMenu);
this.add(jpLabel, cLabel);
jpLabel.add(jlDisplayWord);
this.add(jpImagesText, cImagesText);
jpImagesText.add(jpImages);
jpImagesText.add(jpText);
this.add(jpButtons, cButtons);
//Add the text areas to the panels
jpText.add(jtaStats);
jpText.add(jtaMessage);
//Add buttons to jpButtons panel
jpButtons.add(jbtA);
jpButtons.add(jbtB);
jpButtons.add(jbtC);
jpButtons.add(jbtD);
jpButtons.add(jbtE);
jpButtons.add(jbtF);
jpButtons.add(jbtG);
jpButtons.add(jbtH);
jpButtons.add(jbtI);
jpButtons.add(jbtJ);
jpButtons.add(jbtK);
jpButtons.add(jbtL);
jpButtons.add(jbtM);
jpButtons.add(jbtN);
jpButtons.add(jbtO);
jpButtons.add(jbtP);
jpButtons.add(jbtQ);
jpButtons.add(jbtR);
jpButtons.add(jbtS);
jpButtons.add(jbtT);
jpButtons.add(jbtU);
jpButtons.add(jbtV);
jpButtons.add(jbtW);
jpButtons.add(jbtX);
jpButtons.add(jbtY);
jpButtons.add(jbtZ);
//Create button action listeners
MenuListener menuListener = new MenuListener();
ButtonListener buttonListener = new ButtonListener();
//Register the action listener with the buttons
jmiNewGame.addActionListener(menuListener);
jmiChoosePlayer.addActionListener(menuListener);
jmDifficulty.addActionListener(menuListener);
jmiExit.addActionListener(menuListener);
jrbmiEasy.addActionListener(menuListener);
jrbmiMedium.addActionListener(menuListener);
jrbmiHard.addActionListener(menuListener);
jbtA.addActionListener(buttonListener);
jbtB.addActionListener(buttonListener);
jbtC.addActionListener(buttonListener);
jbtD.addActionListener(buttonListener);
jbtE.addActionListener(buttonListener);
jbtF.addActionListener(buttonListener);
jbtG.addActionListener(buttonListener);
jbtH.addActionListener(buttonListener);
jbtI.addActionListener(buttonListener);
jbtJ.addActionListener(buttonListener);
jbtK.addActionListener(buttonListener);
jbtL.addActionListener(buttonListener);
jbtM.addActionListener(buttonListener);
jbtN.addActionListener(buttonListener);
jbtO.addActionListener(buttonListener);
jbtP.addActionListener(buttonListener);
jbtQ.addActionListener(buttonListener);
jbtR.addActionListener(buttonListener);
jbtS.addActionListener(buttonListener);
jbtT.addActionListener(buttonListener);
jbtU.addActionListener(buttonListener);
jbtV.addActionListener(buttonListener);
jbtW.addActionListener(buttonListener);
jbtX.addActionListener(buttonListener);
jbtY.addActionListener(buttonListener);
jbtZ.addActionListener(buttonListener);
}
//Action listener for menu items
private class MenuListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String sMenuPressed = e.getActionCommand();//Get the item that was pressed or selected
//Evaluate the menu selection and take action if needed
if(sMenuPressed.contentEquals("New Game"))
{
if(sPlayerName.contentEquals(""))
{
setPlayerName();
}
startNewGame();
}
if(sMenuPressed.contentEquals("Choose Player"))
{
setPlayerName();
startNewGame();
}
if(sMenuPressed.contentEquals("Easy"))
{
if(sPlayerName.contentEquals(""))
{
setPlayerName();
}
startNewGame();
}
if(sMenuPressed.contentEquals("Medium"))
{
if(sPlayerName.contentEquals(""))
{
setPlayerName();
}
startNewGame();
}
if(sMenuPressed.contentEquals("Hard"))
{
if(sPlayerName.contentEquals(""))
{
setPlayerName();
}
startNewGame();
}
if(sMenuPressed.contentEquals("Exit"))
{
System.exit(0);
}
}
}
//Action listener for button items
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(sGameStatus.contentEquals("progressing"))
{
//Change action into a character
String sBtnPressed = e.getActionCommand();
char[] chaBtn = sBtnPressed.toCharArray();
char chBtn = chaBtn[0];
//Send action for button greyout
setIsEnabledFalse(chBtn);
//Send action to game logic for evaluation
gameLogic.EvaluateGuess(chBtn, e.getActionCommand());
jlDisplayWord.setText(gameLogic.getDisplayWord());
sGameStatus = gameLogic.EvaluateGame();
jtaMessage.setText((6 - gameLogic.getGuessesMade()) + " guesses left!");
evalGameStatus();
}
}
}
//Starts a new game
private void startNewGame()
{
if(jrbmiEasy.isSelected())
{
gameLogic.getNewGameWord(jrbmiEasy.getActionCommand());
jlDisplayWord.setText(gameLogic.getDisplayWord());
setStatsText();
jtaMessage.setText("6 guesses left!");
setIsEnabledTrue();
}
if(jrbmiMedium.isSelected())
{
gameLogic.getNewGameWord(jrbmiMedium.getActionCommand());
jlDisplayWord.setText(gameLogic.getDisplayWord());
setStatsText();
jtaMessage.setText("6 guesses left!");
setIsEnabledTrue();
}
if(jrbmiHard.isSelected())
{
gameLogic.getNewGameWord(jrbmiHard.getActionCommand());
jlDisplayWord.setText(gameLogic.getDisplayWord());
setStatsText();
jtaMessage.setText("6 guesses left!");
setIsEnabledTrue();
}
sGameStatus = "progressing";
}
//Evaluate game status
private void evalGameStatus()
{
if(sGameStatus.contentEquals("win") || sGameStatus.contentEquals("lose"))
{
if(jrbmiEasy.isSelected())
{
gameLogic.setPlayerStats(sPlayerName, sGameStatus,
jrbmiEasy.getActionCommand());
}
if(jrbmiMedium.isSelected())
{
gameLogic.setPlayerStats(sPlayerName, sGameStatus,
jrbmiMedium.getActionCommand());
}
if(jrbmiHard.isSelected())
{
gameLogic.setPlayerStats(sPlayerName, sGameStatus,
jrbmiHard.getActionCommand());
}
if(sGameStatus.contentEquals("win"))
{
jtaMessage.append("\nYOU WON!");
setStatsText();
}
if(sGameStatus.contentEquals("lose"))
{
jtaMessage.append("\nYOU LOST!");
setStatsText();
jlDisplayWord.setText(gameLogic.getRealWord());
}
}
}
//Sets the alphabet button pressed to not enabled
private void setIsEnabledFalse(char chBtn)
{
switch (chBtn)
{
case 'A': jbtA.setEnabled(false); break;
case 'B': jbtB.setEnabled(false); break;
case 'C': jbtC.setEnabled(false); break;
case 'D': jbtD.setEnabled(false); break;
case 'E': jbtE.setEnabled(false); break;
case 'F': jbtF.setEnabled(false); break;
case 'G': jbtG.setEnabled(false); break;
case 'H': jbtH.setEnabled(false); break;
case 'I': jbtI.setEnabled(false); break;
case 'J': jbtJ.setEnabled(false); break;
case 'K': jbtK.setEnabled(false); break;
case 'L': jbtL.setEnabled(false); break;
case 'M': jbtM.setEnabled(false); break;
case 'N': jbtN.setEnabled(false); break;
case 'O': jbtO.setEnabled(false); break;
case 'P': jbtP.setEnabled(false); break;
case 'Q': jbtQ.setEnabled(false); break;
case 'R': jbtR.setEnabled(false); break;
case 'S': jbtS.setEnabled(false); break;
case 'T': jbtT.setEnabled(false); break;
case 'U': jbtU.setEnabled(false); break;
case 'V': jbtV.setEnabled(false); break;
case 'W': jbtW.setEnabled(false); break;
case 'X': jbtX.setEnabled(false); break;
case 'Y': jbtY.setEnabled(false); break;
case 'Z': jbtZ.setEnabled(false); break;
}
}
//Sets the alphabet buttons back to enabled
private void setIsEnabledTrue()
{
jbtA.setEnabled(true); jbtN.setEnabled(true);
jbtB.setEnabled(true); jbtO.setEnabled(true);
jbtC.setEnabled(true); jbtP.setEnabled(true);
jbtD.setEnabled(true); jbtQ.setEnabled(true);
jbtE.setEnabled(true); jbtR.setEnabled(true);
jbtF.setEnabled(true); jbtS.setEnabled(true);
jbtG.setEnabled(true); jbtT.setEnabled(true);
jbtH.setEnabled(true); jbtU.setEnabled(true);
jbtI.setEnabled(true); jbtV.setEnabled(true);
jbtJ.setEnabled(true); jbtW.setEnabled(true);
jbtK.setEnabled(true); jbtX.setEnabled(true);
jbtL.setEnabled(true); jbtY.setEnabled(true);
jbtM.setEnabled(true); jbtZ.setEnabled(true);
}
//Set the player's name
private void setPlayerName()
{
sPlayerName = JOptionPane.showInputDialog(null, "Enter your name:",
"Player Name", JOptionPane.QUESTION_MESSAGE);
}
//Update stats text area
private void setStatsText()
{
int[] iaPStats = gameLogic.getPlayerStats(sPlayerName);
jtaStats.setText("");
jtaStats.append("Games played: " + iaPStats[0] + "\n");
jtaStats.append("Games won: " + iaPStats[1] + "\n");
if(iaPStats[0] == 0)
{
jtaStats.append("Win percentage: 0%" + "\n");
}
else
{
jtaStats.append("Win percentage: " + ((int)((double)iaPStats[1] / (double)iaPStats[0] * 100)) + "%" + "\n");
}
jtaStats.append("Games played on Easy: " + iaPStats[2] + "\n");
jtaStats.append("Games played on Medium: " + iaPStats[3] + "\n");
jtaStats.append("Games played on Hard: " + iaPStats[4]);
}
}