The Java Game Framework.

Examples, tutorials, documentation and download of the Genuts Framework.
Games made with the Genuts Frameworks.
General articles around games.
Links covering all needs around game dev.
Who are we?
Terms of Service

API >> Tutorials >> Fundamentals >> Displaying simple sprites

Your first Genuts Program

This is the first section that teach Genuts basics by looking at example code. This section examines the code for a simple program, DisplaySprites.java. The examples in the following sections will become progressively more difficult as we introduce and explain more features.

Here's an applet of the DisplaySprites program:

And here's the essential code for DisplaySprites (we assume that it exists an initialized Panel in panel variable):


import com.genuts.gameui.*;

[...]

    // Creates the playfield
    PlayField playfield = new PlayField(310, 248);
    playfield.setBackground(Color.black);

[...]

    // Creates 20 balls (4 lines / 5 columns)
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 5; j++) {
        Sprite sprite = new AnimatedSprite(ballImage,
                                           4, 3,
                                           (int) (Math.random()*5));
        int x = j*62 + 15;
        int y = i*62 + 15;
        sprite.setPosition(x, y);
        playfield.addSprite(sprite);
      }
    }

    // Displays the playfield
    panel.add(playfield);

    // Activate the playfield
    playfield.setPause(false);

[...]

Though very simple, this program's architecture is common to all Genuts programs:

  1. Import the pertinent packages.
  2. Set up the playfield.
  3. Set up sprites.

The first line imports the main Genuts package:


import com.genuts.gameui.*;
This package is needed to use Genuts API.

The top-level Genuts container is the PlayField, which provides support for painting and detecting collision between sprites.
The playfield also sends tick events to Tickable objects. A tick is generated at constant time interval and is primarily used for animations.
The playfield is added as a component to an AWT or a Swing component.

Here is the code that sets up and shows the playfield:


    // Creates the playfield
    PlayField playfield = new PlayField(310, 248);
    playfield.setBackground(Color.black);

[...]

    // Displays the playfield
    panel.add(playfield);

And to activate tick events, the playfield needs to be started:


// Activate the playfield
playfield.setPause(false);

Don't forget to stop the playfield when you want to dispose it with:


// Stop the playfield
playfield.stop();

This is useless in the current example but would be mandatory in an Applet.

We then have to display the sprites. First we need to load the image in ballImage. This is not Genut-specific code so you can refer to Sun's documentation on how to do this. However, we will later see that this can be achieved in several ways. Here we picked up the AnimatedSprite, which takes a large picture that holds all the sprite's phases:

We notice that there are 4 images horizontally and 3 vertically. The animation speed will be chosen randomly (between 0 and 4), meaning that the time interval between 2 phases will be up to 4 ticks.

Here is the code:


Sprite sprite = new AnimatedSprite(ballImage,
                                   4, 3,
                                   (int) (Math.random()*5));

Next, we must set the sprites positions in the PlayField with:


sprite.setPosition(x, y);

And add them to the PlayField's list of sprites with:


playfield.addSprite(sprite);
Note: If the sprite implement Tickable interface, like the AnimatedSprite, it is automatically added to the list of Tickable objects.

We add the PlayField to its container, here a Panel.

The next example will go to into more details about bounced balls.

<< Previous Page Next Page >>

API >> Tutorials >> Fundamentals >> Displaying simple sprites