//****************************************************************************** // Javamation.java: An Applet that plays a series of animation frames and sound // specified in the HTML page that calls the applet. //****************************************************************************** import java.applet.*; import java.awt.*; //============================================================================== // Main Class for applet Javamation // //============================================================================== public class Javamation extends Applet implements Runnable { // m_Javamation is the Thread object for the applet private Thread m_Javamation = null; // The following variables are used to store the values of the // parameters input from the HTML page. private int m_fps = 5; private int m_numImages = 21; private String m_imageFile = "pg_a"; // Parameter names. private final String PARAM_fps = "fps"; private final String PARAM_numImages = "numImages"; private final String PARAM_imageFile = "imageFile"; // Use a mediatracker to monitor image file loading MediaTracker m_mediaTracker = new MediaTracker(this); // variables to store frame information int m_nFrameNumber = 0; Image m_frame[]; // AudioClip objects AudioClip jabber; AudioClip silence; // Button to stop animation Button m_stopButton = new Button("Stop"); // Button to rewind animation Button m_rewindButton = new Button("Rewind"); // Button to play animation Button m_playButton = new Button("Play"); // Javamation Class Constructor public Javamation() { } // The getAppletInfo() method returns a string describing the applet's // author, copyright date, or miscellaneous information. public String getAppletInfo() { return "Name: Javamation\r\n" + "Author: Tom Sephton\r\n" + "Created with Microsoft Visual J++ Version 1.1"; } // The getParameterInfo() method returns an array of strings describing // the parameters understood by this applet. // Javamation Parameter Information: // { "Name", "Type", "Description" }, public String[][] getParameterInfo() { String[][] info = { { PARAM_fps, "int", "Frames per second" }, { PARAM_numImages, "int", "Total frames in this animation" }, { PARAM_imageFile, "String", "Image file base name" }, }; return info; } // The init() method is called by the AWT when an applet is first loaded or reloaded public void init() { //The following code retrieves the value of each parameter // specified with the tag and stores it in a member // variable. String param; // fps: Frames per second param = getParameter(PARAM_fps); if (param != null) m_fps = Integer.parseInt(param); // numImages: Total frames in this animation param = getParameter(PARAM_numImages); if (param != null) m_numImages = Integer.parseInt(param); // imageFile: Image file base name param = getParameter(PARAM_imageFile); if (param != null) m_imageFile = param; resize(320, 240); // Set up a BorderLayout manager setLayout(new BorderLayout()); // Create a Panel to contain the play control buttons Panel controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); controlPanel.add(m_stopButton); controlPanel.add(m_rewindButton); controlPanel.add(m_playButton); // Add the Control Panel to the applet layout add("South", controlPanel); // Load animation image frames m_frame = new Image[m_numImages]; String sImageFile; int nImageNum; for (int i = 0; i < m_numImages; i++) { nImageNum = i + 1; sImageFile = m_imageFile + ((nImageNum < 10)? "000":"00") + nImageNum + ".jpg"; m_frame[i] = getImage(getDocumentBase(), sImageFile); // add this frame to the mediatracker m_mediaTracker.addImage(m_frame[i], 0); } } // destroy() is called when applet is terminating and being unloaded. public void destroy() { } // Javamation Paint Handler public void paint(Graphics g) { if (m_mediaTracker.checkAll()) { // see if there was an error if (m_mediaTracker.isErrorAny()) { // report the error if any g.drawString("Error loading image", 100, 120); return; } // draw the current frame g.drawImage(m_frame[m_nFrameNumber], 0, 0, null); // update the frame number until the last frame is reached m_nFrameNumber++; if (m_nFrameNumber > m_numImages) { m_nFrameNumber = 0; } } else { g.drawString("Loading Images...", 100, 100); } } // Update method simply calls paint public void update(Graphics g) { paint(g); } // Javamation event handler reacts to control buttons public boolean handleEvent(Event evt) { // If the action was a button push... if (evt.target instanceof Button) { // ...check which button was pushed switch(evt.id) { case Event.ACTION_EVENT: Button button = (Button)evt.target; // If it was the Stop button... if(button.getLabel().equals("Stop")) { stop(); break; } // If it was the Rewind button... else if(button.getLabel().equals("Rewind")) { stop(); m_nFrameNumber = 0; repaint(); break; } // If it was the Play button... else if(button.getLabel().equals("Play")) { start(); break; } default: return false; } return true; } else return false; } // The start() method is called when the page containing the applet // first appears on the screen. This method starts execution of the // applet's thread. public void start() { if (m_Javamation == null) { m_Javamation = new Thread(this); m_Javamation.start(); } } // The stop() method is called when the page containing the applet is // no longer on the screen or when a button press calls this method. // This method stops execution of the applet's thread. public void stop() { if (m_Javamation != null) { m_Javamation.stop(); m_Javamation = null; } else if (m_nFrameNumber == m_numImages) { m_Javamation.stop(); m_Javamation = null; } } // The run() method is called when the applet's thread is started. // This starts display of animation images by updating frame number then calling repaint. // The run() method also loads and plays the sound file connected to the animation. public void run() { try { // Set the frame number to 0 m_nFrameNumber = 0; // force a repaint repaint(); // force all images to be loaded first m_mediaTracker.waitForAll(); // resize screen to match loaded image size resize(m_frame[0].getWidth(null), m_frame[0].getHeight(null)); // Set up sleep delay time based on frame rate int nDelay = 1000/m_fps; // Get audio clip that goes with the animation String sSoundClip = new String(m_imageFile + ".au"); AudioClip jabber = getAudioClip(getCodeBase( ), sSoundClip); // Play the audio clip jabber.play( ); while (true) { repaint(); Thread.sleep(nDelay); } } catch (InterruptedException e) { stop(); } } }