LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2003, 07:46 AM   #1
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Rep: Reputation: 32
g.drawString not working properly


My applet calculates two random numbers and lets the user insert a number. The computer checks whether the answer is correct or not, and then writes an appropriate string in the applet.

However, I have two problems:

a) I cannot call method startGame() from init(). Is that normal? If so, what can be done to call methods in the init() method?

b) When I call repaint(), the drawString does not correctly print the message I want. However, the System.out.println's just before repaint(), the same conditionals occur and the message is displayed correctly. I think that might be because repaint() is called too late once the variables' values have been changed? But I'm not sure

I would really appreciate if someone could help me. Thank you very much for reading this.

Code:
import java.awt.*; 
import java.applet.Applet; 


public class Operaciones extends Applet
{

	/* GUI Varibles:				* 
	 * 1. start Button: Starts game 		* 
	 * 2. answerText TextField: Inputs answer	*/
	Button start; 
	TextField answerText; 

	/* User interactive Variables: 			* 
	 * 1. response: Gets answer from answerText	*/
	int answer = 0; 

	/* Non-user  Variables: 			* 
	 * 1. numA: Generates a random number		* 
	 * 2. numB: Generates a random number		*
	 * 3. correct: Says if answer is correct or not */ 
	int numA; 
	int numB; 
	boolean correct = false; 

	boolean firstTime = true; 


	public void init()
 	{
		answerText = new TextField(10); 
		answerText.setEditable(false); 
	

		start = new Button("Start / New!"); 

		add (answerText); 
		add (start); 
	}
	
	public boolean action(Event e, Object o) 
	{

		
		if (e.target == answerText) 		/* User wants to check an answer  		 *
		 					 * methods called to verify existing answers 	 */
		{	
			repaint(); 

			answer = Integer.parseInt(o.toString()); 
			correct = checkGame(answer); 

		}
		else 					/* User wants to start a new game */ 
			startGame(); 

		
		return true; 

		
		
	}

	public void paint(Graphics g) 
	{

		if (correct & !(firstTime)) 
			g.drawString("Correct! You did it!", 25, 125); 
		else if (!(correct) & !(firstTime))
			g.drawString("Try again!", 25, 125); 
	
	}

	public void startGame()
	{
		numA = (int) (Math.random() * 100); 
		numB = (int) (Math.random() * 100); 

		showStatus("How much is " + numA + " times " + numB + " ?"); 
		correct = false; 
		answerText.setEditable(true); 
	}
		
	public boolean checkGame(int checkAnswer)
	{
		firstTime = false; 

		if (numA * numB == checkAnswer)
			correct = true;	

		if (correct & !(firstTime)) 
			System.out.println("Correct! You did it!"); 
		else if (!(correct) & !(firstTime))
			System.out.println("Try again!"); 
			
		repaint(); 
		try 
		{ 
			Thread.sleep(100000);
		}
		catch (InterruptedException e)
		{
		};  

	
		if (correct) 
			startGame(); 
		else 
			showStatus("ERROR: Try again. How much is " + numA + " times " + numB + "?"); 	

		return correct; 
	}
}
 
Old 10-17-2003, 07:57 AM   #2
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
Try making your startGame method static like this:

static public void startGame()

You should probably do this for your checkGame method too.

At the end of your paint method, do this:

super.paint(g);

Let me know if this helps.
 
Old 10-17-2003, 04:37 PM   #3
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Original Poster
Rep: Reputation: 32
No... the super.paint(g) doesn't solve the problem.

About changing the methods to static, my compiler says i cannot use the variables if I use static... should I pass them all as arguments? What would I win by doing so?
 
Old 10-18-2003, 12:29 PM   #4
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
I messed about and got it working - at least in the java applet viewer - never done much applets, it doesn't work in IE Explorer (yes stuck on windows at the moment) for reasons unknown to myself. I change it a wee bit but there ya go...could be more elegant....

Code:

import java.awt.*; 
import java.applet.Applet; 
import java.awt.event.*;

public class Operaciones extends Applet 
{

	/* GUI Varibles:				* 
	 * 1. start Button: Starts game 		* 
	 * 2. answerText TextField: Inputs answer	*/

        Button myButton; 
	TextField answerText; 

	/* User interactive Variables: 			* 
	 * 1. response: Gets answer from answerText	*/
	
         

	/* Non-user  Variables: 			* 
	 * 1. numA: Generates a random number		* 
	 * 2. numB: Generates a random number		*
	 * 3. correct: Says if answer is correct or not */ 
	
       int numA, numB,correctAnswer,userAnswer; 
       
       boolean correct=false; 

       boolean firstTime=true; 

       String message;
       String questionMessage;    	
   
   ActionListener buttListener=new ActionListener()
     {
      public void actionPerformed(ActionEvent ae)
        {
           if (myButton.getLabel()=="Start") 		
		{	
		  myButton.setLabel("Answer");
                  startGame(); 
                }
            else
                checkGame();
                       

                
          validate(); //Resize button if the label has changed

        }
       };  


      public void init()
 	{
		answerText = new TextField(10); 
		answerText.setEditable(false); 
	

		myButton = new Button("Answer"); 

                myButton.addActionListener(buttListener);

                answerText.setEditable(false);

		this.add(answerText); 

		this.add(myButton); 
               
                startGame();
               
                  
	    }
	


    private void checkGame()
      {

        boolean validNumber=true;

        firstTime=false;
      
        try
         {
           userAnswer = Integer.parseInt(answerText.getText());
          } catch(NumberFormatException nfe) 
                   { validNumber=false;}
           

        if( (validNumber) &  (userAnswer==correctAnswer))
         {

           myButton.setLabel("Start");
           correct=true;

         }           
        else
           {
            answerText.setText("");
            answerText.requestFocus(); 
           } 
       
        repaint();

       }


      private void startGame()
	{
		numA = (int) (Math.random() * 100); 
		numB = (int) (Math.random() * 100); 

                correctAnswer=numA * numB;

                 
                questionMessage=new String("How much is " + numA + " times " + numB + " ?");

 		
                firstTime=true;
                correct = false; 
		
                answerText.setText("");
                answerText.setEditable(true); 
                answerText.requestFocus();         	
                
                repaint();
                  

     }

     
     public void paint(Graphics g) 
	{
            super.paint(g);

            if(correct)  
              {  
               message=new String("Correct ! You did it !");
               questionMessage=new String("Click start to begin"); 
                       
              }
            else
             if(! correct && !firstTime) 
                message=new String("Wrong ! Try again !");
               else
            if(!correct && firstTime) 
               message=new String("");

            g.drawString(message,25,125);
                          
            g.drawString(questionMessage,25,175);		
	
	}

       



}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Superkaramba not working properly mickyg Linux - Software 5 11-07-2005 10:45 PM
Samba Not Working Properly linux-rulz Linux - Networking 4 04-14-2005 03:15 AM
X not working properly. MylesCLin Linux - Software 1 09-15-2004 10:46 AM
Is Kppp working properly? gmichel Linux - Newbie 0 01-13-2004 04:38 AM
virtusertable not working properly Manuel-H Linux - General 0 04-30-2003 08:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration