This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Help With Repaint Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help With Repaint Method" Watch "Help With Repaint Method" New topic
Author

Help With Repaint Method

Devin Sturgeon
Greenhorn

Joined: Mar 15, 2006
Posts: 3
im working on an RPG game and using a JFrame to show the contents of a JPanel(my container), the problem is in my KeyListener


EDIT by mw: Added Code Tags to restore original poster's formatting.
[ March 15, 2006: Message edited by: marc weber ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think the problem here is in implementing threads.

Basically, the run() method needs to be in some class that implements Runnable. (Note that Thread implements Runnable, so you can do this by extending Thread.) Then calling start() on the Thread will enable the code in run() to execute as its own thread.

But in your code...

Thread th= new Thread();
th.start();

...is doing nothing, because the run method you define has nothing to do with the Thread.

For information on threads, see this tutorial.

But I'm not sure that multithreading is really what you want here. Can you describe what you're trying to do in this method?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Devin Sturgeon
Greenhorn

Joined: Mar 15, 2006
Posts: 3
my JFrame is the main method its going to show my JPanel, its the beginning to my game. I just dont understand how threads work so im shooting in the dark trying to figure this out.
if you compile it, you can see if you take the repaint() out of the keyListener it does almost what i want it to, to just move the triangle but you cant see it untill you manually refresh it by adjusting the size of the screen.
i know the thread is my problem beacuse I dont know how to even use them. i havent learned applets or anything yet in my programming class so ill look at the thread tut and thanks
Devin Sturgeon
Greenhorn

Joined: Mar 15, 2006
Posts: 3
I think all im getting at is how do i update the JFrame i went from a working applet to trying to get this to work on a JPanel and i dont understand how the wrapping on it is different
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The thread code can be removed. This is what stuck out to me when I took a quick look at the KeyListener code, but after running the program, I see that the problem does appear to be in the painting. On a key event, the entire JFrame (not just the JPanel) is being rendered with its upper left corner where the JPanel's upper left corner should be. The previous rendering remains on the JPanel.

I don't know Swing very well, so I'm moving this to the Swing forum, where I'm sure someone will be quick to identify the problem...
Ron McKenzie
Greenhorn

Joined: Mar 15, 2006
Posts: 3
It's very common to use Threads with any game. One reason is that games can often become resource hogs and bring the system to it's knees. The more common reason is that if you are developing a game in an applet then it is an easy way to ensure that the applet is actually loaded before you start to draw stuff. For now you can drop your Threads, but you will need some place to put your "Main game loop." I would suggest your main. I think the real reason your program is not working is that repaint() does not work the same for JPanels as it does for Applets. I don't think it calls update(). Here is a working version of your code:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help With Repaint Method
 
Similar Threads
Popup menu not responding with mouse event
Messagebox with Mouseevent
Double buffering / drawChars / flickering
percentage based background scheme for a JPanel