Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID


We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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) {......}

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) ;

Sign In or Register to comment.