[SOLVED] Repainting in a thread
Well, I'm trying to make a test program to see if I can do this, and so far I've had poor luck. What I've done so far is have a panel that I paint some things to, which I update by clicking. Then, what I'm trying to do is when I click a different button, something else shows up and animates by itself, by using a separate thread.
The problem I'm having is that when I hit the other button to run the thread, the new objects are being drawn and updated properly, but the old objects are being removed until the thread finishes. Also, I've found that clicking updates the variables for the background objects so that when the thread ends, they are still drawn based on how many times I clicked.
I've also found that the problem probably lies in the fact that when repaint is called by the thread to repaint the panel, the paint method in the panel refuses to be called until after the thread finishes. I'm not sure on this, but it's just my guess based on what I've tried.
Here's the code that draws the stuff the thread should be calling and calls the panel's repaint.
Before it causes any confusion, the panel class I have extends JPanel, and has a Thread object in it. The Thread object's run is called when I click the correct mouse button. The panel itself is passed into the Thread object's constructor, so it can call repaint on the panel. Also, the thread does call Thread.sleep(50) in the animation loop, in case someone was thinking that I need to give the panel time to update.
private void drawThings()
Graphics2D g2d = (Graphics2D)panel.getGraphics();
g2d.fillRect(0, 0, panel.getWidth(), panel.getHeight());
Any indication to what I'm doing wrong would help, thanks!