import java.applet.Applet; import java.awt.*; import java.awt.event.*; import com.genuts.gameui.*; /** * Display moving balls and a simple playfield with all tiles of the example.
* The size of each tile is automaticaly computed. */ public class PathFinder extends Applet { private PlayField playfield = null; public void init() { setLayout(null); // Loading images Image ballImage = getImage(getClass().getResource("anim-split.gif")); Image tileImage = getImage(getClass().getResource("tile.gif")); MediaTracker tracker = new MediaTracker(this); tracker.addImage(ballImage, 0); tracker.addImage(tileImage, 0); try { tracker.waitForID(0); } catch (InterruptedException e) { System.out.println("Loading interrupted"); } // Gets the size of a tile Tiles.tileSize = tileImage.getWidth(null); // Creates the playfield SpriteCollisionManager collisionManager = new SpriteCollisionManager(10); playfield = new PlayField(collisionManager, Tiles.tiles[0].length*Tiles.tileSize, Tiles.tiles.length*Tiles.tileSize); playfield.setBackground(Color.white); // Disposes tiles from class Tiles for (int y = 0; y < Tiles.tiles.length; y++) { for (int x = 0; x < Tiles.tiles[y].length; x++) { if (Tiles.tiles[y][x]) { Sprite tile = new Sprite(tileImage); tile.setPosition(x*Tiles.tileSize, y*Tiles.tileSize); playfield.addSprite(tile); } } } // Disposes 20 balls for (int cpt = 0; cpt < 20; cpt++) { AnimatedSprite anim = new AnimatedSprite(ballImage, 8, 1, 2); anim.setCurrentPictureNumber((int) (Math.random()*8)); MovingBall moving = new MovingBall(anim, 1, 1); if ((int) (Math.random()*2) == 0) { moving.setClockWise(false); } playfield.addSprite(moving); while (!moving.setPosition((int) (Math.random()*playfield.getPreferredWidth()), (int) (Math.random()*playfield.getPreferredHeight()))); } // Displays the playfield add(playfield); } public void start() { // Activate the playfield playfield.setPause(false); } public void stop() { if (playfield != null) { playfield.stop(); } } }