/** An example applet that wraps the Asteroids animation. */ // Comments in square brackets are general comments that hold true of // any applet import uwcse.graphics.*; import uwcse.animation.*; // [Each GWindow-based application should extend AppletGWindow, which // is itself an extension of Applet] public class AsteroidsApplet extends AppletGWindow { private Stage stage; private boolean animationStarted; // [code that should run when the applet is first loaded] public void init() { // [always do this first, to take care of any work that the // AppletGWindow needs] super.init(); // create the window for the applet GWindow window = new GWindow(this); // create the stage this.stage = new Stage(window); // create the asteriods animation object Asteroids l = new Asteroids(this.stage); // create the handler for input & timer events; register it // with the stage AsteroidsEventHandler h = new AsteroidsEventHandler(this.stage); this.stage.registerEventHandler(h); // start sending timer events, one every three seconds this.stage.startTimerEvents(3000); this.animationStarted = false; } // [code that should run whenever the applet becomes visible (the // first time the applet runs, as well as any time that the web // page containing the applet is exposed)] public void start() { // [always do this first, to take care of any work that the // AppletGWindow needs] super.start(); if (! this.animationStarted) { // start up the animation now this.animationStarted = true; // do the animation in the background, so this start // method finishes this.stage.animateInBackground(); } else { // we're restarting the applet this.stage.resumeAnimation(); } } // [code that should run whenever the applet becomes (temporarily) // invisible, e.g. when the web page containing the applet is // hidden] public void stop() { // [always do this first, to take care of any work that the // AppletGWindow needs] super.stop(); this.stage.suspendAnimation(); } // [code that should run when the applet finishes] public void destroy() { this.stage.quitAnimation(); // [always do this last, to finish with any work that the // AppletGWindow needs] super.destroy(); } }