• 0

[JAVA] drawString help needed


Question

I am trying to draw a line of astricks to the applet once the user gets the proper number guessed. Each astrick should be a different color (random color works for me). I have the following code in this method and it is giving me an error on the g.drawString() something about g. not being intialized. I know its a bonehead question but I do not entirely understand how the Graphics class works exactly. I need to just have this method be called when the user guesses the right number. Also how do you clear any strings you have drawn on the applet? Is there a specific command?

  public void drawStars ( )
   {
       int colorVar;
       Graphics g;
       for( int k = 0; k < 30; k++ )
       {
           colorVar =  (int)( 1000 * Math.random());
           colorVar = colorVar % 2;
           g.setColor(new Color(255, 255, (255 - (colorVar * 15))));
           g.setColor(Color.BLACK);
           g.drawString( "* ", (25 + k), (25 + k) );
       }
   }
}

Link to comment
Share on other sites

17 answers to this question

Recommended Posts

  • 0

I'm not an Java applet programmer, but I can tell you that you 99%/ALWAYS need to initialize all "objects" before use. Primatives do not need initalization

Primatives - int, long, short, char ...etc

Objects - Object, Long, Integer, Graphics.

Graphics g = new Graphics();  //object init

int i;   //init to 0 automatically - but we always say int i = 0; anyway

Link to comment
Share on other sites

  • 0

Yeah..you declared an new object ("g") of the Graphics class but you also need to instantiate the "g" object which is done as liykh001 showed with the "= new Graphics();" part

Link to comment
Share on other sites

  • 0

Then try this...

  public void drawStars (Graphics g){

and send it g from the method call

  drawStars(g){

This is assuming your method call was done in the paint method though.

Link to comment
Share on other sites

  • 0

Here is the full code

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

public class numberGuess extends JApplet implements ActionListener {
   JLabel numberLabel;
   JTextField numberField;
   JTextArea outputArea;
   int randomNum;

   public void init()
   {
      Container container = getContentPane();
      container.setLayout( new FlowLayout() );
      container.setBackground( Color.GREEN );  //Color
      
      randomNum = generateNum();

      // create numberLabel and attach it to content pane
      numberLabel = new JLabel( "Justin Smestad -- Guess a number between 1 and 6" );
      container.add( numberLabel );

      // create numberField and attach it to content pane
      numberField = new JTextField( 10 );
      container.add( numberField );

      // register this applet as numberField?s ActionListener
      numberField.addActionListener( this );


      // JTextArea to display previous numbers
      outputArea = new JTextArea(1,25);
      // attach outputArea to container
      container.add( outputArea );

   }

   public void actionPerformed( ActionEvent event )
   {  
           
      int enteredNum = Integer.parseInt(numberField.getText());
      
      clear();
      
      checkNum( enteredNum );

   } 
   
   public int generateNum ()
   {
       int number = 0;
       
       do
       {
           number = (int) (10 * (Math.random()));
       }
       while(number < 1 || number > 6);
       
       return number;
   }
   
   public void checkNum ( int input )
   {
       if( input < 1 || input > 6)
       {
           outputArea.setText ( input + " is out of the range 1 to 6 . Try Again." );
       }
       else if(input > randomNum )
       {
           outputArea.setText ( input + " is too high. Try Again." );
       }
       else if(input < randomNum)
       {
           outputArea.setText ( input + " is too low. Try Again." );
       }
       else
       {
           outputArea.setText ( "Congratulations! You guessed the number " + input + "!" );
           randomNum = generateNum();
           drawStars();
           
       }
   }
   
   public void clear( )
   {
       numberField.setText("");
   }
   
   public void drawStars ( )
   {
       int colorVar;
       Graphics g = new Graphics();
       for( int k = 0; k < 30; k++ )
       {
           colorVar =  (int)( 1000 * Math.random());
           colorVar = colorVar % 2;
           g.setColor(new Color(255, 255, (255 - (colorVar * 15))));
           g.setColor(Color.BLACK);
           g.drawString( "* ", (25 + k), (25 + k) );
       }
   }
}

Link to comment
Share on other sites

  • 0

The problem is if I move the drawStars method, I need it to run every time the correct answer is expressed and also I need the stars cleared after they are drawn for the next round of guessing.

Link to comment
Share on other sites

  • 0

Now I get

"The method drawStars(Graphics) in the type numberGuess is not applicable for the arguments ()"

as an error.

When I add the call as drawStars( g ); it does not work because I do not have a graphics object to pass in.

Link to comment
Share on other sites

  • 0

Try adding this:

void draw(Graphics g) {

drawStars(g);

}

Of course that will do it every time Java refreshes your applet, so you may want to add a check to see if you're drawing.

Link to comment
Share on other sites

  • 0

(dont forget Im not an applet programmer) Ok looking at your code, I'm not sure where you are attempting to draw the stars.

couldnt you just print the * in the "outputArea" JTextArea object? - forgetting about random color

are you attempting to open a new window and display the stars in a text area?

Link to comment
Share on other sites

  • 0

try init using

Graphics g = getGraphics();

or better yet do

getGraphics().setColor(new Color(255, 255, (255 - (colorVar * 15))));
getGraphics().setColor(Color.BLACK);
getGraphics().drawString( "* ", (25 + k), (25 + k) );

I believe getGraphics() is an inherited method of applet class

Link to comment
Share on other sites

  • 0

Since I dont have a java IDE on this computer I cant say for sure, but there is probably a method that will clear it for you

for example getGraphics().clear(); or getGraphics().drawString("");

you need to look up the Java API on Grapahics class and look at inherited methods, implmeneted interfaces..etc !!!!

if you are using Eclipse (www.eclipse.org) you can just do getGraphics(). and it will automatically display a list of all avaliabe methods to that object.

Link to comment
Share on other sites

  • 0
try

g.repaint();

and print your new asterisks :D

584812727[/snapback]

repaint will (I think) redraw the current * again - more like a refresh method

I think you need to clear the contents before repainting

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.