This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Hangman game - draw the gangman

 
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
 
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • 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.

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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: 5167
11
Netbeans IDE Opera Java
  • 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: 5167
11
Netbeans IDE Opera Java
  • 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..
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic