Paint Erases

Hello

Well I am trying to make a hangman program and one part of the program is getting messed up. Its in the paint method:

// Total is the number of entries

for (int i = 0 ; i <= total ; i++)

{

for (int j = 0 ; j < hiddenWord.length () ; j++)

{

if (hiddenWord.charAt (j) == foundLetters [total])

{

g.drawString ("" + hiddenWord.charAt (j), 25 + 20 * j, 40);

}

}

}

Ok so lets say total is equal to 1, so it will run twice

and the hidden word is lets say "hello"

and the user guesses "l" it will output "l" twice, but if the user than presses "e" the two "l"s will get erased

"_ _ ll _" to "_ e _ _ _"

my question is how can I prevent the "l" from erasing

Thanks

[1162 byte] By [raptorz2222a] at [2007-11-26 16:10:20]
# 1

I am not sure about the logic of your program to find if the character matches.But thats not the issue here.

I think the probelm is everytime you paint the entire background is cleared.Instead i suggest you use a JTextArea and then use insert method

insert(String character,int position)

to add to the JTextArea

So imagine you get a matching l at position 3 you will have to do

insert(String character,3);

where pattern should contain the character l which you will have to convert to String

qUesT_foR_knOwLeDgea at 2007-7-8 22:32:45 > top of Java-index,Java Essentials,New To Java...
# 2
I forgot to tell you one thing.You should be careful while using insert as it will throw IllegalArgumentException if the positon is invalid.So initially before starting the game you can initialize the JTextArea to hold a empty string of some length greater than the hidden string value
qUesT_foR_knOwLeDgea at 2007-7-8 22:32:45 > top of Java-index,Java Essentials,New To Java...
# 3

You need to educate yourself on how painting in AWT & Swing works.

http://java.sun.com/products/jfc/tsc/articles/painting/index.html

http://java.sun.com/products/java-media/2D/index.jsp

You seem to be under the impression that because you painted it once it's permanent. This is not the case. A particular component may have to paint itself hundreds, thousands or even more times during it's lifetime in any non-trivial application. Each time you need to paint everything that you want to paint (certain optimizations such as only painting dirty regions notwithstanding). Swing doesn't magically know that you really wanted to keep what you had before and it doesn't attempt to.

kablaira at 2007-7-8 22:32:45 > top of Java-index,Java Essentials,New To Java...
# 4

As for the suggestion to use JTextArea that would certainly work though I very much doubt that's the graphical appearance the OP is looking for. One very simple solution would be to use a different javax.swing.JLabel for each "letter". Add all of them to a javax.swing.JPanel using an appropriate layout and initial text of "_". Then it's simply a matter of changing the text of the appropriate JLabel to be what you want. This would allow you to more easily use different Font and Color settings for different letters (for example, temporarily marking the letters that were just revealed in red) and would more easily facilitate replacing them with icons if desired at a later date. Plus, you have more flexibility in how each are positioned using different layouts and it still leaves the details of painting to the JLabel.

kablaira at 2007-7-8 22:32:45 > top of Java-index,Java Essentials,New To Java...
# 5

> As for the suggestion to use JTextArea that would

> certainly work though I very much doubt that's the

> graphical appearance the OP is looking for. One very

> simple solution would be to use a different

> javax.swing.JLabel for each "letter". Add all of

> them to a javax.swing.JPanel using an appropriate

> layout and initial text of "_". Then it's simply a

> matter of changing the text of the appropriate JLabel

> to be what you want. This would allow you to more

> easily use different Font and Color settings for

> different letters (for example, temporarily marking

> the letters that were just revealed in red) and would

> more easily facilitate replacing them with icons if

> desired at a later date. Plus, you have more

> flexibility in how each are positioned using

> different layouts and it still leaves the details of

> painting to the JLabel.

Yes even i felt that the OP needed to use graphical interface.Well i suggested JTextArea as i felt that it is the easiest way to perform the job.If the OP wants to use the graphical appeareance which he first indicated in his post then kab's suggeston is the what you need if you want to have it drawn on the container Jpanel or a JFrame background.

However just have a look at JTextArea.It will also help you learn the api if you haven't come across it.You can initialze it to hold five _ initially to give the impression that there are five letters and replace accordingly using insert

method of JTextArea.Don't forget the dukes(Dukes give me nightmares nowadays).

qUesT_foR_knOwLeDgea at 2007-7-8 22:32:45 > top of Java-index,Java Essentials,New To Java...