GameGrid: Game programming with Java

Research project PHBern  
HomePrintJava-Online

Mouse events I (with GGMouseListener)


With the help of mouse events, mouse controlled actions can be executed. In JGameGrid, the mouse is monitored by a GGMouseListener, which is registerd with addMouseListener(). With the help of an "OR"-mask only the events which are relevant for the application can be defined. The other ones are filtered out. The callbackmethod mouseEvent() is called when the right or left mouse button is clicked or released as well as when the mouse is moved.

With the parameter of the class GGMouse important event information can be retrieved, especially about the current mouse position.

Example 1: with the help of the mouse, an actor can be moved to any possible position inside the grid.

Inside the switch structure, we decide what happens after the left mouse button is pressed (lPress), released (lRelease) or pressed and moved (lDrag).

Run this example

Edit this example in the Online-Editor

 

// JGameEx21.java

import ch.aplu.jgamegrid.*;
import java.awt.*;

public class JGameEx21 extends GameGrid implements GGMouseListener
{
  private Actor actor ;
  public JGameEx21()
  {
    super(8, 8, 70, Color.red,  false);
    setTitle("Drag Mouse!");
    addActor(new Actor("sprites/mouse.gif")new Location(5, 5));
    addMouseListener(thisGGMouse.lPress | GGMouse.lDrag );
    show();
  }

  public boolean mouseEvent(GGMouse mouse)
  {
    Location location = toLocationInGrid(mouse.getX(), mouse.getY());
    switch (mouse.getEvent())
    {
      case GGMouse.lPress:
        actor = getOneActorAt(location);
        break;
      case GGMouse.lDrag:
        if (actor != null)
          actor.setLocation(location);
        break;
    }
    refresh();
    return true;
  }

  public static void main(String[] args)
  {
    new JGameEx21();
  }
}

Explaining the program code:
addMouseListener(this, GGMouse.lPress | GGMouse.lDrag | 
GGMouse.lRelease)
the MouseListener is registered. Since the application class implements a GGMouseListener, its reference is set as a parameter (this).
lPress, lDrag and lRelease are constants of the class GGMouse, which are connected through OR, to set the mouse events
Location location = toLocationInGrid(mouse.getX(), mouse.getY()) returns the grid coordinates auf the mouse cursor's position at the click. Only the positions inside the visible area are considered
actor = getOneActorAt(location) retruns the actor inside the cell where the mouse click was made in

 

Example 2: Clicking the left mouse button a new clownfish shows up on the gird. Hitting the right one, the fish disappears.

The fish is an instance of the class Clownfish. Since the movements of the fish are already implemented in the class Clownfish, each new initialized fish already starts moving.

The MouseListener and the mouse events are implemented in the application class. In this example we only need GGMouse.lPress and GGMouse.rPress.


Run this example

Edit this example in the Online-Editor

 

// JGameEx23.java

import ch.aplu.jgamegrid.*;
import java.awt.*;

public class JGameEx23 extends GameGrid implements GGMouseListener
{
  public JGameEx23()
  {
    super(15, 15, 40, Color.red, "sprites/reef.gif"false);
    addMouseListener(thisGGMouse.lPress);
    show();
    doRun();
  }

  public boolean mouseEvent(GGMouse mouse)
  {
    Location location = toLocationInGrid(mouse.getX(), mouse.getY());
    addActor(new Clownfish(), location);
    return true
  }

  public static void main(String[] args)
  {
    new JGameEx23();
  }
}

class Clownfish extends Actor 
{
  public Clownfish()
  {
    super("sprites/sNemo.gif");
  }

  public void act()
  {
    if (getX() == || getX() == 14)
    {
      turn(180);
      setHorzMirror(!isHorzMirror());
    }
    move();
  }
}

Explaining the program code:
addMouseListener(this, GGMouse.lPress | GGMouse.rPress) registers the MouseListener. Only the left (lPress) and right (rPress) mouse click are relevant
Location location = toLocationInGrid(mouse.getX(), mouse.getY()) saves the grid coordinates of the mouse cursor at the click in the variable location
addActor(new Clownfish(), location) initializes a new clownfish at the position of the mouse click
Actor actor = getOneActorAt(location)
if (actor != null)
    actor.removeSelf()
if there is an actor at the position of the right mouse click, it is removed from the grid

 

Example 3: the falling aliens can be removed by clicking them. As soon as they hit the ground, they change color and cannot be removed anymore.

The actions of the aliens are defined in the class Alien. An alien moves from the top to the bottom (setDirection 90°) and changes color as soon as it hits the border (y = 19). The aliens are continuously initialized as an instance of the class Alien at a random x-coordinate and a y-coordiante fixed to 0.

The MouseListener and the mouse events are implemented in the application class. In this example we only use GGMouse.lPress.

Run this example

Edit this example in the Online-Editor

 

// JGameEx24.java

import ch.aplu.jgamegrid.*;

public class JGameEx24 extends GameGrid implements GGMouseListener
{
  private int nbHits = 0;

  public JGameEx24()
  {
    super(202030false);
    addMouseListener(this, GGMouse.lPress);
    setSimulationPeriod(200);
    playSound(GGSound.DUMMY);
    show();
    doRun();
  }

  public boolean mouseEvent(GGMouse mouse)
  {
    Location location = toLocationInGrid(mouse.getX(), mouse.getY());
    if (mouse.getEvent() == GGMouse.lPress)
    {
      Actor actor = getOneActorAt(location);
      if (actor != null && actor.getY() != 19)
      {
        actor.removeSelf();
        nbHits++;
        setTitle("Hits: " + nbHits);
        playSound(GGSound.PING);
      }
    }
    return false;
  }

  public void act()
  {
    if (getNbCycles() % 5 == 0)
    {
      int x = (int)(20 * Math.random());
      addActor(new Alien()new Location(x, 0));
    }
  }

  public static void main(String[] args)
  {
    new JGameEx24();
  }
}

// ------------------------ class Alien --------
class Alien extends Actor
{
  public Alien()
  {
    super("sprites/alien.gif"2);
    setVisible(0true);
  }

  public void act()
  {
    setDirection(90);
    if (getY() < 19)
      move();
    else
      setVisible(1true);
  }
}

Explaining the program code:

if (getNbCycles() % 5 == 0)

sets the interval of the aliens' appearance. A new alien is initialized each fifth simulation cycle
location = toLocationInGrid(mouse.getX(), mouse.getY()) saves the position of the mouse click in the variable location
Actor actor = getOneActorAt(location)
 if (actor != null && actor.getY() != 19)      
     actor.removeSelf()
if there is an actor at the position of the mouse click, it is removed. The red aliens (y = 19) cannot be removed
nbHits++
setTitle("Hits: " + nbHits
the amount of alien-hits are counted and displayed in the title bar