• Mouse

    1 - Using the mouse.

    A Window class object, in addition to a Keyboard class instance, also provides one of the Mouse class. To access this object , use the following procedure:

    Like the keyboard, the mouse buttons also have behaviors that, if necessary, can be changed. But it is not possible to add or remove buttons.

    2 - Standard mouse buttons

    The standard buttons are as follows: BUTTON_LEFT, BUTTON_MIDDLE and BUTTON_RIGHT. To access the code for these buttons do: Mouse.BUTTON_RIGHT, for the right button, for example.

    All the standard buttons have the DETECT_INITAL_PRESS_ONLY behavior.

    3 - Changing the behavior of a button.

    To change the behavior of a button use the method:

    The parameters to be passed are: the button code to be changed and the desired behavior.

    Example: Changing the behavior of the right button:

    Now, while the right button is pressed, the method boolean isRightButtonPressed(), described below, will return true.

    4 - Pressed buttons

    To find out if a button is pressed, use the following methods of the Mouse class:

    5 - Retrieving the mouse position

    To retrieve the position (x,y) of the mouse on the screen use the method below, present in the Mouse class:

    Example: Point mousePosition = mouse.getPosition();

    The Point class, that exists in Java, serves to store the (x,y) values returned by getPosition() method. The values are accessed as follows: ‘mousePosition.x ‘ and ‘mousePosition.y’.

    6 - Mouse over some object of the game.

    To determine whether the mouse is over some object of the game, use the method present in the Mouse class:

    The object passed as parameter can be one of the following objects: GameObject, GameImage, Animation or Sprite.

    Example:

    7 - Mouse over a given area

    To determine whether the mouse is over a determined of the screen use the method present in the Mouse class:

    area

    Beside, an area is represented with 45 pixels of height and 237 pixels of width.

    Their minimum and maximum points are (0,0) and (45,237).

    To determine whether the mouse is over an area, use the following procedure:

    Instead of boolean isOverArea(Point start, Point end) we could have used the following method, which still would have the same result:

    Using the boolean method isOverArea (int minX, int minY, int maxX, int maxY), we would have:

    Example: By clicking with the mouse the image changes its position.

    Note: The order in which images are draw is important. If we draw first the 'image' and then the background, the 'image' will be overlaid.

    UFF - Universidade Federal Fluminense - Institudo de Computação - Ciência da Computação