Results 1 to 6 of 6
Thread: Hangman GUI help
- 05-20-2009, 02:33 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
Hangman GUI help
Hi, I'm currently working on a hangman GUI game for my assignment. The structure of my game goes as follows:
Basically, when I run the program, a main panel appears (HangmanPanel) with 3 other sub-panels inside: red panel (mainPanel), green panel (guessPanel), white panel (keyPanel). There's one last panel which is draw hangman panel but I haven't gotten around to that yet, I may not add it. My problem is implementing the game logic into this program. Upon pressing "New Game" button, the program will enter the startGame() method, but I'm not sure how to transfer values of the hint and answers to the guessPanel.
This is the file where it displays the GUI. I want to implement my game logic into this file.
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class HangmanPanel extends JPanel { // Constants for fonts private final static Font STD_FONT = new Font("Arial", Font.BOLD, 16); private final static Font BIG_FONT = new Font("Arial", Font.BOLD, 20); private String[] wordAns = {"APPLE", "ORANGE", "MANGO", "MOTORCYCLE", "LORRY", "SOFA", "TABLE", "CHAIR", "SPOON", "FORK", "BLUE", "BROWN", "YELLOW", "TIGER", "ELEPHANT"}; private String[] wordHint = {"FRUIT", "FRUIT", "FRUIT", "VEHICLE", "VEHICLE", "FURNITURE", "FURNITURE", "FURNITURE", "EATING UTENSIL", "EATING UTENSIL", "COLOUR", "COLOUR", "COLOUR", "ANIMAL", "ANIMAL"}; // Number of letters private final static int NUM_LETTERS = 26; // Colors for selection private Color UNSELECTED_COLOR = Color.BLACK; private Color SELECTED_COLOR = Color.BLUE; // GUI components for panel ButtonListener listener; JPanel mainPanel, guessPanel, keyPanel, drawPanel; private JButton newGameBtn; private JButton quitBtn; private JButton [] letterBtns; private JButton selectedBtn; private JLabel guessHint; private static String word; public HangmanPanel() { selectedBtn = null; listener = new ButtonListener(); setBackground (Color.blue); setPreferredSize (new Dimension(800, 500)); setLayout (new GridLayout (2, 2, 5, 10)); mainPanel = createMainPanel(); guessPanel = createGuessPanel(); keyPanel = createKeyPanel(); //drawPanel = createDrawPanel(); add (mainPanel); add (guessPanel); add (keyPanel); } private JPanel createMainPanel() { JPanel panel = new JPanel(); panel.setPreferredSize (new Dimension(300, 300)); panel.setBackground (Color.red); panel.setBorder (BorderFactory.createLineBorder(Color.black, 3)); //panel.setLayout (new BoxLayout (panel, BoxLayout.Y_AXIS)); panel.setLayout (null); JLabel welcomeLbl = new JLabel ("Welcome to Hangman Game!"); welcomeLbl.setFont (STD_FONT); welcomeLbl.setLocation (70, 20); welcomeLbl.setSize (250, 20); welcomeLbl.setHorizontalAlignment(0); newGameBtn = new JButton ("New Game"); newGameBtn.setLocation (150, 75); newGameBtn.setSize (newGameBtn.getPreferredSize()); newGameBtn.addActionListener (listener); quitBtn = new JButton ("Quit Game"); quitBtn.setSize (quitBtn.getPreferredSize()); quitBtn.setLocation (150, 125); quitBtn.addActionListener (listener); panel.add (newGameBtn); panel.add (Box.createRigidArea (new Dimension (0, 20))); panel.add (quitBtn); panel.add (welcomeLbl); return panel; } /*private createDrawPanel() { JPanel panel = new JPanel(); panel.set }*/ private JPanel createGuessPanel() { JPanel panel = new JPanel(); panel.setLayout (null); panel.setPreferredSize (new Dimension(300, 300)); panel.setLocation (250, 0); panel.setBackground (Color.green); panel.setBorder (BorderFactory.createLineBorder(Color.black, 3)); return panel; } private JPanel createKeyPanel() { letterBtns = new JButton[NUM_LETTERS]; int letter = 65; JPanel panel = new JPanel(); panel.setLayout (new GridLayout (4, 7, 5, 10)); panel.setPreferredSize (new Dimension(600, 300)); panel.setBackground (Color.white); panel.setBorder (BorderFactory.createLineBorder(Color.black, 3)); panel.setBorder (BorderFactory.createTitledBorder("Keyboard")); for (int i = 0; i < NUM_LETTERS; i++) { char c = (char)letter; letterBtns[i] = new JButton ("" + c); letterBtns[i].setFont (STD_FONT); panel.add (letterBtns[i]); letterBtns[i].addActionListener (listener); letter++; } setKeyboard(); return panel; } public void setKeyboard() { for (int i = 0; i < NUM_LETTERS; i++) letterBtns[i].setEnabled (false); } public String startGame() { int j = 0; Random generator = new Random(); int num = generator.nextInt(j); word = wordAns[num]; char[] temp = word.toCharArray(); for (int k = 1; k < word.length(); k++ ) { temp[k] = '_'; } } private class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent event) { Object source = event.getSource(); if (source == newGameBtn) { for (int i = 0; i < NUM_LETTERS; i++) { letterBtns[i].setEnabled (true); } startGame(); } } } }
This is the file that I run to start the program:
Java Code:import javax.swing.*; public class HangmanGame extends JFrame { public HangmanGame() { setTitle ("Hangman Game"); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // create a main panel add (new HangmanPanel()); setLocation (300, 100); pack(); setVisible(true); } public static void main (String[] args) { JFrame frame = new HangmanGame(); } }Last edited by kurt; 05-20-2009 at 02:39 PM.
- 05-21-2009, 01:36 AM #2
It's good design to keep game logic and GUI as separate as possible. You call code in response to events by registering various EventHandlers on the GUI components, and you can update the GUI from other threads using SwingUtilities.invokeLater()
Any other specific questions?Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-21-2009, 02:01 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
I'm not sure how to separate game logic from GUI. I already have problems implementing the logic into the same class as the GUI. All I want to know is how to pass values from my game logic (in this case, wordHint[num] and wordAns[num] from startGame() method to createGuessPanel)
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.io.*; public class HangmanPanel extends JPanel { // Constants for fonts private final static Font STD_FONT = new Font("Arial", Font.BOLD, 16); private final static Font BIG_FONT = new Font("Arial", Font.BOLD, 20); private String[] wordAns; private String[] wordHint; Scanner myScanner = null; String aLine = null; private String word; char[] temp; int num; // Number of letters private final static int NUM_LETTERS = 26; // Colors for selection private Color UNSELECTED_COLOR = Color.BLACK; private Color SELECTED_COLOR = Color.BLUE; // GUI components for panel ButtonListener listener; JPanel mainPanel, guessPanel, keyPanel, drawPanel; private JButton newGameBtn; private JButton quitBtn; private JButton [] letterBtns; private JButton selectedBtn; private JLabel hintLbl; private JLabel wordLbl; public HangmanPanel() { selectedBtn = null; listener = new ButtonListener(); setBackground (Color.blue); setPreferredSize (new Dimension(800, 500)); setLayout (new GridLayout (2, 2, 5, 10)); mainPanel = createMainPanel(); guessPanel = createGuessPanel(); keyPanel = createKeyPanel(); //drawPanel = createDrawPanel(); add (mainPanel); add (guessPanel); add (keyPanel); } private JPanel createMainPanel() { JPanel panel = new JPanel(); panel.setPreferredSize (new Dimension(300, 300)); panel.setBackground (Color.red); panel.setBorder (BorderFactory.createLineBorder(Color.black, 3)); //panel.setLayout (new BoxLayout (panel, BoxLayout.Y_AXIS)); panel.setLayout (null); JLabel welcomeLbl = new JLabel ("Welcome to Hangman Game!"); welcomeLbl.setFont (STD_FONT); welcomeLbl.setLocation (70, 20); welcomeLbl.setSize (250, 20); welcomeLbl.setHorizontalAlignment(0); newGameBtn = new JButton ("New Game"); newGameBtn.setLocation (150, 75); newGameBtn.setSize (newGameBtn.getPreferredSize()); newGameBtn.addActionListener (listener); quitBtn = new JButton ("Quit Game"); quitBtn.setSize (quitBtn.getPreferredSize()); quitBtn.setLocation (150, 125); quitBtn.addActionListener (listener); panel.add (newGameBtn); panel.add (Box.createRigidArea (new Dimension (0, 20))); panel.add (quitBtn); panel.add (welcomeLbl); return panel; } [B]private JPanel createGuessPanel()[/B] { JPanel panel = new JPanel(); panel.setLayout (null); panel.setPreferredSize (new Dimension(300, 300)); panel.setLocation (250, 0); panel.setBackground (Color.green); panel.setBorder (BorderFactory.createLineBorder(Color.black, 3)); hintLbl = new JLabel(); [B]hintLbl.setText("Hint: " + wordHint[num]);[/B] // not working hintLbl.setLocation (70, 20); hintLbl.setSize (250, 20); panel.add (hintLbl); return panel; } private JPanel createKeyPanel() { letterBtns = new JButton[NUM_LETTERS]; int letter = 65; JPanel panel = new JPanel(); panel.setLayout (new GridLayout (4, 7, 5, 10)); panel.setPreferredSize (new Dimension(600, 300)); panel.setBackground (Color.white); panel.setBorder (BorderFactory.createLineBorder(Color.black, 3)); panel.setBorder (BorderFactory.createTitledBorder("Keyboard")); for (int i = 0; i < NUM_LETTERS; i++) { char c = (char)letter; letterBtns[i] = new JButton ("" + c); letterBtns[i].setFont (STD_FONT); panel.add (letterBtns[i]); letterBtns[i].addActionListener (listener); letter++; } setKeyboard(); return panel; } public void setKeyboard() { for (int i = 0; i < NUM_LETTERS; i++) letterBtns[i].setEnabled (false); } [B]public void startGame()[/B] { int i = 0; try { myScanner = new Scanner (new File("words.txt")); // Loop until all words have been read from the file while (myScanner.hasNextLine()) { // read a line from file aLine = myScanner.nextLine(); // Split the data into two parts as words and hints String[] data = aLine.split(";"); [B]wordAns[i] = data[0]; wordHint[i] = data[1];[/B] i++; } // Randomize and display a hint from the data Random generator = new Random(); [B]num = generator.nextInt(i);[/B] // Convert the word to be guessed into a char array word = wordAns[num]; temp = word.toCharArray(); } catch (FileNotFoundException e) { System.out.println ("File is not found!"); } finally { if (myScanner != null) { myScanner.close(); } } } private class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent event) { Object source = event.getSource(); if (source == newGameBtn) { for (int i = 0; i < NUM_LETTERS; i++) { letterBtns[i].setEnabled (true); } startGame(); } } } }
- 05-21-2009, 10:12 PM #4
As method arguments or shared fields.
What does "not working" mean? Note that createGuessPanel() is called only once, long before startGame() can execute.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-22-2009, 06:18 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
I'm now currently using file reading to obtain the words and hints from there to the program. But I'm not sure how to display it in the guessPanel. I'm using startGame() method to separate the words and hints and then randomize a set of word and hint when it is called, but I have no idea how to pass these information to the panel I want and display them as labels. I tried setting text from hintLbl (the label that displays the hint) on startGame() method, but there's no effect. hintLbl is added in the createGuessPanel(). Note that I haven't even started on the word to be displayed once game starts, just only the hint.
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.io.*; public class HangmanPanel extends JPanel { // Constants for fonts private final static Font STD_FONT = new Font("Arial", Font.BOLD, 16); private final static Font BIG_FONT = new Font("Arial", Font.BOLD, 20); private String[] wordAns; private String[] wordHint; Scanner myScanner = null; String aLine = null; private String hint; private String word; char[] temp; int num; // Number of letters private final static int NUM_LETTERS = 26; // Colors for selection private Color UNSELECTED_COLOR = Color.BLACK; private Color SELECTED_COLOR = Color.BLUE; // GUI components for panel ButtonListener listener; JPanel mainPanel, guessPanel, keyPanel, drawPanel; private JButton newGameBtn; private JButton quitBtn; private JButton [] letterBtns; private JButton selectedBtn; private JLabel hintLbl; private JLabel wordLbl; public HangmanPanel() { selectedBtn = null; listener = new ButtonListener(); setBackground (Color.blue); setPreferredSize (new Dimension(800, 500)); setLayout (new GridLayout (2, 2, 5, 10)); mainPanel = createMainPanel(); guessPanel = createGuessPanel(); keyPanel = createKeyPanel(); //drawPanel = createDrawPanel(); add (mainPanel); add (guessPanel); add (keyPanel); } private JPanel createMainPanel() { JPanel panel = new JPanel(); panel.setPreferredSize (new Dimension(300, 300)); panel.setBackground (Color.red); panel.setBorder (BorderFactory.createLineBorder(Color.black, 3)); //panel.setLayout (new BoxLayout (panel, BoxLayout.Y_AXIS)); panel.setLayout (null); JLabel welcomeLbl = new JLabel ("Welcome to Hangman Game!"); welcomeLbl.setFont (STD_FONT); welcomeLbl.setLocation (70, 20); welcomeLbl.setSize (250, 20); welcomeLbl.setHorizontalAlignment(0); newGameBtn = new JButton ("New Game"); newGameBtn.setLocation (150, 75); newGameBtn.setSize (newGameBtn.getPreferredSize()); newGameBtn.addActionListener (listener); quitBtn = new JButton ("Quit Game"); quitBtn.setSize (quitBtn.getPreferredSize()); quitBtn.setLocation (150, 125); quitBtn.addActionListener (listener); panel.add (newGameBtn); panel.add (Box.createRigidArea (new Dimension (0, 20))); panel.add (quitBtn); panel.add (welcomeLbl); return panel; } /*private createDrawPanel() { JPanel panel = new JPanel(); panel.set }*/ private JPanel createGuessPanel() { JPanel panel = new JPanel(); panel.setLayout (null); panel.setPreferredSize (new Dimension(300, 300)); panel.setLocation (250, 0); panel.setBackground (Color.green); panel.setBorder (BorderFactory.createLineBorder(Color.black, 3)); hintLbl = new JLabel(); hintLbl.setLocation (70, 20); hintLbl.setSize (250, 20); panel.add (hintLbl); return panel; } private JPanel createKeyPanel() { letterBtns = new JButton[NUM_LETTERS]; int letter = 65; JPanel panel = new JPanel(); panel.setLayout (new GridLayout (4, 7, 5, 10)); panel.setPreferredSize (new Dimension(600, 300)); panel.setBackground (Color.white); panel.setBorder (BorderFactory.createLineBorder(Color.black, 3)); panel.setBorder (BorderFactory.createTitledBorder("Keyboard")); for (int i = 0; i < NUM_LETTERS; i++) { char c = (char)letter; letterBtns[i] = new JButton ("" + c); letterBtns[i].setFont (STD_FONT); panel.add (letterBtns[i]); letterBtns[i].addActionListener (listener); letter++; } setKeyboard(); return panel; } public void setKeyboard() { for (int i = 0; i < NUM_LETTERS; i++) letterBtns[i].setEnabled (false); } public void startGame() { int i = 0; try { myScanner = new Scanner (new File("words.txt")); // Loop until all words have been read from the file while (myScanner.hasNextLine()) { // read a line from file aLine = myScanner.nextLine(); // Split the data into two parts as words and hints String[] data = aLine.split(";"); wordAns[i] = data[0]; wordHint[i] = data[1]; i++; } // Randomize and display a hint from the data Random generator = new Random(); num = generator.nextInt(i); // Convert the word to be guessed into a char array hint = wordHint[num]; word = wordAns[num]; temp = word.toCharArray(); hintLbl.setText ("Hint: " + hint); } catch (FileNotFoundException e) { System.out.println ("File is not found!"); } finally { if (myScanner != null) { myScanner.close(); } } } private class ButtonListener implements ActionListener { public void actionPerformed (ActionEvent event) { Object source = event.getSource(); if (source == newGameBtn) { for (int i = 0; i < NUM_LETTERS; i++) { letterBtns[i].setEnabled (true); } startGame(); } } } }
- 05-22-2009, 10:22 AM #6
You'll need to call repaint() on the panel after changing the label.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
Similar Threads
-
Hangman Game..
By iPetey in forum New To JavaReplies: 4Last Post: 05-07-2009, 02:24 PM -
Need help with hangman game
By kurt in forum New To JavaReplies: 4Last Post: 04-25-2009, 06:47 PM -
Need help with Hangman!!!
By chinasome in forum New To JavaReplies: 10Last Post: 11-09-2008, 04:42 AM -
Hangman Help!!!
By chinasome in forum New To JavaReplies: 5Last Post: 11-08-2008, 02:30 AM -
Hangman Game
By L23 in forum New To JavaReplies: 8Last Post: 07-03-2008, 01:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks