home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.Component;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.MediaTracker;
- import java.awt.image.ImageObserver;
-
- public class AnimationMT extends Applet implements Runnable {
- Thread m_AnimationMT;
- private int m_fps = 10;
- private String m_imageFile = "LJN";
- private int m_numImages = 22;
- private final String PARAM_fps = "fps";
- private final String PARAM_imageFile = "imageFile";
- private final String PARAM_numImages = "numImages";
- MediaTracker m_mediaTracker = new MediaTracker(this);
- int m_nFrameNumber;
- Image[] m_frame;
-
- public void start() {
- if (this.m_AnimationMT == null) {
- this.m_AnimationMT = new Thread(this);
- this.m_AnimationMT.start();
- }
-
- }
-
- public String[][] getParameterInfo() {
- String[][] info = new String[][]{{"fps", "int", "Frame rate"}, {"imageFile", "String", "Name of file seq"}, {"numImages", "int", "Number of images"}};
- return info;
- }
-
- public void stop() {
- if (this.m_AnimationMT != null) {
- this.m_AnimationMT.stop();
- this.m_AnimationMT = null;
- }
-
- }
-
- public String getAppletInfo() {
- return "Name: AnimationMT\r\n" + "Author: Stephen R. Davis\r\n" + "Created for Learn Java Now (c)";
- }
-
- public void run() {
- try {
- this.m_nFrameNumber = 0;
- ((Component)this).repaint();
- this.m_mediaTracker.waitForAll();
- ((Applet)this).resize(this.m_frame[0].getWidth((ImageObserver)null), this.m_frame[0].getHeight((ImageObserver)null));
- int nDelay = 1000 / this.m_fps;
-
- while(true) {
- ((Component)this).repaint();
- Thread.sleep((long)nDelay);
- }
- } catch (InterruptedException var3) {
- this.stop();
- }
- }
-
- public void destroy() {
- }
-
- public void init() {
- String param = ((Applet)this).getParameter("fps");
- if (param != null) {
- this.m_fps = Integer.parseInt(param);
- }
-
- param = ((Applet)this).getParameter("imageFile");
- if (param != null) {
- this.m_imageFile = param;
- }
-
- param = ((Applet)this).getParameter("numImages");
- if (param != null) {
- this.m_numImages = Integer.parseInt(param);
- }
-
- ((Applet)this).resize(320, 240);
- this.m_frame = new Image[this.m_numImages];
-
- for(int i = 0; i < this.m_numImages; ++i) {
- int nImageNum = i + 1;
- String sImageFile = this.m_imageFile + (nImageNum < 10 ? "0" : "") + nImageNum + ".gif";
- this.m_frame[i] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), sImageFile);
- this.m_mediaTracker.addImage(this.m_frame[i], 0);
- }
-
- }
-
- public void paint(Graphics g) {
- if (this.m_mediaTracker.checkAll()) {
- if (this.m_mediaTracker.isErrorAny()) {
- g.drawString("Error loading an image", 10, 20);
- return;
- }
-
- g.drawImage(this.m_frame[this.m_nFrameNumber], 0, 0, (ImageObserver)null);
- ++this.m_nFrameNumber;
- if (this.m_nFrameNumber >= this.m_numImages) {
- this.m_nFrameNumber = 0;
- }
- } else {
- g.drawString("Loading images...", 10, 20);
- }
-
- }
-
- public void update(Graphics g) {
- this.paint(g);
- }
- }
-