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

    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?
    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());
    }// end init()

    public void paint(Graphics g)
    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.");
    changeQuestion = false;

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

    //Repaint if correct answer is guessed
    if(changeQuestion == true)
    }//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

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


    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
    Rep Power


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

Similar Threads

  1. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 8
    Last Post: 10-18-2017, 09:29 AM
  2. Error Checking not working correctly
    By RickAintree in forum New To Java
    Replies: 1
    Last Post: 12-15-2010, 01:54 PM
  3. Method Graphics.drawString() - Symbol not found
    By km0r3 in forum AWT / Swing
    Replies: 1
    Last Post: 08-27-2010, 02:55 PM
  4. 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
  5. 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

Posting Permissions

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