Different to the methods of GGMouseListener (mouse.getX(), mouse.getY()), which return the grid coordinates of the mouse click, the methods of GGMouseTouchListener return the actor which the mouse hits or touches. This comes in handy when working with small celled or pixel grids. In this example, Nemo can be moved smoothly around the whole window. It does not jump from cell to cell, as in the examples of the GGMouseListener. The GGMouseTouchListener is registered with addMouseTouchListener(). Only events which are needed in this application are defined. In our examples l Press, lDrag and lRelease. Example 1: The actor Nemo can be grabbed at any position of the Sprite and moved to any position inside the grid. The cell size is set to one (pixel grid). |
![]() |
// JGameEx22.java import ch.aplu.jgamegrid.*; import java.awt.*; public class JGameEx22 extends GameGrid implements GGMouseTouchListener { private Point hotSpot; public JGameEx22() { super(300, 300, 1, false); setTitle("Drag Nemo!"); Actor nemo = new Actor("sprites/nemo.gif"); nemo.addMouseTouchListener(this, GGMouse.lPress | GGMouse.lDrag | GGMouse.lRelease); addActor(nemo, new Location(150, 150)); show(); } public void mouseTouched(Actor actor, GGMouse mouse, Point spot) { switch (mouse.getEvent()) { case GGMouse.lPress: hotSpot = spot; break; case GGMouse.lDrag: if (hotSpot == null) // Pressed outside sprite hotSpot = spot; Location loc = new Location(mouse.getX() - hotSpot.x, mouse.getY() - hotSpot.y); actor.setLocation(loc); refresh(); break; case GGMouse.lRelease: hotSpot = null; break; } } public static void main(String[] args) { new JGameEx22(); } } |
addMouseTouchListener(this, GGMouse.lPress | GGMouse.lDrag | |
registers the MouseTouchListener with the left mouse click, drag and release. Since the application implements a GGMouseTouchListener, its reference is set as a parameter (this) |
mouseTouched(Actor actor, GGMouse mouse, Point spot) | a callbackmethod, which returns an event if the mouse touches an actor's Sprite |
Location loc = new Location(mouse.getX() - hotSpot.x, mouse.getY() - hotSpot.y); actor.setLocation(loc); |
moves the actor to its new position |
Example 2: Sometimes its useful to smoothly move an object and finally setting it down exactly in a grid cell. In this example, randomly set pearls have to be moved into a one row.
|
![]() |
|
![]() |
// JGameEx22a.java import ch.aplu.jgamegrid.*; import java.awt.*; public class JGameEx22a extends GameGrid implements GGMouseTouchListener, GGMouseListener { private Point hotSpot = null; private Actor nemo = new Actor("sprites/nemo.gif"); public JGameEx22a() { super(300, 300, 1, false); setTitle("Drag Nemo!"); nemo.addMouseTouchListener(this, GGMouse.lPress | GGMouse.lRelease); addMouseListener(this, GGMouse.lDrag); addActor(nemo, new Location(150, 150)); show(); } public boolean mouseEvent(GGMouse mouse) { if (hotSpot == null) return false; Location loc = new Location(mouse.getX() - hotSpot.x, mouse.getY() - hotSpot.y); nemo.setLocation(loc); refresh(); return false; } public void mouseTouched(Actor actor, GGMouse mouse, Point spot) { switch (mouse.getEvent()) { case GGMouse.lPress: hotSpot = spot; break; case GGMouse.lRelease: hotSpot = null; break; } } public static void main(String[] args) { new JGameEx22a(); } } |
addMouseTouchListener(this, GGMouse.lPress | GGMouse.lDrag | GGMouse.lRelease) | registers the MouseTouchListener with the left mouse click, drag and release. Since the application implements a GGMouseTouchListener, its reference is set as a parameter (this) |
case GGMouse.lPress: draggedPearl = actor |
the method lPress of the GGMouseTouchListener returns the actor which is selected with the mouse. The selected pearl is called draggedPearl. This step is important, because it allows the player to set several pearls into the same location. The top one can then be removed again if necessary |
if (draggedPearl != null) | actions only happen when a pearl is selected. Since the click outside a pearl does not return anything, it is necessary to start this query before the next action |
Point mousePos = new Point(mouse.getX(), mouse.getY()); draggedPearl.setPixelLocation(mousePos); |
follows the mouse cursor and moves the selected pearl |
case GGMouse.lRelease: |
after releasing the pearl, it is set exactly in the middle of the cell. The variable draggedPearl is set to null. This allows a new pearl to be selected |
Example 3: clicking on a match, it is removed. The click can be made on any part of the match As in the other examples, the advantages of the GGMouseTouchListener are used. Not the grid cells define the interactive parts, but its Sprites. |
![]() |
// JGameEx25.java import ch.aplu.jgamegrid.*; import java.awt.*; public class JGameEx25 extends GameGrid implements GGMouseTouchListener { private int nbMatches = 20; public JGameEx25() { super(600, 120, 1, false); setTitle("Remove matches"); for (int i = 0; i < nbMatches; i++) { Actor match = new Actor("sprites/match.gif"); addActor(match, new Location(12 + 30 * i, 60)); match.addMouseTouchListener(this, GGMouse.lPress); } show(); doRun(); } public void mouseTouched(Actor actor, GGMouse mouse, Point pix) { actor.removeSelf(); nbMatches--; setTitle("Remove matches. " + nbMatches + " matches remaining."); } public static void main(String[] args) { new JGameEx25(); } } |
addActor(match, new Location(12 + 30 * i, 60)) |
positions the matches in the pixel grid. Registers the MouseTouchListener for each match |
void mouseTouched() actor.removeSelf() |
callbackmethod of the MouseTouchListeners. The touched match is removed |
nbMatches-- | number of matches left is shown in the title bar |