This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hangman game - draw the gangman

 
Christian Mucyo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Christian Mucyo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was using this code in the paint method..

 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic