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!
memilaviAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zicaiCommented:
Hi, memilavi

To react to mouse events in the Java 1.1 Event Model, you need to implement MouseListener interface(also MouseMotionListener interface if you need to react to mouse drag and mouse move). The following code segment demonstrates the idea:

public class test extends Applet implements MouseListener, MouseMotionListener {

 ....
 public void init() {
   ...
   this.addMouseListener(this);//register the applet as the mouse listener
   this.addMouseMotionListener;//register itself as the motion listener
 }

//Override methods from MouseListener interface
 public void mousePressed(MouseEvent e) {
 //do the things you want
 }
 public void mouseEntered(MouseEvent e) {
 //do the things you want
 }
 public void mouseExited(MouseEvent e) {
 //do the things you want
 }
 public void mouseClicked(MouseEvent e) {
 //do the things you want
 }
 public void mouseReleased(MouseEvent e) {
 //do the things you want
 }

//Override methods from MouseMotionListener interface
 public void mouseDragged(MouseEvent e) {
 //do the things you want
 }
 public void mouseMoved(MouseEvent e) {
 //do the things you want
 }
 ...
}

If you are only interested in only one of the many kind of mouse events, you can use inner classes(it is said to be the most common way to use Java 1.1 event model):
public class test extends Applet {
 ...
 public void init() {
  this.addMouseListener(new MouseAdapter() {
    public void MousePressed(MouseEvent e)  {
     //do the things you want
    }
   });
  this.addMouseMotionListener(new MouseMotionAdapter() {
    public void mouseDragged(MouseEvent e)  {
     //do the things you want
    }
   });
 }
 ...
}

To have a thorough understanding of the Java 1.1 event model, pick a book and read the related chapter:)

Hope this helps.

Yours sincerely
Zicai
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mbormannCommented:
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);
             }
             }
0
memilaviAuthor Commented:
Thanks, zicai!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.