Results 1 to 9 of 9
  1. #1
    Ahdood is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Question Hangman Radio Button Problem

    I guess I don't really know how the radio buttons work, but i want to make a radio button that would allow the user to choose which level of difficulty they'd want to play the hangman game on. I don't want to copy the whole 300+ lines of code, but here a few lines..

    Java Code:
    public class HangmanPanel extends JPanel {
    // bunch of stuff in here. JButton, ImageIcon, etc.
    private JRadioButton easy, hard;
    public HangmanPanel() {
    // again a lot of stuff.
    
     add(wordLabel);
                    for (int letterIndex = 0; letterIndex < 6; letterIndex++) {
                            add(letters[letterIndex]);
                            System.out.print("- ");
                    }
     
                    add(photoLabel);
                    add(Box.createRigidArea(new Dimension(500, 0)));
                    add(guessr);
                    add(easy);
                    add(hard);
    I've tried a bunch of stuff, including putting the radio buttons in their own class that also extended HangmanPanel, etc. These are the errors I get..

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at HangmanPanel.<init>(HangmanPanel.java:143) [ where I do add(easy) ]
    at Hangman.main(Hangman.java:10) [HangmanPanel panel = new HangmanPanel(); that is the error line in the Hangman.java that sets up the frame.

    Anyone that could point me in the right direction that would be awesome.

  2. #2
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,760
    Rep Power
    7

    Default Re: Hangman Radio Button Problem

    It would appear the the easy variable is null. Why is it null?

  3. #3
    Ahdood is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Hangman Radio Button Problem

    OOH. I feel dumb.
    Java Code:
    easy = new JRadioButton("Easy");
    That made it work :). Now I currently have a dictionary class with an array that my game pulls when a new game is begun, How can I have it use a different dictionary if easy is selected?

    Would it be something like
    Java Code:
    if (easy.isSelected())
    {
    // use this dictionary
    }
    
    else
    {
    //other
    }
    I actually think I would need a listener of some sort, I was looking at a few examples and it seems they used a ClickListener, which i'm unfamiliar with. So it would just be Public Class Whatever implements ClickListener?

  4. #4
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,760
    Rep Power
    7

    Default Re: Hangman Radio Button Problem

    When does the game begin? The code to load the dictionary should be called after the user starts the game and before the word is chosen. Makes sense doesn't it?

  5. #5
    Ahdood is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Hangman Radio Button Problem

    The game begins as soon as the application is launched, and a random word is picked, obviously you can create a new game, etc. I can post the dictionary class that I am using with the hangman game,

    Java Code:
     import java.util.*;
    public class Dictionary {
    private Random rndm = new Random();
    private String word[] = {"PHONES", "HOUSES", "ACCORD", "JUGFUL", "KAYAKS",
    "NEVADA", "JOUSTS", "ETCHES", "FIZZED", "GREENS", "BETRAY",
    "QUARTZ", "BATMAN", "CASUAL", "DRIVER", "EJECTS", "KOALAS",
    "IMMUNE", "LAZILY", "MEDIUM", "NOTICE", "ORPHAN", "RHYTHM",
    "SCHOOL", "UNIONS" };
    private int wordIndex = rndm.nextInt(24);
    public Dictionary (){
    }
    public Dictionary(int num){
    word[wordIndex] = word[num];
    }
    public String getWord(){
    return word[wordIndex];
    }
    public void setWord(int x){
    x = rndm.nextInt(24);
    word[wordIndex] = word[x];
    }
    public void setRandomWord(int x){
    x = rndm.nextInt(24);
    word[wordIndex] = word[x];
    }
    public String toString(){
    return word[wordIndex];
    }
    }
    A few of the things in there are required for my class, overload constructor, default constructor, etc. Can I use an actionlistener for a radio button as well? Or would I use a switch statement? or what? ahh i'm confused lol.

  6. #6
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,760
    Rep Power
    7

    Default Re: Hangman Radio Button Problem

    You could add a Listener to the buttons but I don't think that would be a good idea. Imagine if the user clicks 'easy' you load the easy dictionary, then they click 'hard' you load the hard dictionary, they they click easy, click hard click easy over and over again and you load a dictionary everytime. Why not load the dictionary only once just before the games chooses the secret word. Once again, doesn't that make sense?

  7. #7
    Ahdood is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Hangman Radio Button Problem

    Well yes, I see what you are saying, and it makes sense. However, I would like them to be able to choose if they want to play using harder words. Initially, if they open the application, it will be preselected to the easy word dictionary. I would make it so, if they chose Hard, it would start a new game using that new dictionary and picking a word from that dictionary. Not sure how I will go about implementing that, but that should work should it not?

  8. #8
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,760
    Rep Power
    7

    Default Re: Hangman Radio Button Problem

    How I would do it is not start the game until the user clicks "START GAME".

  9. #9
    Ahdood is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Hangman Radio Button Problem

    Okay! thank you very much for all your feedback :)

Similar Threads

  1. Disable Radio button
    By AJG in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 11:09 AM
  2. Radio Button
    By S-NESH in forum New To Java
    Replies: 6
    Last Post: 02-04-2011, 05:03 PM
  3. Radio Button help!
    By javanator in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 08:01 PM
  4. Problem with Radio Button on JSP page (esp one row data)
    By DrMath in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-16-2010, 01:48 AM
  5. How to use SWT Radio Button
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:25 PM

Tags for this Thread

Posting Permissions

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