This content has been marked as final. Show 5 replies
I don't understand the question, and since nobody else has responded yet I suspect nobody else does either.
Ok ill try and explain better:
If i had "jtp.insertIcon(icon);" in the "public static ImageIcon gameState(int count) " method it would not work as jtp is a JTextPane and cannot be used in a static method.
What I am trying to do is passed the value of icon from the static method (the middle one) to the bottom method so that it can be used in a JTextPane - jtp.
So every time the user selects an incorrect letter the hangman is displayed more and more.
If you're in a static context the only want to call non-static code is to instantiate an object. You'll either have to instantiate something or use a reference to an existing object.
I'd recommend that in general, you want to get out of a static context as quickly as possible. Static code is generally against the spirit of object-oriented programming anyway, but also trying to mix static and non-static code can be a real pain (apart from the occasional static utility method). So in your main() method, instatiate the class that provides the main program code and start doing the real work in there.
Cheers for the help.
The problem now is that I have to count how many times the action performed method is used to determine how much of the hangman should be shown.
I think count has to be in a static method i order to work i.e. increment.
Is there another way of counting how many wrong (non matching letter answers) there are, instead of a static method?
When you said:
You'll either have to instantiate something or use a >reference to an existing object.was that the other way?
If so could you please explain a bit more
Message was edited by:
Well I don't know what the larger structure of your program is, so I couldn't suggest tweaks to it.
But in general...I'd suggest first that you separate your program logic from the GUI. The program logic would be in its own object (and thus would have its own state, and it would not be static). The program logic object's state would include a counter to indicate how many incorrect guesses there are, the correct answer, and the correct letters guessed so far (or, a version of the answer with correct guesses filled it). The object would have a method to guess a letter.
The image of the hanged man would really be a GUI widget that has its own internal state consisting of a number. It shows a series of images of parts of a hanged man based on the number. The program logic would have a reference to the GUI widget, and would update its state (the number).
The main program would instantiate the program logic object and the GUI elements. There'd be a listener for key or button events representing letter guesses. These would be translated by the listener into the program logic object's guess-a-letter method.