I've gotten this applet to do some of what it should but I'm having trouble getting it to do the rest. It's a multiplication applet that asks the user a question such as "What is 6 times 7?", has a JTextField for the user's answer, and a JButton to check the answer. After the button is clicked, if the answer is wrong, it should draw the string "No. Please try again." and allow the user to try as many times as it takes for them to get it correct. If the answer is correct, it should draw the string "Very good!!!" AND reset the question with a new set of random numbers. So far it will recognize if the answer is right or wrong but if, for example, the user first puts in a wrong answer, it draws the appropriate string but when the user gets it right, it draws the appropriate string on top of the other string instead of replacing it AND it doesn't reset the question with new numbers. Can someone PLEASE help me figure out these two issues?
Here's my code:
import java.awt.*;
import java.awt.Graphics;
import java.lang.Object;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class NewJApplet extends JApplet implements ActionListener
{
public Graphics brush ;
Random rand = new Random();
int number1 = rand.nextInt(10);
int number2 = rand.nextInt(10);
JLabel question = new JLabel("What is " + number1 + " times " + number2 + "?");
JTextField answer = new JTextField(3);
JButton checkAnswer = new JButton("Check Answer");
Font font1 = new Font("Teen", Font.BOLD, 30);
Font font2 = new Font("Teen", Font.ITALIC, 36);
String right = "Very good!!!";
String wrong = "No. Please try again.";
Container con = getContentPane();
public void init()
{
setLayout(new FlowLayout());
con.setBackground(Color.BLUE);
question.setLocation(20, 20);
question.setFont(font1);
con.add(question);
answer.setLocation(20, 40);
con.add(answer);
checkAnswer.setLocation(20, 60);
con.add(checkAnswer);
checkAnswer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int ans = Integer.parseInt(answer.getText());
if(ans == number1 * number2)
{
answer.setText("");
Random rand = new Random();
int number1 = rand.nextInt(9) + 1;
int number2 = rand.nextInt(9) + 1;
brush = getGraphics();
brush.setFont(font2);
brush.drawString(right, 20, 120);
validate();
}
else
{
answer.setText("");
brush = getGraphics();
brush.setFont(font2);
brush.drawString(wrong, 20, 120);
validate();
}
}
}
);
}
@Override
public void actionPerformed(ActionEvent e)
{
answer.setText("");
Random rand = new Random();
int number1 = rand.nextInt(10);
int number2 = rand.nextInt(10);
}
}