Results 1 to 7 of 7
Thread: Help! Anagram Games
- 10-13-2008, 03:04 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 1
- Rep Power
- 0
Help! Anagram Games
hi all! Now i'm doing my work to create simple anagram game using java. I found some difficulties in my coding i think. The "submit" button doesnt work. I already tried to find what the mistake is, but i found nothing. Hope you guys can help me. Thx b4. :)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EasyMode extends JFrame implements ActionListener{
String Q[] = {"Question 1", "Question 2", "Question 3", "Question 4"};
String Question[] = {" A J A V ", " O T O B ", " I B M T P A ", " L O O P "};
String Answer[] = {"JAVA","BOOT","BITMAP","POOL"};
String Clues[] = {"A programming language expressly designed for use in the distributed environment of the Internet.", "Protective footgear, as of leather or rubber, covering the foot and part or all of the leg.", "A set of bits that represents a graphic image, with each bit or group of bits corresponding to a pixel in the image.", "A small body of still water."};
int element1 = 0, element2 = 0;
int i = 0;
int score = 0;
JLabel L1 = new JLabel(Q[element1]);
JLabel L2 = new JLabel(Question[element2]);
JLabel L3 = new JLabel();
JLabel L4 = new JLabel();
JLabel L5 = new JLabel("Easy Level");
JButton clues = new JButton("Clues");
JButton next = new JButton("Submit");
JTextField tf = new JTextField("",15);
EasyMode(){
setSize(325,325);
setLocation(50,50);
setTitle("Anagram Quiz-Easy Mode");
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
L1.setBounds(30,50,100,10);
add(L1);
L2.setBounds(50,100,150,10);
add(L2);
L3.setBounds(30,265,250,30);
add(L3);
L4.setBounds(30,285,270,30);
add(L4);
clues.setBounds(40,180,80,30);
clues.addActionListener(this);
add(clues);
next.setBounds(140,180,100,30);
next.addActionListener(this);
add(next);
tf.setBounds(50,120,100,25);
tf.addActionListener(this);
add(tf);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if(obj==clues){
JOptionPane.showMessageDialog(this,(Clues[i]),"The Clue", JOptionPane.INFORMATION_MESSAGE);
i++;
if((obj==next)&&(element1<3)&&(element2<3)){
L1.setText(Q[++element1]);
L2.setText(Question[++element2]);
tf.setText("");
}
else if((obj==next)&&(element1>=3)&&(element2>=3)){
JOptionPane.showMessageDialog(this, "Congratulations, you have finished the easy mode game. Please be ready for the next Level!", "Message", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
L3.setText("");
if(((JTextField)obj).getText().equals(Answer[element2])){
L3.setText("Your Answer is CORRECT!");
}
else{
L3.setText("Your answer is WRONG!");
}
L4.setText("");
if(((JTextField)obj).getText().equals(Answer[element2])){
score+=25;
L4.setText("Your current score is:"+score);
}
else
score-=5;
L4.setText("Your current score is:"+score);
}
}
public static void main (String args[]){
EasyMode game = new EasyMode();
}
}
-
A few things that you can do to help any of us help you:
1) When posting code, please use code tags so your code maintains its formatting and is readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. You may want to click on the Preview tab to make sure that your code is formatted correctly. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:
Java Code:[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
As it is, I see that you are getting the source of from the ActionEvent object -- which is the button that was pushed -- and trying to get the text out of it, which is of course impossible. You need to call getText on your JTextField, but also not when the Clue button is pressed but only when the Submit button is pressed. Remember that in a project like this, it is best to divide and conquer: solve small problems one at a time in small programs, then combine into a large program when the issues have been solved.
You've a bit of work to do. Good luck.
- 10-13-2008, 03:23 AM #3The "submit" button doesnt work.
For example: Is the listener being called?
What object created the ActionEvent?
What if() statements are found to be true?
Re the above reference to casting the obj object. That will only work if obj == tf. But if obj == tf, then use tf.getText() vs casting obj to JTextField.Last edited by Norm; 10-13-2008 at 03:28 AM.
- 10-13-2008, 09:12 AM #4Java Code:
public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Clues")) { //do some job } }
- 10-13-2008, 03:20 PM #5
e.getActionCommand().equals("Clues"))
This style of coding doesn't let the compiler help you find errors.
By coding the literal separately in two or more places, there is a chance that they can be different. A better approach is to use a constant (final) to define the String and then use that constant in your code.
final String CluesStr = "Clues";
...
setCommand(CluesStr); // Not real method??
...
e.getActionCommand().equals(CluesStr))
Also if you decide you want to change "Clues" to "The clue", there is only one place to change and it works.
With the independent usage of a literal, you can misspell it or miss a usage and the compiler will NOT complain.
- 10-13-2008, 04:47 PM #6
Yes Norm you are right,i wanted to show him only the main idea,but you are completely right.
- 10-13-2008, 05:16 PM #7
There are different opinions on how to detect which object created the ActionEvent. I prefer the method the OP shown. IE is this the object by using the ==. Using the contents of an object (getActionCommand) can be ambiguous. It is possible for two objects to have the same content. Not likely but possible.
Similar Threads
-
problem for games
By Gunther in forum Java 2DReplies: 4Last Post: 12-25-2007, 09:42 AM -
Browser-based games. Where do I start?
By Brute in forum EclipseReplies: 1Last Post: 08-09-2007, 08:35 PM -
Recursive Anagram
By zoe in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 07:15 AM -
OS 10.4 - Help with Java in Yahoo! Games - Fonts Unreadable?
By acleme1 in forum Java AppletsReplies: 3Last Post: 07-16-2007, 12:22 PM
Bookmarks