Forums Register Login

Help With Repaint Method

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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:
Our first order of business must be this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1452 times.
Similar Threads
Popup menu not responding with mouse event
Messagebox with Mouseevent
Double buffering / drawChars / flickering
percentage based background scheme for a JPanel
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:47:57.