Results 1 to 7 of 7
  1. #1
    geeky123 is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    Default 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();
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]
    2) You need to describe in greater detail what exactly is wrong. Are you getting any error messages? Is the program logic not working?

    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.

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

    Default

    The "submit" button doesnt work.
    Try debugging your code by Adding some println()s to the code to see where the flow is going.
    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 02:28 AM.

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Java Code:
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Clues")) {
          //do some job
        }
      }
    the same for other JButtons

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

    Default

    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.

  6. #6
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Yes Norm you are right,i wanted to show him only the main idea,but you are completely right.

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

    Default

    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

  1. problem for games
    By Gunther in forum Java 2D
    Replies: 4
    Last Post: 12-25-2007, 08:42 AM
  2. Browser-based games. Where do I start?
    By Brute in forum Eclipse
    Replies: 1
    Last Post: 08-09-2007, 07:35 PM
  3. Recursive Anagram
    By zoe in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:15 AM
  4. Replies: 3
    Last Post: 07-16-2007, 11:22 AM

Posting Permissions

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