Link to home
Start Free TrialLog in
Avatar of memilavi
memilavi

asked on

HELP!! Trapping mouse events

Hi.
I've began to write an applet a few days ago using Visual J++ 6.0.
In my recent applets, I've trapped mouse click using the 'mouseDown' event, but now I see this event is deprecated, and there is new method for trapping event. I just can't understand how to implement the new method! I see terms like 'processMouseEvent','eventListenet' and so on, and can't find my way through that maze of words!
Please help me! Please give me a step-by-step instruction of implementing mouse events trapping!

Thanks a lot!
ASKER CERTIFIED SOLUTION
Avatar of zicai
zicai

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mbormann
mbormann

a cool example,sorry I cant explain this but run it and see,
zicai,
pls help ,i am going tommorrow on a long holiday.

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

             public class Assign2 extends java.applet.Applet
             {
             private TextField txt = new TextField("",20);
             private Button b1 = new Button("Button");
             private Button b2 = new Button("Glutton");
             private Button b3 = new Button("Sutton");
             private static Assign2 applet=null;
             private static Color foreGround=null;
             private static Color backGround=null;

             public void init()
             {
             setLayout(new BorderLayout());
             txt.setEditable(false);

             Panel p1 = new Panel();
             //b1.setBackground(Color.cyan);
             b1.addMouseListener(new GenericListener());
             b2.addMouseListener(new GenericListener());
             b3.addMouseListener(new GenericListener());
             p1.add(b1);
             p1.add(b2);
             p1.add(b3);
             add(p1,BorderLayout.NORTH);
             add(txt,BorderLayout.SOUTH);
             }

             class GenericListener extends MouseAdapter
             {
             public void mouseEntered( MouseEvent e )
             {
             //System.out.println("Changing Foreground");

             Button temp = (Button)e.getSource();
             foreGround=temp.getForeground();
             backGround=temp.getBackground();
             temp.setForeground(new
             Color((float)Math.random(),(float)Math.random(),(float)Math.random()))  ;

             temp.setBackground(new
             Color((float)Math.random(),(float)Math.random(),(float)Math.random()))  ;

             }
             public void mouseExited( MouseEvent e )
             {
             //System.out.println("*** Restoring Foreground");
             Button temp = (Button)e.getSource();
             temp.setForeground(foreGround);
             temp.setBackground(backGround);
             }
             }

             public static void main(String[] args)
             {
             applet = new Assign2();
             Frame aFrame = new Frame("Assign2");

             aFrame.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e)
             {
             applet.stop();
             applet.destroy();
             System.exit(0);
             }
             });

             aFrame.add(applet, BorderLayout.CENTER);
             applet.init();
             applet.start();
             aFrame.pack();
             aFrame.setVisible(true);
             }
             }
Avatar of memilavi

ASKER

Thanks, zicai!