Graphics(g) problem and Compile problem, help :)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Graphics(g) problem and Compile problem, help :)

  1. #1
    Join Date
    Jul 2004
    Posts
    8

    Question Graphics(g) problem and Compile problem, help :)

    For some reason this code still does not run right when i compile it. It starts and then overloads and breaks and quits...??? This is kinda frustrating but im thanking all ya mates for helping through this

    bill c (*worried but willfull)




    Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.lang.Math;
    import java.awt.Graphics;
    
    public class GuessMe extends JApplet implements ActionListener {
    
    	JLabel lguess;
    	JTextField tguess; 
      int random, tries=0;
    	public void init()
    	{
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		lguess = new JLabel( "Guess a number between 1 and 1000!" ); // label the variable
    		c.add( lguess ); 
    		tguess = new JTextField(5);
    		c.add(tguess);
    		tguess.setText("");
        
        
    //	register tguess as ActionListener
     	tguess.addActionListener( this );
        random = mystery();
        
    	}
    	//when action pereformed brackets
    	public void actionPerformed(ActionEvent e)
    	{
    		++tries;
    		
    		repaint();
    	}
    	
    	
    	
    	public void paint(Graphics g)
    	{
    		
    			int guess;
    			
    			
    			g.drawString("You guessed " + tries, 70, 180);
    			
    			
    			guess = Integer.parseInt(tguess.getText() );
    			
    			if (guess < random)
    		          {
    				showStatus("Too low!, Try again.");
    		          tguess.setText("");
    		          
    		          }
    			else if (guess > random)
    		          {
    				showStatus("Too high! Try again.");
    		        tguess.setText("");
    		          
    		          }
    			else
    				  {
    				showStatus("Congratulations. You guessed the number! ");
    				winnermsg();
    				  }
    							
    			
    			
    	}
    
    	public void winnermsg()
    	
    	{
    		Graphics g = getGraphics();
    		
    			if (tries < 10)
    				
    				g.drawString("Either you know the secret or you got lucky!", 70, 185);
    			if (tries == 10)
    				
    				g.drawString("Ahah! You know the secret!",70, 185);
    			
    			if (tries > 10)
    				
    				g.drawString("You should be able to do better!", 70, 185);
    			
    		
    	}
    
    	//random number generator
    	public int mystery()
    
    	{
    
      int n = (int)(Math.random()* 1000); 
    
      return n;
    
    	}
    
    
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    you should not create your own graphics object! I know cma said you should on codeguru, but the only graphics object there should be is the one that gets passed to the paint method. This should then be passed (as a parameter) to any of the methods that need to use it.

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    yeah.. balls to cma! lol.. quick.. lets say some more stuff about him now he cant see us

    i'd go with mike though.. the java documentation itsel advocates that you do not create your own graphics contexts.. they are always created for you by any panel that is capable of showing on screen... draw on that, and you draw on the panel.. draw on your own, and you'll have the devils own job of getting your drawing on screen.. because none of the common GUI panes allow you to set the graphics in use, to your own object.. you must use the provided one
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  4. #4
    Join Date
    Feb 2004
    Posts
    541
    lol, he said the same thing as me all of his own accord anyway so we'll give him the benefit of the doubt

  5. #5
    Join Date
    Jul 2004
    Posts
    8

    Unhappy

    i gotcah mikebarr81 but its not comiling right, it acting non normal when i compile it, got any suggestions for compiling error on this thing?

    thanks bill


    Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.lang.Math;
    import java.awt.Graphics;
    
    public class GuessMe extends JApplet implements ActionListener {
    
    	JLabel lguess;
    	JTextField tguess; 
    	int random, tries=0;
    	public void init()
    	{
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		lguess = new JLabel( "Guess a number between 1 and 1000!" ); // label the variable
    		c.add( lguess ); 
    		tguess = new JTextField(5);
    		c.add(tguess);
    		tguess.setText("");
      
      
    //	register tguess as ActionListener
    	tguess.addActionListener( this );
      random = mystery();
      
    	}
    	//when action pereformed brackets
    	public void actionPerformed(ActionEvent e)
    	{
    		++tries;
    		
    		repaint();
    	}
    	
    	
    	
    	public void paint(Graphics g)
    	{
    		
    			int guess;
    			
    			
    			g.drawString("You guessed " + tries, 70, 180);
    			
    			
    			
    			guess = Integer.parseInt(tguess.getText() );
    			
    			if (guess < random)
    		          {
    				showStatus("Too low!, Try again.");
    		          tguess.setText("");
    		          
    		          }
    			else if (guess > random)
    		          {
    				showStatus("Too high! Try again.");
    		        tguess.setText("");
    		          
    		          }
    			else
    				  {
    				showStatus("Congratulations. You guessed the number! ");
    				winnermsg(g);
    				  }
    							
    			
    			
    	}
    
    	public void winnermsg(Graphics g)
    	
    	{
    		
    		
    			if (tries < 10)
    				
    				g.drawString("Either you know the secret or you got lucky!", 70, 185);
    			if (tries == 10)
    				
    				g.drawString("Ahah! You know the secret!",70, 185);
    			
    			if (tries > 10)
    				
    				g.drawString("You should be able to do better!", 70, 185);
    			
    		
    	}
    
    	//random number generator
    	public int mystery()
    
    	{
    
    int n = (int)(Math.random()* 1000); 
    
    return n;
    
    	}
    
    
    }

  6. #6
    Join Date
    Feb 2004
    Posts
    541
    The problem is that you're checking what the input is everytime the applet paints. The applet paints itself all the time, not only when the user enters numbers in the text. When you first start it it paints, when you move it it paints, when you move something over it it also paints. You should have an actionPerformed method to listen for the user pressing enter in the textbox or something, and when the actionPerformed method fires you then determine what their guess is. Then you can call repaint, and the paint method will decide what needs to be displayed.

  7. #7
    Join Date
    Jul 2004
    Posts
    8
    OK this is what i have now...

    1)-how do i get g.drawstring to be reconized in my winnermsg() method? I don't want to put in in my Graphics (g) becasue it will print out everytime.

    2)-how do i get my: g.drawString("You guessed this many times:" + tries, 70, 180);

    to stop writing on top of each other, the number stack on each other. Im not sure how to clear it.

    thanks again...
    bill


    Code:
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.lang.Math;
    import java.awt.Graphics;
    
    public class TEST extends JApplet implements ActionListener {
    
    	JLabel lguess;
    	JTextField tguess; 
    	int random, tries=0;
    	public void init()
    	{
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		lguess = new JLabel( "Guess a number between 1 and 1000!" ); // label the variable
    		c.add( lguess ); 
    		tguess = new JTextField(5);
    		c.add(tguess);
    		tguess.setText("");
      
      
    //	register tguess as ActionListener
    	tguess.addActionListener( this );
        random = mystery();
      
    	}
    	//when action pereformed brackets
    	public void actionPerformed(ActionEvent e)
    	{
    		++tries;
    
    
    		int guess;
    	
    		guess = Integer.parseInt(tguess.getText() );
    	
    		
    		repaint();
    	
    	if (guess < random)
              {
    		showStatus("Too low!, Try again.");
              tguess.setText("");
              
              }
    	else if (guess > random)
              {
    		showStatus("Too high! Try again.");
            tguess.setText("");
              
              }
    	else
    		  {
    		showStatus("Congratulations. You guessed the number! ");
    		
    		winnermsg();
    		
    		  }
    					
    	}
    	
    	
    	public void paint(Graphics g)
    	{
    			
    			g.drawString("You guessed this many times:" + tries, 70, 180);
    				
    			
    	}	
    			
    	public int winnermsg()
    	{
    		if (tries < 10)
    			
    			g.drawString("Either you know the secret or you got lucky!", 70, 200);
    		if (tries == 10)
    			
    			g.drawString("Ahah! You know the secret!",70, 200);
    		
    		if (tries > 10)
    			
    			g.drawString("You should be able to do better!", 70, 200);
    	}
    	
    
    	//random number generator
    	public int mystery()
    
    	{
    
    		int n = (int)(Math.random()* 1000); 
    
    		return n;
    
    	}
    
    
    }

  8. #8
    Join Date
    Feb 2004
    Posts
    541
    Ok, to get the winnermsg method to work you need to pass it the Graphics object. To do this, give it a Graphics parameter from the paint method. You can't call it from the actionperformed method because the actionPerformed method doesn't get given a graphics object.

    The actionPerformed method needs to update information in the class by setting a class variable to the value that the user entered. It should then call repaint. This will result in a call to paint with the right graphics object. The paint method can then decide what to print out.

  9. #9
    Join Date
    Jul 2004
    Posts
    8
    thanks mikebarr, like this?

    or another way?



    Code:
    else
    		  {
    		showStatus("Congratulations. You guessed the number! ");
    		
    		winnermsg(g);
    		
    		  }
    					
    	}
    	
    	
    	public void paint(Graphics g)
    	{
    			
    			g.drawString("You guessed this many times:" + tries, 70, 180);
    			repaint();
    				
    			
    	}	
    			
    	public int winnermsg(Graphics g)
    	{
    		if (tries < 10)
    			
    			g.drawString("Either you know the secret or you got lucky!", 70, 200);
    		if (tries == 10)
    			
    			g.drawString("Ahah! You know the secret!",70, 200);
    		
    		if (tries > 10)
    			
    			g.drawString("You should be able to do better!", 70, 200);
    	}

  10. #10
    Join Date
    Feb 2004
    Posts
    541
    you can't call winnermsg from the actionPerformed method because the actionperformed method doesn't get passed a graphics object. You need to call winnermsg from the paint method

  11. #11
    Join Date
    Jul 2004
    Posts
    8

    Talking thanks !

    HEy thanks mike and cjard, cma the others that helped! I changed my code a bit more and i got it to work, but what i found that messed me up was that when i ran my applet and i said it was writing over each other,
    when i moved the applet screen out of the desktop view and back it it like refreshed and only one number was showing, not stacked up like before.

    Do you know guys why my applet was refreshing by itself and why ihad to move it off my screen to refresh the number of guesses space?


    thanks again guys, you helped me so much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center