Results 1 to 4 of 4
  1. #1
    napoleonrokz is offline Newbies
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Question Need Help Starting GUI for Hang Man (JFrames)

    So I'm creating a Hang Man game in java. I have the hang man mechanics working in the console. It will select a word from a list and allow you to guess and all that good stuff. Now I'm trying to give it graphics per say. I'm having trouble understanding how to set up it's GUI.

    First, here's the code thus far, including my attempt to add a JFrame:

    Java Code:
    //Hang Man Game
    
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class HangMan
    {
    	private String word;
    	private StringBuffer blanks;
    	
    	public void launchFrame()
    	{
    		JFrame frame = new JFrame("Hang Man");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.pack();
    		frame.setVisible(true);
    	}
    	
    	private boolean showLetters(char guess)
    	{
    		boolean found = false;
    		for(int i = 0; i < word.length(); i++)
    		{
    			if(word.charAt(i) == guess)
    			{
    				blanks.setCharAt(i, guess);
    				found = true;
    			}
    		}
    		return found;
    	}
    	
    	
    	private void doGameLoop(int misses)
    	{
    		blanks = new StringBuffer();
    		for (int i = 0; i< word.length(); i++)
    			blanks.append("-");
    		StringBuffer missed = new StringBuffer();
    		
    		Scanner input =  new Scanner(System.in);
    		
    		//Start Game
    		while(!word.equals(blanks.toString()))
    		{
    			System.out.println("");
    			System.out.println("missed: " + missed);
    			System.out.println(blanks);
    			String line = input.nextLine();
    			if(line.length() < 1)
    				continue;
    			char guess = line.charAt(0);
    			
    			boolean found = showLetters(guess);
    			if(!found)
    			{
    				missed.append(guess);
    				misses--;
    				if(misses == 0)
    				{
    					System.out.println("");
    					System.out.println(word);
    					System.out.println("you suck");
    					return;
    				}
    			}
    		}
    		
    		System.out.println("");
    		System.out.println(blanks);
    		System.out.println("you got lucky");
    		
    	}
    	
    	public HangMan(String filename) throws FileNotFoundException
    	{
    		System.out.println("using wordlist: " + filename);
    		
    		//reading wordlist
    		Scanner input = new Scanner(new File(filename));
    		Vector<String> wordlist = new Vector<String>();
    		
    		while(input.hasNextLine())
    			wordlist.add(input.nextLine());
    			
    		System.out.println("wordlist count: " + wordlist.size());
    		
    		//randomly choose word from wordlist
    		int index = (int)(Math.random() * (double)wordlist.size());
    		word = wordlist.get(index);
    		System.out.println("word choosen:     " + word);
    	}
    	
    	public static void main(String args[]) throws FileNotFoundException
    	{
    		//get wordlist filename
    		if(args.length < 1)
    		{
    			System.err.println("ERROR: wordlist stupid!!");
    			return;
    		}
    		
    		HangMan app = new HangMan(args[0]);
    		app.doGameLoop(7);
    		app.launchFrame();
    	}
    }
    This compiles just fine, but I can't get the frame to open. This is what I want help with. After I figure that out I'll move on to getting the mechanics inside it. Don't help me too far, I'm trying to learn as I go. I just want the frame to appear. I'll bump this thread if I need more help.

    I'll explain further if needed.

    Also, I'm new here. I apologize if this is the wrong forum for this thread.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,444
    Rep Power
    25

    Default

    but I can't get the frame to open.
    Is the launchFrame method called?

  3. #3
    napoleonrokz is offline Newbies
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Sorry it took me o long to get back. Good news though I found the solution to my original problem. The program was hitting the loop before the frame, and wouldn't launch it. Now I have another problem I'm trying to work through.

    I'm tying to get it to display the blanks and then alter those blanks after a guess is found. This works in the console but I can't figure a way to display it within the frame and panel. I've been playing with JLabel and JTextArea, and while I can get it to display the things like 'misses:' I can't get it to display the blanks. Also I'm going to need this t be multiple lined so it will appear:

    misses:
    -------- *dashes are un-guessed letters in word*

    When I use the label or text area with blanks it gives me this compiling error:

    setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.StringBuffer)

    textArea.setText(blanks);
    *points at the dot*

    What's the best way to do this?

    Here is my code so far:

    Java Code:
    //Hang Man Game
    
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class HangMan
    {
    	private String word;
    	private StringBuffer blanks;
    	//JLabel label = new JLabel();
    	JTextArea textArea = new JTextArea();
    
    	
    	public void launchInterface()
    	{
    		JFrame frame = new JFrame("Hang Man");
    		JPanel panel = new JPanel();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(new BorderLayout());
    		frame.setSize(500, 500);
    		frame.setVisible(true);
    		
    		textArea.setEditable(false);  
    		textArea.setCursor(null);  
    		textArea.setOpaque(false);  
    		textArea.setFocusable(false);  
    		textArea.setText("Testng text area");
    
    		frame.add(panel);
    		//panel.add(label);
    		panel.add(textArea);
    	}
    	
    		
    	private boolean showLetters(char guess)
    	{
    		boolean found = false;
    		for(int i = 0; i < word.length(); i++)
    		{
    			if(word.charAt(i) == guess)
    			{
    				blanks.setCharAt(i, guess);
    				found = true;
    			}
    		}
    		return found;
    	}
    	
    	
    	private void doGameLoop(int misses)
    	{
    		blanks = new StringBuffer();
    		for (int i = 0; i< word.length(); i++)
    			blanks.append("-");
    		StringBuffer missed = new StringBuffer();
    		
    		Scanner input =  new Scanner(System.in);
    		
    		//Start Game
    		while(!word.equals(blanks.toString()))
    		{
    			System.out.println("");
    			System.out.println("missed: " + missed);
    			//label.setText("missed: " + missed);
    			//textArea.setText("missed: " + missed);
    			System.out.println(blanks);
    			//textArea("" + blanks);
    			String line = input.nextLine();
    			if(line.length() < 1)
    				continue;
    			char guess = line.charAt(0);
    			
    			boolean found = showLetters(guess);
    			if(!found)
    			{
    				missed.append(guess);
    				misses--;
    				if(misses == 0)
    				{
    					System.out.println("");
    					System.out.println(word);
    					System.out.println("you suck");
    					//label.setText("you suck");
    					return;
    				}
    			}
    		}
    		
    		System.out.println("");
    		System.out.println(blanks);
    		System.out.println("you got lucky");
    		//label.setText("you got lucky");
    		
    		
    	}
    	
    	public HangMan(String filename) throws FileNotFoundException
    	{
    		System.out.println("using wordlist: " + filename);
    		//label.setText("wordlist: " + filename);
    		
    		//reading wordlist
    		Scanner input = new Scanner(new File(filename));
    		Vector<String> wordlist = new Vector<String>();
    		
    		while(input.hasNextLine())
    			wordlist.add(input.nextLine());
    			
    		System.out.println("wordlist count: " + wordlist.size());
    		//label.setText("wordlist count: " + wordlist.size());
    		
    		//randomly choose word from wordlist
    		int index = (int)(Math.random() * (double)wordlist.size());
    		word = wordlist.get(index);
    		System.out.println("word choosen:     " + word);
    		//label.setText("word choosen:     " + word);
    	}
    	
    	public static void main(String args[]) throws FileNotFoundException
    	{
    		//get wordlist filename
    		if(args.length < 1)
    		{
    			System.err.println("ERROR: wordlist stupid!!");
    			return;
    		}
    		
    		HangMan app = new HangMan(args[0]);
    		app.launchInterface();
    		app.doGameLoop(7);
    	}
    }

  4. #4
    napoleonrokz is offline Newbies
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Ummm, I'm not sure if my last reply went through. It's not appearing for me but that could be because of my privilege settings which I don't understand yet. So this could be a double post, I'm sorry if that's so.

    Sorry it took me o long to get back. Good news though I found the solution to my original problem. The program was hitting the loop before the frame, and wouldn't launch it. Now I have another problem I'm trying to work through.

    I'm tying to get it to display the blanks and then alter those blanks after a guess is found. This works in the console but I can't figure a way to display it within the frame and panel. I've been playing with JLabel and JTextArea, and while I can get it to display the things like 'misses:' I can't get it to display the blanks. Also I'm going to need this t be multiple lined so it will appear:

    misses:
    -------- *dashes are un-guessed letters in word*

    When I use the label or text area with blanks it gives me this compiling error:

    setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (java.lang.StringBffer)

    textArea.setText(blanks);
    ^

    What's the best way to do this?

    Here is my code so far:

    Java Code:
    //Hang Man Game
    
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class HangMan
    {
    	private String word;
    	private StringBuffer blanks;
    	//JLabel label = new JLabel();
    	JTextArea textArea = new JTextArea();
    
    	
    	public void launchInterface()
    	{
    		JFrame frame = new JFrame("Hang Man");
    		JPanel panel = new JPanel();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(new BorderLayout());
    		frame.setSize(500, 500);
    		frame.setVisible(true);
    		
    		textArea.setEditable(false);  
    		textArea.setCursor(null);  
    		textArea.setOpaque(false);  
    		textArea.setFocusable(false);  
    		textArea.setText("Testng text area");
    
    		frame.add(panel);
    		//panel.add(label);
    		panel.add(textArea);
    	}
    	
    		
    	private boolean showLetters(char guess)
    	{
    		boolean found = false;
    		for(int i = 0; i < word.length(); i++)
    		{
    			if(word.charAt(i) == guess)
    			{
    				blanks.setCharAt(i, guess);
    				found = true;
    			}
    		}
    		return found;
    	}
    	
    	
    	private void doGameLoop(int misses)
    	{
    		blanks = new StringBuffer();
    		for (int i = 0; i< word.length(); i++)
    			blanks.append("-");
    		StringBuffer missed = new StringBuffer();
    		
    		Scanner input =  new Scanner(System.in);
    		
    		//Start Game
    		while(!word.equals(blanks.toString()))
    		{
    			System.out.println("");
    			System.out.println("missed: " + missed);
    			//label.setText("missed: " + missed);
    			//textArea.setText("missed: " + missed);
    			System.out.println(blanks);
    			//textArea("" + blanks);
    			String line = input.nextLine();
    			if(line.length() < 1)
    				continue;
    			char guess = line.charAt(0);
    			
    			boolean found = showLetters(guess);
    			if(!found)
    			{
    				missed.append(guess);
    				misses--;
    				if(misses == 0)
    				{
    					System.out.println("");
    					System.out.println(word);
    					System.out.println("you suck");
    					//label.setText("you suck");
    					return;
    				}
    			}
    		}
    		
    		System.out.println("");
    		System.out.println(blanks);
    		System.out.println("you got lucky");
    		//label.setText("you got lucky");
    		
    		
    	}
    	
    	public HangMan(String filename) throws FileNotFoundException
    	{
    		System.out.println("using wordlist: " + filename);
    		//label.setText("wordlist: " + filename);
    		
    		//reading wordlist
    		Scanner input = new Scanner(new File(filename));
    		Vector<String> wordlist = new Vector<String>();
    		
    		while(input.hasNextLine())
    			wordlist.add(input.nextLine());
    			
    		System.out.println("wordlist count: " + wordlist.size());
    		//label.setText("wordlist count: " + wordlist.size());
    		
    		//randomly choose word from wordlist
    		int index = (int)(Math.random() * (double)wordlist.size());
    		word = wordlist.get(index);
    		System.out.println("word choosen:     " + word);
    		//label.setText("word choosen:     " + word);
    	}
    	
    	public static void main(String args[]) throws FileNotFoundException
    	{
    		//get wordlist filename
    		if(args.length < 1)
    		{
    			System.err.println("ERROR: wordlist stupid!!");
    			return;
    		}
    		
    		HangMan app = new HangMan(args[0]);
    		app.launchInterface();
    		app.doGameLoop(7);
    	}
    }

Similar Threads

  1. Doesn't anyone here know how to help? I feel like I wanna hang myself
    By I Want To Kill Java in forum Advanced Java
    Replies: 25
    Last Post: 12-29-2010, 10:29 AM
  2. What type of hang? and how to fix?
    By joshbrigade in forum New To Java
    Replies: 2
    Last Post: 11-06-2009, 10:40 PM
  3. Read XML from Internet Hang!
    By yupingliew in forum Networking
    Replies: 0
    Last Post: 10-22-2009, 05:56 AM
  4. Applet hang
    By Worlock in forum New To Java
    Replies: 2
    Last Post: 08-10-2009, 06:43 AM
  5. Web application hang without comm.jar
    By chankokchern in forum Java Applets
    Replies: 0
    Last Post: 12-16-2008, 12:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •