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..
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.
Re: Hangman Radio Button Problem
It would appear the the easy variable is null. Why is it null?
Re: Hangman Radio Button Problem
OOH. I feel dumb. 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
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?
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?
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,
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.
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?
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?
Re: Hangman Radio Button Problem
How I would do it is not start the game until the user clicks "START GAME".
Re: Hangman Radio Button Problem
Okay! thank you very much for all your feedback :)