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.
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
Joined: Mar 15, 2006
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
Joined: Mar 15, 2006
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
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...
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: