Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

repainting a JPanel from that panel's MouseLIstener

Thread view: 
Ted - 18 Aug 2006 22:48 GMT
Hello all. This is a problem some of my students are experiencing and
I'm not sure of the best way to solve it.

They have written a program to play othello. They have extended JPanel
to create an Othello board which paints the 8x8 grid. They have added a
Mouselistener so when the user click on a spot, it makes the move and
updates.

The problem comes in when a student wanted to have the computer PAUSE
before making its move so the human could see the effect of his/her own
move. The code looks kind of like:

class X extends JPanel implements MouseListener  {

paintComponent(Graphics g) { ... }

mousePressed() {
  makeHumanMove();
  repaint();
  Thread.sleep(1000); // wait 1 second...
  makeComputerMove();
  repaint();
}
}

The problem is that since all this code is happening from within the
JPanel, the sleep takes effect before the repaint occurs, so the user
doesn't see any change until after the computer has moved and the
humans move appears to be skipped.

Thanks for your consideration.

Ted Ward
Knute Johnson - 18 Aug 2006 23:17 GMT
> Hello all. This is a problem some of my students are experiencing and
> I'm not sure of the best way to solve it.
[quoted text clipped - 29 lines]
>
> Ted Ward

This question has probably been asked more than any other question.
Fortunately it is a very easy problem to solve.

The Swing GUI that the JPanel is part of, does its update from the Event
Dispatch Thread (EDT).  Events, such as the mouse press also are
processed on the EDT.  So you can't sleep in listener methods if you
want your GUI to stay lively.  What you need to do is run your event
code in another thread so that the EDT is never blocked.

public void mousePressed(MouseEvent me) {
    Runnable r = new Runnable() {
        public void run() {
            // sleep, dream or speak (as the case may be)
        }
    };
    EventQueue.invokeLater(r);
}

Signature

Knute Johnson
email s/nospam/knute/

Ted - 21 Aug 2006 16:53 GMT
> > Hello all. This is a problem some of my students are experiencing and
> > I'm not sure of the best way to solve it.
[quoted text clipped - 52 lines]
> Knute Johnson
> email s/nospam/knute/

Thanks, but if the event handler is in another thread, it will the
sleep still keep the user from making another move? I guess it would
since the "handler" would be sleeping....

I'll give this a try, thanks guys.
Knute Johnson - 22 Aug 2006 04:04 GMT
>>> Hello all. This is a problem some of my students are experiencing and
>>> I'm not sure of the best way to solve it.
[quoted text clipped - 58 lines]
>
> I'll give this a try, thanks guys.

No.  That's the point, it is in another thread and so the GUI is still
alive.  If you want to block any further moves while it is being
processed you need to disable that mechanism.

Signature

Knute Johnson
email s/nospam/knute/

danharrisandrews@gmail.com - 18 Aug 2006 23:31 GMT
Hi Ted,

Since the pause is happening on the event dispatch thread the GUI will
appear to freeze for a second. As a quick hack try the code below. For
a better solution you may want to do a search on the web for
"SwingWorker" and read about how it can be used.

Cheers,

Dan Andrews

- - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - -

makeHumanMove();
repaint();
Thread thread = new Thread() {
   public void run() {
       try {
           Thread.sleep(1000);  // wait 1 second...
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               makeComputerMove();
               repaint();
           }
       });

   }
};
thread.start();

> Hello all. This is a problem some of my students are experiencing and
> I'm not sure of the best way to solve it.
[quoted text clipped - 29 lines]
>
> Ted Ward


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.