Each actor and object appearing in the game grid are objects extended from the class Actor. This already reflects a perfect example of object-oriented programming. Each actor has a sprite assigned through adding the picture's path to the actor's constructor. (It is also possible to add more than one picture to one actor.) | ![]() |
The basic idea of JGameGrid is to give each actor a dynamic "existance". As soon as the button Run in the navigation bar is clicked, a simulation cycle starts by calling the method act() periodically. The simulation periode can be change by changing the sliders position. After each cycle the background is reloaded. This makes the realization of animations easier (this concept was borrowed from Greenfoot).
Clicking the button Step the simulation cycle is executed once. This allows the user to follow the animation step by step. Clicking Reset all actors are set back to their starting positions. Each animation can be paused by clicking on Pause and resumed with pressing the button Run or Step. |
|
Example 1: The actor "nemo" moves horizontally from cell to cell and disappears on the right
|
![]() |
class Fish extends Actor | The class Fish is extended from the class Actor and inherits all non-private methods |
public Fish() { super("sprites/nemo.gif"); } |
Inside the constructor of the class Fish the path to the sprite is added |
act() | The method act() defines what the actor is supposed to do |
move() | This moves the actor to the next cell. When initializing an actor, its starting direction of 0° is set (east) |
Fish nemo = new Fish() | This initializes an object nemo of the class Fish. Since Fish extended the class Actor, nemo is an actor |
addActor(nemo, new Location (1 , 4)) | The actor is added to the game grid at the given location. In a grid of 10x10 cells the coordinates x and y can have a value of 0 to 9. The sprite of the actor is centered to the cell and can overlap the cell's border |
show() | The game window and all its actors are shown |
If you do not want to strictly follow the object-oriented design of java programming, the object nemo can be initialized as an instance of the class Actor inside the main class. Since the method act() is called automatically by the class GameGrid, the actions of the actors can be defined inside the main class as well. This alternative allows the user to drop the class Fish and makes the program code easier.
// ActorEx2.java import ch.aplu.jgamegrid.*; import java.awt.Color; public class ActorEx2 extends GameGrid { private Actor nemo = new Actor("sprites/nemo.gif"); public ActorEx2() { super(10, 10, 60, Color.red); addActor(nemo, new Location(1, 4)); show(); } public void act() { nemo.move(); } public static void main(String[] args) { new ActorEx2(); } } |
private Actor nemo = new Actor("sprites/nemo.gif") | This initializes the instance nemo of the class Actor. The path of the sprite needs to be set. Since nemo is used inside the method act() it must be declared as an instance variable |
nemo.move() | Since move() is a method of the class Actor, nemo must be written in front of it |