![]() |
|
API >> Tutorials >> Fundamentals >> Displaying simple sprites
Your first Genuts ProgramThis 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):
Though very simple, this program's architecture is common to all Genuts programs:
The first line imports the main Genuts package:
The top-level Genuts container is the PlayField, which provides support for painting and detecting collision between sprites. Here is the code that sets up and shows the playfield:
And to activate tick events, the playfield needs to be started:
Don't forget to stop the playfield when you want to dispose it with:
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:
Next, we must set the sprites positions in the PlayField with:
And add them to the PlayField's list of sprites with:
We add the PlayField to its container, here a Panel. The next example will go to into more details about bounced balls.
API >> Tutorials >> Fundamentals >> Displaying simple sprites
|