Results 1 to 5 of 5
  1. #1
    Mikki is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Default Multiplication practice app

    Hi All,

    Can anyone p lease help me? I am writing this program which will help elementary students learn multiplication. The code works fine, but I am having problems with the layout. The JPanel and the string painted by g.drawString is overlapping. How can I fix it ?

    Here is the code I have attempted:

    //this program generates single digit multiplication problems for elementary students

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Multiply2 extends JFrame {

    //variables used in the program
    JPanel panel;
    JLabel status,prompt;
    JTextField question,input;
    String questionString;
    int correctAnswer,userInput;


    //setup graphical user interface components
    public Multiply2 ()
    {


    super ("Multiplication Practice for Kids");

    ActionEventHandler handler = new ActionEventHandler();

    //creat text fields and a label
    question = new JTextField(20);
    question.setEditable(false);

    status = new JLabel ("Are you ready to have fun with Multiplication",SwingConstants.CENTER);
    prompt = new JLabel("Enter your answer: ");

    input = new JTextField(4);
    input.addActionListener(handler);

    //add components to applet
    Container c = getContentPane();
    panel = new JPanel();
    c.setLayout (new FlowLayout());
    getContentPane().setBackground(Color.yellow);
    panel.setBackground (Color.BLUE);

    c.add(status);
    c.add(panel);
    panel.add(question);
    panel.add(prompt);
    panel.add(input);


    //generate a question
    createQuestion();

    }


    private class ActionEventHandler implements ActionListener
    {
    //verify the response entered by the user
    public void actionPerformed (ActionEvent e)
    {

    //get user input and convert in into integer
    userInput = Integer.parseInt(input.getText());

    //clear the textfield
    input.setText("");

    //display the correct response
    repaint();

    }

    }
    //show whether the answer was correct or not
    public void paint(Graphics g)

    {

    //to make sure that the children are painted
    super.paint(g);

    if (userInput != correctAnswer)

    g.drawString("No. Please try again.",20,70);

    else
    {

    g.drawString("Very Good!",20,70);
    createQuestion();

    }




    }


    //creat a new question and its corresponding answer
    public void createQuestion()
    {

    //get two random numbers between 0 and 9
    int digit1 = (int) (Math.random() * 10);
    int digit2 = (int) (Math.random() * 10);

    //correct answer after multiplying the two digits
    correctAnswer = digit1 * digit2;
    questionString = "How much is " + digit1 + " times " + digit2 + "?";

    //add to the applet
    question.setText(questionString);

    }

    //set the size of the window
    public static void main (String args[])
    {

    Multiply2 window = new Multiply2();

    window.addWindowListener
    (
    new WindowAdapter()
    {
    public void windowClosing (WindowEvent e)
    {

    System.exit (0);

    }

    }

    );

    window.setSize (500,200);

    window.show();

    }


    }//end of Multiply2 class

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Multiplication practice app

    Usually you're better off putting text into JLabels rather than painting them in paint(...) or paintComponent(...) as then you can use the layout managers to help you place your JLabels and avoid any overlap.

    Also, consider editing your post above and putting your code in [code] [/code] tags so that it will retain its formatting and be readable.

  3. #3
    Mikki is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Default Re: Multiplication practice app

    Hi,

    Thank you so much for your help. I tried it and but now the string which I wanted to "print" according to the "response" is not showing on the screen at all.
    What am I doing wrong?

    Java Code:
    //this program generates single digit multiplication problems for elementary students 
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Multiply2 extends JFrame {
    
    	//variables used in the program
    	JPanel panel;
    	JLabel status,prompt,response;
    	JTextField question,input;
    	String questionString;
    	int correctAnswer,userInput;
    
    
    	//setup graphical user interface components
        	public Multiply2 ()
                {
                    
    		
    		super ("Multiplication Practice for Kids");
    
    		ActionEventHandler handler = new ActionEventHandler();
    	
    		//creat text fields and a label
    		question = new JTextField(20);
    		question.setEditable(false);
    
    		status = new JLabel ("Are you ready to have fun with Multiplication",SwingConstants.CENTER);
    		prompt = new JLabel("Enter your answer: ");
    		response = new JLabel ();	
    
    		input = new JTextField(4);
    		input.addActionListener(handler);
    
    		//add components to applet
    		Container c = getContentPane();
    		panel = new JPanel();
    		c.setLayout (new FlowLayout());
    		getContentPane().setBackground(Color.yellow);
    		panel.setBackground (Color.BLUE);
    		
    		c.add(status);
    		c.add(panel);
    		panel.add(question);
    		panel.add(prompt);
    		panel.add(input);
    		panel.add(response);
    
    		
    		//generate a question
    		createQuestion();	
    
    	    }
    
        	
    	private class ActionEventHandler implements ActionListener
    		{	
    			//verify the response entered by the user
    			public void actionPerformed (ActionEvent e) 
    			{
    			
    				//get user input and convert in into integer
    				userInput = Integer.parseInt(input.getText());
    
    				//clear the textfield
    				input.setText("");
    
    				//display the correct response
    				//repaint();	
    				verifyAnswer();		
       
       	 		}
    
    		}
    	
    	//show whether the answer was correct or not
    	public void verifyAnswer()
    
    		{		
    
    			//to make sure that the children are painted
    			//super.paint(g);
    
    			if (userInput != correctAnswer)
    				
    				//g.drawString("No. Please try again.",20,70);
    				response = new JLabel ("No.Please try again.",SwingConstants.CENTER);
    	
    			else
    				{
    
    					//g.drawString("Very Good!",20,70);
    					response = new JLabel ("Very Good",SwingConstants.CENTER);
    					createQuestion();						
    	
    				}
    	
    		
    			
    		
    		}
         
       	
    	//creat a new question and its corresponding answer
    	public void createQuestion()
    		{
    			
    			//get two random numbers between 0 and 9
    			int digit1 = (int) (Math.random() * 10);
    			int digit2 = (int) (Math.random() * 10);
    
    			//correct answer after multiplying the two digits
    			correctAnswer = digit1 * digit2;
    			questionString = "How much is " + digit1 + " times " + digit2 + "?";
    
    			//add to the applet
    			question.setText(questionString); 		
    			
    		}
    
    	//set the size of the window
    	public static void main (String args[])
    		{
    
    			Multiply2 window = new Multiply2();
    
    			window.addWindowListener
    			(
    				new WindowAdapter()
    				{
    					public void windowClosing (WindowEvent e)
    					{
    
    						System.exit (0);
    
    					}
    
    				}
    	
    			);
    
    			window.setSize (500,200);
    		
    			window.show();		
    
    		}
     
        
    }//end of Multiply2 class

  4. #4
    jfabian is offline Member
    Join Date
    Jun 2012
    Posts
    22
    Rep Power
    0

    Default Re: Multiplication practice app

    Hi Miki,

    The problems is in line 89 and 95
    Java Code:
      //response = new JLabel ("No.Please try again.",SwingConstants.CENTER);
      //response = new JLabel ("Very Good",SwingConstants.CENTER);
    You already create the object in line 32
    Java Code:
    response = new JLabel();
    So instead of creating again the object, you just need to set the result to the JLabel, like this:

    Java Code:
        public void verifyAnswer()
     
            {      
     
                //to make sure that the children are painted
                //super.paint(g);
     
                if (userInput != correctAnswer)
    
                	response.setText("No.Please try again.");
         
                else
                    {
     
                        response.setText("Very Good");
                        createQuestion();                      
         
                    }
             
             
            }

    It should work properly now
    Last edited by jfabian; 06-24-2012 at 11:17 PM.

  5. #5
    Mikki is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Default Re: Multiplication practice app

    Great it works. Thank you so much.

Similar Threads

  1. int array multiplication
    By sternhagenr in forum New To Java
    Replies: 10
    Last Post: 08-20-2011, 01:41 AM
  2. need help with multiplication
    By dakid2 in forum New To Java
    Replies: 10
    Last Post: 03-08-2011, 03:41 AM
  3. Multiplication Table
    By BillyB in forum New To Java
    Replies: 17
    Last Post: 12-24-2010, 06:58 AM
  4. Multiplication Table
    By SwEeTAcTioN in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 04:11 AM
  5. Help with Multiplication
    By phil028 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 07:39 PM

Posting Permissions

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