aspose file tools*
The moose likes Applets and the fly likes Hangman game - draw the gangman Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Hangman game - draw the gangman" Watch "Hangman game - draw the gangman" New topic
Author

Hangman game - draw the gangman

Christian Mucyo
Greenhorn

Joined: Feb 18, 2012
Posts: 8
Hello everyone, I am have some trouble with a Hangman game that I am coding. It is not completely finish, right now I am coding the drawing of the hangman.

Here is the part of the code where am I having trouble to understand.



So my first issue is that the first time errors > 1 the line is drawn but then disappear and reappear with the second line at errrors > 2. I don't know how I can make the first line stay on the frame.

My other issue is when the application draws the legs of the hangman at errors > 11 and errors > 12. When it comes to that point, the whole hangman disappear and each time I put a wrong letter it flashes and I can see the hangman for about a second. I don't really see the issues here.

This is not the whole code so if you need to see the whole code please let me know.

Thanks
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

No, we don't need to see your whole code, but some context would be good. Is this code part of an Applet's repaint() method? I assume your problem is that the whole repaint area is being cleared each time you call the method, but it's been so long since I worked with applets (if this is an applet) that I don't remember if that's normal, or something you're causing.
Christian Mucyo
Greenhorn

Joined: Feb 18, 2012
Posts: 8
Hello greg, first thanks for answering so fast.

Yes, this is an applet but I am not using the repaint() method because when I try to use it it erase the hangman every time there is an error.

The code is in the actionPerformed method and each time I input a letter and press go all the statements are suppose to execute according to their conditions.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

Never use getGraphics() of a component. Override a painting method: paint(...) for AWT, paintComponent(...) for Swing.
http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html

The code is in the actionPerformed method

Change the state of the object -- set the values of fields -- in actionPerformed(...) and call repaint().


luck, db
There are no new questions, but there may be new answers.
Christian Mucyo
Greenhorn

Joined: Feb 18, 2012
Posts: 8
Wow, thanks a lot darryl, that was it. I just did it and it works fine now. Thanks.
By the way, do you know how to setup a gradient background in java? I tried but it keeps going on top of everything else.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

What you paint last overpaints anything painted before (d'oh!) so paint the background first.

Exactly how are you painting a gradient?
Christian Mucyo
Greenhorn

Joined: Feb 18, 2012
Posts: 8
I was using this code in the paint method..

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

I would suggest making the GradientPaint an instance field, or even a static final constant if it never changes. Constructing a GradientPaint can be computationally expensive, so it would be better to avoid creating a new one each time through a painting method.
Christian Mucyo
Greenhorn

Joined: Feb 18, 2012
Posts: 8
How do you do that ? I always did it withing the paint method and my professor never showed us another way.



I am doing it this way, when I put it outside the paint method I am getting all kind of errors, not recognizing what g and g2D are..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hangman game - draw the gangman