Results 1 to 3 of 3
  1. #1
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default drawString() method not working correctly

    Can anyone tell me why the drawString method isn't displaying when an incorrect answer is given to the problem in my applet?
    <code>
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;

    public class JAppletLearnMath2 extends JApplet implements ActionListener

    {
    int num1, num2;//variables to hold random numbers
    int answer, userGuess;//to hold user guess and correct product
    JTextField guess = new JTextField(3); // Holds user's answer
    JLabel question = new JLabel("Type your answer and press enter: "); // To hold result messages.
    String answerResponse;//for response to user's entry
    boolean changeQuestion;//to change question after correct answer
    Random ranNum = new Random();
    final int LIMIT = 10;//to set limit of random selection
    Container con = getContentPane();

    public void init()
    {
    con.setLayout(new FlowLayout());
    con.add(question);
    con.add(guess);
    guess.addActionListener(this);
    }// end init()

    public void paint(Graphics g)
    {
    super.paint(g);
    newQuestion();
    g.drawString(answerResponse, 50, 100);

    }//end paint

    public void actionPerformed(ActionEvent event)
    {
    userGuess = Integer.parseInt(guess.getText());//convert user answer to integer

    //If/else statements to display response to user's guess
    if(userGuess != answer)
    {
    answerResponse = ("No your guess of " + userGuess + " is not right. " +
    "Please try again.");
    guess.setText("");
    changeQuestion = false;
    }

    else
    {
    answerResponse = ("Very good! " + num1 + " times " + num2 +
    " is equal to " + answer + ".");
    guess.setText("");
    changeQuestion = true;
    }

    //Repaint if correct answer is guessed
    if(changeQuestion == true)
    {
    repaint();
    }
    }//end actionPerformed

    public void newQuestion()
    {
    num1 = (ranNum.nextInt(LIMIT) + 1);
    num2 = (ranNum.nextInt(LIMIT) + 1);
    answer = num1 * num2;
    showStatus("How much is " + num1 + " times " + num2 + "?");
    }//end newQuestion

    }//end class
    </code>

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    It looks like you're only calling repaint() if they get the question right, which is why the display won't update with the "You're wrong" answer. You'll have to call repaint whether they get the question right or wrong, so you'll have to work out the "did they get it right or wrong" conditional in the paint() method.

  3. #3
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Just save yourself a lot of trouble and use a Swing JLabel instead of Graphics.drawString() and repaint() and all that.

Similar Threads

  1. Error Checking not working correctly
    By RickAintree in forum New To Java
    Replies: 1
    Last Post: 12-15-2010, 01:54 PM
  2. Method Graphics.drawString() - Symbol not found
    By km0r3 in forum AWT / Swing
    Replies: 1
    Last Post: 08-27-2010, 02:55 PM
  3. My rotate 2d pos method isnt working correctly..
    By Addez in forum New To Java
    Replies: 5
    Last Post: 12-01-2009, 09:04 AM
  4. Gueesing Game Almost done, but not working correctly
    By mbnumba6 in forum New To Java
    Replies: 5
    Last Post: 03-18-2009, 03:01 AM
  5. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 04:45 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
  •