• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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!
0
memilavi
Asked:
memilavi
1 Solution
 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now