Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet6.html |
import java.awt.*;
import java.applet.Applet;
/** EXTEND THE JAVA APPLET CLASS AND IMPLEMENT THE RUNNABLE INTERFACE **/
public class AnimatorApplet6 extends Applet implements Runnable
{int frameNumber = -1;
int delay;
Thread animatorThread;
boolean frozen = false;
Font waitfont=new Font("TimesRoman",Font.BOLD,14);
FontMetrics waitfontm = getFontMetrics(waitfont);
String waitstr1, waitstr2 = "";
Font shipfont=new Font("TimesRoman",Font.PLAIN,10);
FontMetrics shipfontm = getFontMetrics(shipfont);
String shipstr = "";
String shipstrpad = "
";
Font titlefont=new Font("TimesRoman",Font.BOLD,24);
FontMetrics titlefontm = getFontMetrics(titlefont);
String titlestr = "";
Color starblue = new Color(0,100,255);
Dimension preImageDim;
// Define preImage variables needed to
Image preImage;
// create the offscreen copy of the image
Graphics preImageGraphics;
// we want to paint on the screen.
Image backgroundstarImage[];
Image AndromedaImage;
Image MilkywayHydrogenImage;
Image PleiadesClusterImage;
Image borgcubeImage;
// Not currently being used.
Image rocketshipImage;
MediaTracker tracker;
int backNumber = 0;
int shipstrIndex = 0;
int xspeedUp = 0;
int yspeedUp = 0;
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet6.html |
public void init()
{setBackground(Color.black);
String str;
int fps = 10;
// Define the default fps (frames per second).
str = getParameter("fps");
// Possibly, get HTML string specifying the
try
// fps.
{if (str != null)
{fps =
Integer.parseInt(str);} // If possible, convert HTML fps to an
}
// integer.
catch (Exception e) {}
// In case the try caused something strange.
delay = (fps > 0) ? (1000/fps):
100; // Convert fps to the milisecond delay
// time between frames.
backgroundstarImage = new
Image[3];
tracker = new MediaTracker(this);
// Spawn the tracker background thread(s)
AndromedaImage = getImage(getCodeBase(),"images/am5hst.jpg");
tracker.addImage(AndromedaImage,0);
MilkywayHydrogenImage =
getImage(getCodeBase(),"images/nHI_alt_skyview.jpg");
tracker.addImage(MilkywayHydrogenImage,0);
PleiadesClusterImage = getImage(getCodeBase(),"images/pleiades2.gif");
tracker.addImage(PleiadesClusterImage,0);
backgroundstarImage[0] =
AndromedaImage;
tracker.addImage(backgroundstarImage[0],0);
backgroundstarImage[1] =
MilkywayHydrogenImage;
tracker.addImage(backgroundstarImage[1],0);
backgroundstarImage[2] =
PleiadesClusterImage;
tracker.addImage(backgroundstarImage[2],0);
//borgcubeImage = getImage(getCodeBase(),"images/borgcube.jpg");
//tracker.addImage(borgcubeImage,0);
rocketshipImage = getImage(getCodeBase(),"images/rocketship.gif");
tracker.addImage(rocketshipImage,0);
}
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet6.html |
public boolean mouseDown(Event e, int x, int y)
{if (frozen)
{frozen = false;
start();}
else
{frozen = true;
animatorThread
= null; // Instead of calling stop() here, since
}
// stop() now nullifies our preImage context,
return true;
// nullify only the animation thread in case
}
// we want to build on our current preImage.
/** GENERATE AND/OR START THE ANIMATION THREAD **/
public void start()
{if (frozen) { }
// The animation is supposed to stay frozen.
else
// Otherwise, generate and/or start the
{if (animatorThread
== null) // animation thread. Apparently,
if run
{animatorThread
= new Thread(this);} // breaks after catching an exception,
animatorThread.start();
// the animatorThread though not null
}
// will still need to be restarted.
}
/** CREATE AND DISPLAY FRAMES OF THE ANIMATION THREAD **/
public void run()
{try
// Start loading the images and
{tracker.waitForAll();}
// wait until done before starting
catch (InterruptedException
e) {}
// the animation.
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// Set the priority low.
long startTime = System.currentTimeMillis();
// Remember start time.
while (Thread.currentThread()
== animatorThread) // The animation loop.
{frameNumber++;
// Advance the frame number.
repaint();
// Display the frame.
try
// Sleep for the delay period.
{startTime += delay;
Thread.sleep(Math.max(0,
startTime-System.currentTimeMillis()));
}
catch (InterruptedException e) {break;} // In case the try caused
}
// something strange, exit
}
// the while loop.
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet6.html |
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet6.html |
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet6.html |
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet6.html |
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet6.html |