Thanks for your response!
Some more details concerning my problem.. I have added a MouseListener
to my JPanel. After each mouse click I'd like to start an animation on my panel.
That's why I included my thread directly inside the MousePressed(e) method.
As you saw before, I used repaint() to call the paint(g) method. My problem
is that my panel is in fact being repainted, but the repaint takes place
after the thread has ended... Here is quite a simplified version of the code:
__________________________________________________ _______
public class Animation extends JPanel implements MouseListener {
private int counter = 0;
public Animation() {
addMouseListener(this);
}
public void paint(Graphics g) {
super.paint(g);
g.drawString("blabla", 10, 20 + counter * 5);
// etc (just an example)
}
public void mousePressed(MouseEvent e) {
counter = 0;
new Thread() {
public void run() {
while(counter < 20) {
counter++;
repaint();
try { Thread.sleep(100); } catch(Exception e) {}
}
}
}.start();
}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
__________________________________________________ _______
....I tried different ways to get a working animation (writing an inner and
extra class for the read, implementing the Runnable interface, etc.), but
nothing was working. So it would be great if you could help me, launching
the animation without getting the mouse listener break.
Thank you, greetings,
Yoshi O.
__________________________________________________ _______
"Steve W. Jackson" <> wrote in message news:<stevewjackson->...
> In article < >,
> (Yoshi) wrote:
>
> >:Hello,
> >:
> >:I want to start a thread/animation after a mouse click.
> >:My problem is that the corresponding panel isn't repainted
> >:by the thread - as soon as the thread has ended, the
> >:repaint works... Here's a little code snippet:
> >:________________________________________
> >:
> >
ublic void mousePressed(MouseEvent e) {
> >: counter = 0;
> >: new Thread() {
> >: public void run() {
> >: while(counter < 20) {
> >: counter++;
> >: repaint();
> >: try { Thread.sleep(100); } catch(Exception e) {}
> >: }
> >: }
> >: }.start();
> >:}
> >:________________________________________
> >:
> >:I tried to put the thread in another class, I tried different
> >:versions of Runnable & Thread, I tried to find an easy
> >:solution in some tutorial, but nothing is working. I'd be
> >:very happy to get a solution for this problem...
> >:
> >:Thanks very much!
> >:Yoshi Okamoto
>
> It's not clear from the example above what it is you're really trying to
> accomplish. But you should know that repaint() doesn't actually paint
> anything in Swing. I suggest you read the very informative page at
> <http://java.sun.com/products/jfc/tsc/articles/painting/index.html> that
> addresses the subject. In AWT, an event is posted, while in Swing the
> component's RepaintManager updates its dirty regions and causes posting
> of an event that ultimately leads to painting of dirty regions.
>
> If that doesn't help solve your problem, perhaps additional detail about
> your goal is in order.
>
> = Steve =