Java AWT | MouseInfo and PointerInfo

MouseInfo and PointerInfo is a part of Java AWT. MouseInfo provides information about the location of the pointer and the number of buttons on the mouse. PointerInfo provides information returns the information about the location of the pointer and the graphics device.
Methods of MouseInfo
| Method | Explanation |
|---|---|
| getNumberOfButtons() | Returns the number of buttons on the mouse. |
| getPointerInfo() | Returns a PointerInfo object that represents the current location and graphics device of the pointer. |
Methods of PointerInfo
| Method | Explanation |
|---|---|
| getDevice() | Returns the graphics device on which mouse was present when the object was created . |
| getLocation() | Returns a point object that gives the location of the mouse pointer. |
- Example Program to find the number of buttons on the Mouse
// Java Program to find the number of// buttons on the mouseimportjava.awt.*;importjavax.swing.*;ÂÂclassnumberofbuttons {   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Âintnumberofbuttons;       Â// number of buttons in the mouse       Ânumberofbuttons = MouseInfo.getNumberOfButtons();       Â// print the number of buttons       ÂSystem.out.println("Number of buttons on the mouse ="                                          Â+ numberofbuttons);   Â}}Output:
Number of buttons on the mouse = 5
- Example Program to show the Position of Mouse
// Java Program to show the// position of mouseimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;ÂÂclassmouseextendsJFrame {   Âbooleanb;   Â// label   ÂJLabel l, l1;   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// create object       Âmouse m =newmouse();   Â}   Â// default constructor   Âmouse()   Â{       Âsuper("mouse");       Â// create labels       Âl =newJLabel("");       Âl1 =newJLabel("");       Â// create a panel       ÂJPanel p =newJPanel();       Â// add labels to panel       Âp.add(l);       Âp.add(l1);       Âadd(p);       Âshow();       ÂsetSize(300,300);       Âb =true;       Âexecute();   Â}   Âpublicvoidexecute()   Â{       Âwhile(b) {           Â// get the pointer info object from the mouseInfo           ÂPointerInfo pi = MouseInfo.getPointerInfo();           Â// get the location of mouse           ÂPoint p = pi.getLocation();           Â// set the text of labels           Âl.setText("x position ="+ p.getX());           Âl1.setText("y position ="+ p.getY());       Â}   Â}}Output:
References:




