//****************************************************************************** // anim.java: Applet // //****************************************************************************** import java.applet.*; import java.awt.*; //============================================================================== // Main Class for applet anim // //============================================================================== public class anim extends Applet implements Runnable { // THREAD SUPPORT: // m_anim is the Thread object for the applet //-------------------------------------------------------------------------- Thread m_anim = null; private Graphics m_mainScreen; private Graphics m_bufferGraphic; private Image m_bufferImage; private Image m_pic; Dimension m_dimImage; private int m_nCurrImage; private int m_nImgWidth = 0; private int m_nImgHeight = 0; private boolean m_fAllLoaded = false; private int x = 0; private int y = 0; private int x_direction = 1; private int y_direction = 1; // anim Class Constructor //-------------------------------------------------------------------------- public anim() { // TODO: Add constructor code here } // APPLET INFO SUPPORT: // The getAppletInfo() method returns a string describing the applet's // author, copyright date, or miscellaneous information. //-------------------------------------------------------------------------- public String getAppletInfo() { return "Name: anim\r\n" + "Author: George Macdonald\r\n"; } // The init() method is called by the AWT when an applet is first loaded or // reloaded. Override this method to perform whatever initialization your // applet needs, such as initializing data structures, loading images or // fonts, creating frame windows, setting the layout manager, or adding UI // components. //-------------------------------------------------------------------------- public void init() { resize(320, 240); } public void destroy() { } // ANIMATION SUPPORT: // Displays the image at the correct location private void displayImage(Graphics g) { if (!m_fAllLoaded) return; // Draw Image g.drawImage(m_pic, x, y, null); // Update x and change direction if necessary if (x > (size().width - m_nImgWidth)) x_direction = -1; if (x < 0) x_direction = 1; x += x_direction; // Update y and change direction if necessary if (y > (size().height - m_nImgHeight)) y_direction = -1; if (y < 0) y_direction = 1; y += y_direction; } // anim Paint Handler //-------------------------------------------------------------------------- public void paint(Graphics g) { if (m_fAllLoaded) { // Check that the image is the right size ResizeImage(); // Clear the buffer m_bufferGraphic.clearRect(0, 0, 320, 240); // Display the image on the buffer displayImage(m_bufferGraphic); // Copy the buffer onto the main screen g.drawImage(m_bufferImage, 0, 0, null); } else g.drawString("Loading images...", 10, 20); } // The start() method is called when the page containing the applet // first appears on the screen. public void start() { if (m_anim == null) { m_anim = new Thread(this); m_anim.start(); } } // The stop() method is called when the page containing the applet is // no longer on the screen. public void stop() { if (m_anim != null) { m_anim.stop(); m_anim = null; } } // THREAD SUPPORT // The run() method is called when the applet's thread is started. If // your applet performs any ongoing activities without waiting for user // input, the code for implementing that behavior typically goes here. For // example, for an applet that performs animation, the run() method controls // the display of images. //-------------------------------------------------------------------------- public void run() { m_nCurrImage = 0; // If re-entering the page, then the images have already been loaded. // m_fAllLoaded == TRUE. //---------------------------------------------------------------------- if (!m_fAllLoaded) { repaint(); m_mainScreen = getGraphics(); // Load in all the images //------------------------------------------------------------------ MediaTracker tracker = new MediaTracker(this); String strImage; m_pic = getImage(getDocumentBase(), "../graphic/paint/paint01.GIF"); tracker.addImage(m_pic, 0); // Wait until all images are fully loaded //------------------------------------------------------------------ try { tracker.waitForAll(); m_fAllLoaded = !tracker.isErrorAny(); } catch (InterruptedException e) { } if (!m_fAllLoaded) { stop(); m_mainScreen.drawString("Error loading images!", 10, 40); return; } // Get the image width and height m_nImgWidth = m_pic.getWidth(this); m_nImgHeight = m_pic.getHeight(this); } repaint(); while (true) { try { paint(m_mainScreen); } catch (InterruptedException e) { stop(); } } } private void ResizeImage() { // get the window size Dimension dim = size(); int nWidth = dim.width; int nHeight = dim.height; if (m_dimImage != null && m_dimImage.width == nWidth && m_dimImage.height == nHeight) { return; } m_dimImage = new Dimension(nWidth, nHeight); m_bufferImage = createImage(nWidth, nHeight); m_bufferGraphic = m_bufferImage.getGraphics(); } // MOUSE SUPPORT: // The mouseDown() method is called if the mouse button is pressed // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseDown(Event evt, int x, int y) { // TODO: Place applet mouseDown code here return true; } // MOUSE SUPPORT: // The mouseUp() method is called if the mouse button is released // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseUp(Event evt, int x, int y) { // TODO: Place applet mouseUp code here return true; } // MOUSE SUPPORT: // The mouseDrag() method is called if the mouse cursor moves over the // applet's portion of the screen while the mouse button is being held down. //-------------------------------------------------------------------------- public boolean mouseDrag(Event evt, int x, int y) { // TODO: Place applet mouseDrag code here return true; } // MOUSE SUPPORT: // The mouseMove() method is called if the mouse cursor moves over the // applet's portion of the screen and the mouse button isn't being held down. //-------------------------------------------------------------------------- public boolean mouseMove(Event evt, int x, int y) { // TODO: Place applet mouseMove code here return true; } // MOUSE SUPPORT: // The mouseEnter() method is called if the mouse cursor enters the // applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseEnter(Event evt, int x, int y) { // TODO: Place applet mouseEnter code here return true; } // MOUSE SUPPORT: // The mouseExit() method is called if the mouse cursor leaves the // applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseExit(Event evt, int x, int y) { // TODO: Place applet mouseExit code here return true; } // TODO: Place additional applet code here }