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

Discussion in 'Java' started by Yoshi, Feb 8, 2004.

  1. Yoshi

    Yoshi Guest

    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
     
    Yoshi, Feb 8, 2004
    #1
    1. Advertising

  2. 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:
    >:________________________________________
    >:
    >: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 =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Feb 10, 2004
    #2
    1. Advertising

  3. Yoshi

    Yoshi Guest

    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:<-berlin.de>...
    > 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:
    > >:________________________________________
    > >:
    > >: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 =
     
    Yoshi, Feb 24, 2004
    #3
  4. Yoshi <> 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
     
    Christian Kaufhold, Feb 25, 2004
    #4
  5. Yoshi

    Minti Guest

    (Yoshi) wrote in message news:<>...
    > 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
     
    Minti, Feb 25, 2004
    #5
  6. Yoshi

    Yoshi Guest

    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.
     
    Yoshi, Mar 4, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Dahmus
    Replies:
    3
    Views:
    6,962
    Mike Dahmus
    Sep 4, 2003
  2. stan k.
    Replies:
    1
    Views:
    9,756
    Babu Kalakrishnan
    Nov 7, 2003
  3. Esko Piirainen

    repaint() call doesn't get processed

    Esko Piirainen, Nov 12, 2005, in forum: Java
    Replies:
    4
    Views:
    702
    Thomas Hawtin
    Nov 14, 2005
  4. Z
    Replies:
    12
    Views:
    4,977
    Knute Johnson
    Feb 19, 2007
  5. Kgbol
    Replies:
    4
    Views:
    1,146
Loading...

Share This Page