Open In App

Java AWT | MouseInfo and PointerInfo

Last Updated : 31 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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 mouse
    import java.awt.*;
    import javax.swing.*;
      
    class numberofbuttons {
      
        // Main Method
        public static void main(String args[])
        {
            int numberofbuttons;
      
            // 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 mouse
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
      
    class mouse extends JFrame {
      
        boolean b;
      
        // label
        JLabel l, l1;
      
        // Main Method
        public static void main(String args[])
        {
      
            // create object
            mouse m = new mouse();
        }
      
        // default constructor
        mouse()
        {
            super("mouse");
      
            // create labels
            l = new JLabel("");
            l1 = new JLabel("");
      
            // create a panel
            JPanel p = new JPanel();
      
            // add labels to panel
            p.add(l);
            p.add(l1);
      
            add(p);
            show();
            setSize(300, 300);
      
            b = true;
            execute();
        }
      
        public void execute()
        {
            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:



Similar Reads

Java AWT vs Java Swing vs Java FX
Java's UI frameworks include Java AWT, Java Swing, and JavaFX. This plays a very important role in creating the user experience of Java applications. These frameworks provide a range of tools and components for creating graphical user interfaces (GUIs) that are not only functional but also visually appealing. As a Java developer, selecting the righ
11 min read
Java.awt.image.RescaleOp Class in Java with Examples
RescaleOp is a class in java.awt.image package which implements BufferedImageOp and RasterOp interface. This class performs a pixel-by-pixel rescaling of the data in the source image by multiplying the sample values for each pixel by a scale factor and then adding an offset. The scaled sample values are clipped to the minimum/maximum representation
5 min read
Difference between AWT and Swing in Java
Java is one of the most in-demand programming languages for developing a variety of applications. The popularity of Java can be attributed to its versatility as it can be used to design customized applications that are light and fast and serve a variety of purposes ranging from web services to android applications. Java is fast, reliable, and secur
2 min read
Java AWT | GridLayout Class
GridLayout class represents a layout manager with a specified number of rows and columns in a rectangular grid. The GridLayout container is divided into an equal-sized of rectangles, and one of the components is placed in each rectangle. Every rectangle cell has the same size therefore, they contain a component, which fills the entire cell. When th
5 min read
Java AWT | Cursor class with examples
Cursor class is a part of Java AWT package and it is used to create custom cursors or inherit system or predefined cursors. Cursor class is mainly used to encapsulate the bitmap representation of the mouse cursor. Constructor of cursor class are : Cursor(int t) : Creates a cursor with specified class Cursor(String name) : Creates a custom cursor wi
4 min read
Java AWT | Choice Class
Choice class is part of Java Abstract Window Toolkit(AWT). The Choice class presents a pop- up menu for the user, the user may select an item from the popup menu. The selected item appears on the top. The Choice class inherits the Component. Constructor for the Choice class Choice() : creates an new empty choice menu . Different Methods for the Cho
5 min read
Java AWT | Desktop Class
The Desktop class is a part of Java AWT package. This class is used to launch associated applications registered on the native desktop to handle a URI or a file. Important Points About Desktop Class : It can open a default web browser showing a specific URI It can launch default mail client with optional mailto URI It can launch a registered applic
5 min read
Java AWT | Color Class
The Color class is a part of Java Abstract Window Toolkit(AWT) package. The Color class creates color by using the given RGBA values where RGBA stands for RED, GREEN, BLUE, ALPHA or using HSB value where HSB stands for HUE, SATURATION, BRIcomponents. The value for individual components RGBA ranges from 0 to 255 or 0.0 to 0.1. The value of alpha det
9 min read
Java AWT | Dimension Class
Dimension class is a part of Java AWT. It contains the height and width of a component in an integer as well as double precision. The use of Dimension class is that many functions of Java AWT and Swing return dimension object. Constructors of the Dimension class Dimension() : It will create a new Object with height and width set to zero.Dimension(D
4 min read
Java AWT | Canvas Class
Canvas class is a part of Java AWT. Canvas is a blank rectangular area where the user can draw or trap input from the user. Canvas class inherits the Component class.Constructor of the Canvas class are : Canvas(): Creates a new blank canvas.Canvas(GraphicsConfiguration c): Creates a new canvas with a specified graphics configuration. Commonly used
3 min read
Article Tags :
Practice Tags :