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 / GUI / January 2005

Tip: Looking for answers? Try searching our database.

Help with Threads (wait and notify)

Thread view: 
Thanasis \(sch\) - 08 Jan 2005 12:44 GMT
Hi to everyone,

the simple java applet i provide below outputs 5 the following 5 lines:
Welcome to Java 1 times
Welcome to Java 2 times
Welcome to Java 3 times
Welcome to Java 4 times
Welcome to Java 5 times

What i want to achieve is my applet to display the first line  and then to
stop waiting for the user to click on the iteration button.
When the user clicks on this button i want my applet to display the second
line and then to stop again until the user clicks again the button and so on
...
i have got help from this newsgroup and solved the problem without threads.
The problem with the solution i got is that to display a message say k, i
have to display again all the previous messages from 1 to (k-1).

How this could be done SOLELY WITH THREADS(WAIT AND NOTIFY)?What code must i
put inside the button's
actionPerformed method and what code must i add
inside the for loop;

Any help would be very much appreciated
thanks in advance
thanasis

==============================
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Test extends Applet    {
 Button iteration;
 Panel tools;

public void init() {
     tools = new Panel();
    iteration=new Button("Next Iteration");
    iteration.addActionListener(new buttonListener());
    tools.add(iteration);
   this.add(tools,"north");
}

public void paint(Graphics g) {

 for (int i=1;i<=5;i++){
    g.drawString("Welcome to Java "+i+" times", 50, 60+20*i );

  }
}
}
/* =============== ACTION LISTENER FOR button================*/
class buttonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

   }   //end actionPerformed
}  //end class
Andrey Kuznetsov - 08 Jan 2005 14:54 GMT
> What i want to achieve is my applet to display the first line  and then to
> stop waiting for the user to click on the iteration button.
[quoted text clipped - 11 lines]
> actionPerformed method and what code must i add
> inside the for loop;

it is unclear why do you want to use threads for this simple thing?
all you need is class variable which you increment every button click.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Test extends Applet {
   Button iteration;
   Panel tools;
   int counter;

   public void init() {
       tools = new Panel();
       iteration=new Button("Next Iteration");
       iteration.addActionListener(new ButtonListener());
       tools.add(iteration);
       this.add(tools,"north");
   }

   public void paint(Graphics g) {
       g.drawString("Welcome to Java "+ counter +" times", 50, 60+20*i );
   }

   class ButtonListener implements ActionListener {
       public void actionPerformed(ActionEvent e) {
           counter++;
           repaint();
       }
   }
}

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Thanasis \(sch\) - 08 Jan 2005 15:10 GMT
Dear Andrey,

thanks for reply.

> it is unclear why do you want to use threads for this simple thing?
> all you need is class variable which you increment every button click.

The problem with the solution with class variable is that to display a
message say k, i
have to display again all the previous messages from 1 to (k-1). As you
might have noticed i have a for loop inside the paint method.

Since i  am writing visualization applets for graph algorithms  i don't want
the above feature. My visualizaion applet iterates through all graph nodes
every time the user clicks on button step.  I want my applet to display ONLY
one NODE every time user clicks on step button

thanks
thanasis
Andrey Kuznetsov - 08 Jan 2005 18:28 GMT
> > it is unclear why do you want to use threads for this simple thing?
>> all you need is class variable which you increment every button click.
[quoted text clipped - 8 lines]
> nodes every time the user clicks on button step.  I want my applet to
> display ONLY one NODE every time user clicks on step button

may be you don't understand how repainting works.
or I don't understand what you want.
If you want to increase number of displayed nodes on every button click
then you change your loop to so that it goes only till counter.

   int counter;

   public void paint(Graphics g) {
       for(int i = 0; i < counter; i++) {
           drawNode(i, g);
       }
   }

   class ButtonListener implements ActionListener {
       public void actionPerformed(ActionEvent e) {
           counter++;
           repaint();
       }
   }

you can optimize button click handler if you just draw new node:

class ButtonListener implements ActionListener {
       public void actionPerformed(ActionEvent e) {
           counter++;
           Graphics g = getGraphics();
           g.drawNode(counter, g);
           g.dispose();
       }
   }

the implementation of drawNode(int node, Graphics g) is up to you...

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Thanasis \(sch\) - 08 Jan 2005 19:26 GMT
thanks andrew,

i think i got it

thanasis
>> > it is unclear why do you want to use threads for this simple thing?
>>> all you need is class variable which you increment every button click.
[quoted text clipped - 41 lines]
>
> the implementation of drawNode(int node, Graphics g) is up to you...
Chris Smith - 08 Jan 2005 15:05 GMT
Thanasis (sch) <thanasis_gr@hotmail.com> wrote:
> i have got help from this newsgroup and solved the problem without threads.
> The problem with the solution i got is that to display a message say k, i
> have to display again all the previous messages from 1 to (k-1).

That's not a problem.  It's a simple fact of GUI environments that you
need to be ready to repaint the screen at any time, and when something
changes you generally display it by repainting the entire screen.  That
may not be what you're accustomed to if you've written console
applications, but it's just the way things work.

> How this could be done SOLELY WITH THREADS(WAIT AND NOTIFY)?What code must i
> put inside the button's actionPerformed method and what code must i add
> inside the for loop;

This isn't a problem that can be solved with wait/notify.  If you're
looking for a way to learn threads, you need to pick a different task.  
When you're writing a GUI, there is *one* thread, called the event
dispatch thread or EDT, which is solely responsible for handling the
event queue, including button presses and painting.  So the same thread
is running actonPerformed *and* paint, and you can't communicate back
and forth between the two.

If you did have a thread wait() inside the paint method, then the GUI
would become unresponsive.  It would not redraw itself if someone
dragged a window over it; it wouldn't respond at all to button presses,
menu items, or anything else.  That's because all these things are
handled by the event dispatch thread, but the event dispatch thread is
busy waiting on a monitor.

If, instead, your goal is to avoid redrawing all the text when possible
(perhaps for imagined performance reasons?) then you can look at the
version of repaint() that specifies a clipping rectangle -
myComponent.repaint(x, y, width, height) - and check the clipping
rectangle in the paint method.  Then you'd only actually draw a certain
string if it's inside of the clipping rectangle you specified.  That
doesn't use wait/notify, but it might solve your underlying problem.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



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.