An Eventful onset

This page is currently putting up with massive transmutation and that is precisely why only the lonely code will have to make do. We will be back here soon, but till then, Thanx for visiting this page. If you are a member of the Java-crazed community, you can still try out the code furnished here. Maybe with some divine intervention you might be able to get the drift...

Every time a bold line of code appears in the programs, consider it to be the new addition to the previous program.


The import statements for the following programs

import java.applet.*;
import java.awt.*;
Program 1
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{       int i=0;
        int j=0;
        public void init()
        {       add(new Button("Hell"));
        }
        public boolean handleEvent (Event e)
        {       i++;
                System.out.println("HandleEvent "+i);
                return true;
        }
        public boolean action(Event e, Object o)
        {       j++;
                System.out.println("action "+j);
                return true;
        }
}
Note that only the handle event is called. Can you figure out why's that so?

Program 2

public class zzz extends Applet
{       int j=0;    
        public void init()
        {       add(new Button("Hell"));
        }
        public boolean action(Event e, Object o)
        {       j++;
                System.out.println("action "+j);
                return true;
        }
}
Now when we don't have the handleEvent(), the action() gets called.

Program 3

public class zzz extends Applet
{       int i=0;int j=0;    
        public void init()
        {       add(new Button("Hell"));
        }
        public boolean handleEvent (Event e)
        {       i++;
                System.out.println("HandleEvent "+i);
                return false;
        }
        public boolean action(Event e, Object o)
        {       j++;
                System.out.println("action "+j);
                return true;
        }
        public boolean mouseDown(Event e,int x,int y)
        {       System.out.println("Mouse Down");
                return true;
        }
}
Note the difference when you use return true /false The action() and the mouseDown() do not get called

Program 4

public class zzz extends Applet
{       int i=0;int j=0;    
        public void init()
        {       add(new Button("Hell"));
        }
        public boolean handleEvent (Event e)
        {       super.handleEvent(e);    
                i++;
                Object o = e.target;
                Class c = o.getClass();
                System.out.println("Id="+e.id+" x="+e.x+" y="+e.y+" Target "+c.getName());
                showStatus("i..."+i+"..."+e.toString());
                return false;
        }
        public boolean action(Event e, Object o)
        {       j++;
                System.out.println("action "+j);
                return true;
        }
        public boolean mouseDown(Event e,int x,int y)
        {       System.out.println("Mouse Down");
                return true;
        }
}
If super not given , the current action() and mouseDown() are not called
import java.awt.*;
import java.applet.*;
public class zzz extends Applet
{       Scrollbar s;
        TextField t;
        Choice c;
        public void init()
        {       setLayout(new BorderLayout());
                t = new TextField("0", 10);
                add("North",t);
                s = new Scrollbar(Scrollbar.HORIZONTAL, 0, 100, 0, 10000);
                add("South",s);
                c = new Choice();
                c.addItem("a1");
                c.addItem("a2");
                c.addItem("a3");
                add("East",c);
        }
        public void paint(Graphics g)
        {       Dimension d = size();
                g.drawRect(0,0, d.width - 1, d.height - 1);
        }
        public boolean handleEvent(Event e)
        {       if (e.target instanceof Scrollbar)
                {       t.setText(String.valueOf(s.getValue()));
                        return true;
                }
                if (e.target instanceof Choice)
                {       showStatus("index is" + c.getSelectedIndex());
                        return true;
                } 
                if (e.target instanceof TextField)
                {       t.setText("index is" + c.getSelectedIndex());
                        return true;
                } 
                return false;
        }
}

Send us your comments, feedback, suggestions et al right offhand...

Back to our Java page


Vijay Mukhi's Computer Institute
B-13, Everest Building, Tardeo, Bombay 400 034, India.
http://www.neca.com/~vmis
e-mail :vmukhi@giasbm01.vsnl.net.in
1