Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

need to make all methods work from paint(Graphics g) ?

erjcanerjcan Posts: 2Member
the code below generates a question on number multiplication (inside applet)
e.g. - "what is 5 times 6"

the user enters answer and clicks button - if he is right, "very good " is displayed and a new string (question) is shown
or "try again" is shown and he has to answer it again


this is a WORKING CODE, i only need to make generate_question() and check_answer() methods to get called from paint() method

I DON'T HAVE paint() coded yet - i m asking you guys how to do it?

paint() should look like this - paint(Graphics g) {......}


[code]
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;

public class ex_6_31 extends JApplet implements ActionListener{

public void check_answer(int a, int b, int answer){
if( answer == a * b) {
answerArea.setText("very good!") ;
generate_new_question() ;
}
else answerArea.setText("try again!") ;

}
public void generate_new_question(){
a = 1 + (int) (Math.random() * 9 ) ;
b = 1 + (int) (Math.random() * 9 ) ;
String question = "" ;
question += "What is " + a + " times " + b + " ?" ;
questionArea.setText(question) ;
}
int a ;
int b ;
JTextField answerField;
JButton answerButton ;
JTextArea questionArea ;
JTextArea answerArea ;
public void init(){
Container c = getContentPane() ;
c.setLayout(new FlowLayout()) ;

questionArea = new JTextArea() ;
questionArea.setEditable(false) ;
questionArea.setText("") ;
c.add(questionArea) ;

answerField = new JTextField(4) ;
c.add(answerField) ;

answerButton = new JButton("Answer!") ;
answerButton.addActionListener(this) ;
c.add(answerButton) ;

answerArea = new JTextArea() ;
answerArea.setEditable(false) ;
answerArea.setText("") ;
c.add(answerArea) ;

generate_new_question() ;
}
public void actionPerformed(ActionEvent e){
int result = Integer.parseInt(answerField.getText()) ;
check_answer(a, b, result) ;


}
}
[/code]
Sign In or Register to comment.