Monday, 24 February 2014

EVENT CLASSES:MouseEvent Class

 KeyEvent and MouseEvent are subclasses of abstract InputEvent class..................

The MouseEvent Class

                    KeyEvent and MouseEvent are subclasses of abstract InputEvent class. Both these events are generated by objects of type Component class and its subclasses.The MouseEvent is generated when the user presses the mouse or moves the mouse.


The MouseEvent class defines the following integer constants:


  • MOUSE_CLICKED    :     The user clicked the mouse.
  • MOUSE_DRAGGED  :     The user dragged the mouse.
  • MOUSE_ENTERED   :     The mouse entered a component.
  • MOUSE_EXITED        :     The mouse exited from a component.
  • MOUSE_MOVED        :     The mouse moved.
  • MOUSE_PRESSED   :     The mouse was pressed.
  • MOUSE_RELEASED:    The mouse was released.
  • MOUSE_WHEEL        :    The mouse wheel was moved.


 MouseEvent constructors:


MouseEvent(Componentsrc, int type, long when, int modifiers,int x, int y, int clicks, boolean triggersPopup)



Here,src is a reference to the component that generated the event. The type of the event is specified by type. The system time at which the mouse event occurred is passed in when. The modifiers argument indicates which modifiers were pressed when a mouse event occurred. The coordinates of the mouse are passed in x and y. The click count is passed in clicks. The triggersPopup flag indicates if this event causes a pop-up menu to appear on this platform.


MouseEvent Methods:

  •  getX( ) and getY( ): These return the X and Y coordinates of the mouse within the component when the event occurred.
                  syntax:
                            int getX( )
                            int getY( )
  • getPoint( ) method:The  getPoint( ) method to obtain the coordinates of the mouse.
                    syntax:
                         Point getPoint( )
           It returns a Point object that contains the X,Y coordinates in its integer members: x and y. 

  • translatePoint( ) method :The translatePoint( ) method changes the location of the event.
              syntax:
                      void translatePoint(int x, int y)
            Here, the arguments x and y are added to the coordinates of the event.


  • getClickCount( ) method :The getClickCount( ) method obtains the number of mouse clicks for this event.
             syntax:
                    int getClickCount( )


  • isPopupTrigger( ) method:The isPopupTrigger( ) method tests if this event causes a pop-up menu to appear on this platform.
            syntax:
                  boolean isPopupTrigger( )


  •  getButton( ) method:It returns a value that represents the button that caused the event. The return value will be one of these constants defined by MouseEvent: NOBUTTON BUTTON1 BUTTON2 BUTTON3 The NOBUTTON value indicates that no button was pressed or released.
              syntax:
                     int getButton( )

Java SE 6 added three methods toMouseEvent that obtain the coordinates of the mouse relative to the screen rather than the component. They are shown here:

  • The getLocationOnScreen( ) method:The getLocationOnScreen( ) method returns a Point object that contains both the X and Y coordinate. 

               syntax:
                        Point getLocationOnScreen( )

  • getXOnScreen( ) & getYOnScreen( ):These two methods return the indicated coordinate.

                syntax
                       int getXOnScreen( )
                        int getYOnScreen( )

No comments:

Post a Comment