MouseClicked <-> Thread doesn't repaint...

Y

Yoshi

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:
________________________________________

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();
}
________________________________________

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
 
S

Steve W. Jackson

: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:
:________________________________________
:
: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();
:}
:________________________________________
:
: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 =
 
Y

Yoshi

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.
_________________________________________________________
 
C

Christian Kaufhold

Yoshi said:
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:


comp.lang.java.gui FAQ.


[snip unordered quote]


Christian
 
M

Minti

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:
_________________________________________________________
I am still not sure about your problem, do you intend to say that your
repaint is called up only once rather than 20 times, Are you quite
sure about it, I tested your code with the following stub

public static void main(String args[]) {
Animation an = new Animation();
javax.swing.JFrame anf = new javax.swing.JFrame("Title");
anf.getContentPane().add(an);
anf.pack();
anf.show(true);

}



It seems to be working fine that is your string blabla move down on
mousePress Even though AFAIK you don't use paint in Swing instead you
use paintComponent()

BTW you have to use paintComponent instead of paint if you are dealing
with swing.


HTH
 
Y

Yoshi

Thanks for your help!
Indeed everything is working now after I restarted, extending my own
small sample code and partially adopting the old code. I didn't find
the source of the problem, though. One of those rare problems where
you spends hours without any reasons..

BTW, I swapped paint with paintComponent, and everything looks the
same as before.. I guess the only difference is that paint calls
paintComponent, paintBorder, and paintChildren whereas paintComponent
is just one of these subroutines, right?

Have a nice day,
Yoshi O.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top