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?
Question
serox
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