[java] problem with GUI
hello, I''m having trouble with my applet. it is a simple mathematics learning program
when you click the enter button after placing the answer to the problem in a JTextfield the program doesn''t respond until the fifth click or so and then it operates correctly from there on. how do I fix it?
i call repaint if the answer is correct or if they have recieved 3 tries. if i put the correct answer
i have to click the button about 5 times for the first problem, in order for the program to function correctly
after the first time it works properly though
Here''s the source:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Problem extends JApplet
{
static int isCorrect = 1;
static long num1, num2;
static String answer, diff = "Easy";
static long realAns=1;
static String typeOfProblem = "Multiplication";
static JTextField ans;
static Font f;
static long oldn1, oldn2;
private JComboBox levelOfDiff;
private JComboBox typeProb;
private JButton enterButton;
private JPanel topSubPane;
private JPanel botSubPane;
private JLabel tL;
private JLabel dL;
static myCanvas p;
String levels[] = {"Easy", "Medium", "Hard"};
String types[] = {"Multiplication", "Division", "Addition", "Subtraction"};
public void init()
{
getContentPane().setLayout(new BorderLayout());
levelOfDiff = new JComboBox();
typeProb = new JComboBox();
topSubPane = new JPanel(new FlowLayout());
botSubPane = new JPanel(new FlowLayout());
ans = new JTextField("Place Answer Here! ");
p = new myCanvas();
f = new Font("Courier", Font.BOLD | Font.ITALIC ,72);
tL = new JLabel("Type Of Problems: ");
dL = new JLabel("Difficulty Level: ");
num1 = (long)(Math.random() * 10);
num2 = (long)(Math.random() * 10);
System.out.println(num1);
System.out.println(num2);
enterButton = new JButton("Enter");
for(int index=0; index < levels.length; index++)
levelOfDiff.addItem(levels[index]);
for(int index=0; index < types.length; index++)
typeProb.addItem(types[index]);
getContentPane().add(p, BorderLayout.CENTER);
getContentPane().add(topSubPane, BorderLayout.NORTH);
getContentPane().add(botSubPane, BorderLayout.SOUTH);
typeProb.addItemListener(new TypeP());
levelOfDiff.addItemListener(new DiffP());
enterButton.addActionListener(new Checker());
topSubPane.add(dL);
topSubPane.add(levelOfDiff);
topSubPane.add(tL);
topSubPane.add(typeProb);
botSubPane.add(enterButton);
botSubPane.add(ans);
levelOfDiff.setSize(20,20);
typeProb.setSize(20,20);
}
}
class myCanvas extends JPanel
{
public void paint(Graphics g)
{
Image buffer = createImage(getSize().width, getSize().height);
Graphics offScreen= buffer.getGraphics();
offScreen.setColor(Color.blue);
offScreen.setFont(Problem.f);
if(Problem.typeOfProblem.equals("Addition"))
{
offScreen.drawString(Problem.num1 + " + " + Problem.num2 + " = ", 10, 250);
Problem.realAns =Problem.num1 + Problem.num2;
}
else if(Problem.typeOfProblem.equals("Subtraction"))
{
if(Problem.num1 > Problem.num2)
{
offScreen.drawString(Problem.num1 + " - " + Problem.num2 + " = ", 10, 250);
Problem.realAns = Problem.num1 - Problem.num2;
}
else
{
offScreen.drawString(Problem.num2 + " - " + Problem.num1 + " = ", 10, 250);
Problem.realAns = Problem.num2 - Problem.num1;
}
}
else if(Problem.typeOfProblem.equals("Division"))
{
if(Problem.num1 > Problem.num2)
{
while(Problem.num1 % Problem.num2 !=0)
--Problem.num2;
offScreen.drawString(Problem.num1 + " / " + Problem.num2 + " = ", 10, 250);
Problem.realAns = Problem.num1 / Problem.num2;
}
else
{
while(Problem.num2 % Problem.num1 != 0)
--Problem.num1;
offScreen.drawString(Problem.num2 + " /" + Problem.num1 + " = ", 10, 250);
Problem.realAns = Problem.num2 / Problem.num1;
}
}
else if(Problem.typeOfProblem.equals("Multiplication"))
{
offScreen.drawString(Problem.num1 + " X " + Problem.num2 + " = ", 10, 250);
Problem.realAns = Problem.num1 * Problem.num2;
}
g.drawImage(buffer,0,0,this);
Problem.oldn1 = Problem.num1; Problem.oldn2 = Problem.num2;
}
}
class TypeP implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Problem.typeOfProblem = (String)e.getItem();
System.out.println(Problem.typeOfProblem);
Checker.changeNumbers(); Problem.p.repaint();
}
}
class DiffP implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Problem.diff = (String)e.getItem();
System.out.println(Problem.diff);
Problem.p.repaint();
}
}
class Checker implements ActionListener
{ public static void changeNumbers()
{
if(Problem.diff.equals("Easy"))
{
Problem.num1 = (long)(Math.random() * 10);
Problem.num2 = (long)(Math.random() * 10);
Checker.repeatChecker();
}
else if(Problem.diff.equals("Medium"))
{
Problem.num1 = (long)(Math.random() * 50);
Problem.num2 = (long)(Math.random() * 50);
Checker.repeatChecker();
}
else if(Problem.diff.equals("Hard"))
{
Problem.num1 = (long)(Math.random() * 250);
Problem.num2 = (long)(Math.random() * 250);
Checker.repeatChecker();
}
}
public static void repeatChecker()
{
if((Problem.oldn1 == Problem.num1) && (Problem.oldn2 == Problem.num2)) changeNumbers();
}
public void actionPerformed(ActionEvent e)
{
System.out.println(Problem.ans.getText());
Problem.answer = Problem.ans.getText();
if(Long.parseLong(Problem.answer) == Problem.realAns)
{
Problem.ans.setText("Correct");
changeNumbers();Problem.p.repaint(); Problem.isCorrect=1;
}
else if(Problem.isCorrect >= 3)
{
Problem.ans.setText("Wrong..."+ Problem.realAns + " Go Study Buddy! "); Problem.isCorrect=1; changeNumbers(); Problem.p.repaint();
}
else
{
Problem.ans.setText("Wrong... Try again!"); Problem.isCorrect++; Problem.p.repaint();
}
}
}
I just compiled and run your program and do not have the problems you describe.
I just enter the answer in the field and click enter and it says "correct!" in the field. No problem with that.
However, I see a few problems:
1. In your initialization of ans there is a trailing space. You code does not call Trim() on the result of input so this means that input that seems legal suddenly is illegal if the user forgets to delete this trailing space.
2. When you call parseLong() (I.e. enter is pressed) with a string that is bad, for instance, with a trailing space the NumberFormatException is thrown. This you do not catch and the effects is that the Enter button seems "frozen".
Hope this helps.
Jacob Marner
I just enter the answer in the field and click enter and it says "correct!" in the field. No problem with that.
However, I see a few problems:
1. In your initialization of ans there is a trailing space. You code does not call Trim() on the result of input so this means that input that seems legal suddenly is illegal if the user forgets to delete this trailing space.
2. When you call parseLong() (I.e. enter is pressed) with a string that is bad, for instance, with a trailing space the NumberFormatException is thrown. This you do not catch and the effects is that the Enter button seems "frozen".
Hope this helps.
Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement