Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > MouseClicked <-> Thread doesn't repaint...

Reply
Thread Tools

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

 
 
Yoshi
Guest
Posts: n/a
 
      02-08-2004
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
 
Reply With Quote
 
 
 
 
Steve W. Jackson
Guest
Posts: n/a
 
      02-10-2004
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) (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 =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
 
 
 
Yoshi
Guest
Posts: n/a
 
      02-24-2004
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" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)-berlin.de>...
> In article <(E-Mail Removed) >,
> (E-Mail Removed) (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 =

 
Reply With Quote
 
Christian Kaufhold
Guest
Posts: n/a
 
      02-25-2004
Yoshi <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Minti
Guest
Posts: n/a
 
      02-25-2004
(E-Mail Removed) (Yoshi) wrote in message news:<(E-Mail Removed). com>...
> 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
--
Imanpreet Singh Arora
isingh AT@ acm DOT. org
 
Reply With Quote
 
Yoshi
Guest
Posts: n/a
 
      03-04-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
Thread was being aborted in win2003 server. Back ground thread reading MS access database, no redirects or transfers. Johanna ASP .Net 0 10-13-2004 01:32 PM
"Thread was being aborted" error from WebApp using Thread.Sleep. Stephen Miller ASP .Net 3 07-01-2004 11:50 PM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM
mouseClicked vs. mousePressed on Apple vs. Windows Mike Dahmus Java 3 09-04-2003 07:00 PM



Advertisments