home *** CD-ROM | disk | FTP | other *** search
Wrap
import java.applet.Applet; import java.applet.AudioClip; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Rectangle; import java.awt.image.ImageObserver; public class Animation extends Applet implements Runnable { Thread m_Animation; private Graphics m_Graphics; private Image[] m_Images; private int m_nCurrImage; private int m_nImgWidth; private int m_nImgHeight; private boolean m_fAllLoaded; private int m_nSleepTime; private int m_fps = 10; private String m_imageFile = ""; private int m_numImages = 22; private final String PARAM_fps = "fps"; private final String PARAM_imageFile = "imageFile"; private final String PARAM_numImages = "numImages"; public void start() { if (this.m_Animation == null) { this.m_Animation = new Thread(this); this.m_Animation.start(); } } public String[][] getParameterInfo() { String[][] info = new String[][]{{"fps", "int", "Frame rate"}, {"imageFile", "String", "Name of the image seq"}, {"numImages", "int", "Number of images"}}; return info; } public void stop() { if (this.m_Animation != null) { this.m_Animation.stop(); this.m_Animation = null; } } private void displayImage(Graphics g) { if (this.m_fAllLoaded) { g.drawImage(this.m_Images[this.m_nCurrImage], (((Component)this).size().width - this.m_nImgWidth) / 2, (((Component)this).size().height - this.m_nImgHeight) / 2, (ImageObserver)null); g.setColor(Color.white); g.drawString("Image #" + this.m_nCurrImage, 10, 40); } } public String getAppletInfo() { return "Name: Animation\r\n" + "Author: Stephen R. Davis\r\n" + "Created for Learn Java Now (c)"; } public void run() { this.m_nCurrImage = 0; if (!this.m_fAllLoaded) { ((Component)this).repaint(); this.m_Graphics = ((Component)this).getGraphics(); this.m_Images = new Image[this.m_numImages]; MediaTracker tracker = new MediaTracker(this); for(int i = 1; i <= this.m_numImages; ++i) { String strImage = this.m_imageFile + (i < 10 ? "0" : "") + i + ".gif"; this.m_Images[i - 1] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), strImage); tracker.addImage(this.m_Images[i - 1], 0); } try { tracker.waitForAll(); this.m_fAllLoaded = !tracker.isErrorAny(); } catch (InterruptedException var5) { } if (!this.m_fAllLoaded) { this.stop(); this.m_Graphics.drawString("Error loading images!", 10, 40); return; } this.m_nImgWidth = this.m_Images[0].getWidth(this); this.m_nImgHeight = this.m_Images[0].getHeight(this); } ((Component)this).repaint(); String sSoundClip = new String(this.m_imageFile + ".au"); AudioClip ac = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), sSoundClip); ac.loop(); while(true) { try { this.displayImage(this.m_Graphics); ++this.m_nCurrImage; if (this.m_nCurrImage == this.m_numImages) { this.m_nCurrImage = 0; } Thread.sleep((long)this.m_nSleepTime); } catch (InterruptedException var6) { 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); if (this.m_fps != 0) { this.m_nSleepTime = 1000 / this.m_fps; } } public void paint(Graphics g) { if (this.m_fAllLoaded) { Rectangle r = g.getClipRect(); g.clearRect(r.x, r.y, r.width, r.height); this.displayImage(g); } else { g.drawString("Loading images...", 10, 20); } } }