//****************************************************************************** // CZoomer.java: Applet // F. Permadi 1996 //****************************************************************************** import java.applet.*; import java.awt.*; //============================================================================== // Main Class for applet CZoomer //============================================================================== public class CZoomer extends Applet implements Runnable { Thread mCZoomer = null; Graphics mOffScrGraphics; Image mImage; Image mOffScrImage; int mImgWidth, mImgHeight; int mScaledWidth, mScaledHeight; int mDirection, mSpeed; boolean mLoaded=false; //------------------------------------------------------------------------- // destroy() is called when when you applet is terminating and being unloaded. //------------------------------------------------------------------------- public void destroy() { // TODO: Place applet cleanup code here mImage=null; } //-------------------------------------------------------------------------- // CZoomer Paint Handler //-------------------------------------------------------------------------- public void update(Graphics g) { if (!mLoaded) { Font font1=new Font("TimesRoman", Font.BOLD, 15); g.setFont(font1); g.setColor(getForeground()); g.drawString("Loading image...",10,size().height/2); } mOffScrGraphics.setColor(getBackground()); mOffScrGraphics.fillRect(0,0,size().width,size().height); mOffScrGraphics.drawImage(mImage,(size().width-mScaledWidth)/2, (size().height-mScaledHeight)/2, mScaledWidth, mScaledHeight,this); g.drawImage(mOffScrImage,0,0,this); } //-------------------------------------------------------------------------- // The start() method is called when the page containing the applet // first appears on the screen. //-------------------------------------------------------------------------- public void start() { if (mCZoomer == null) { mCZoomer = new Thread(this); mCZoomer.start(); } } //-------------------------------------------------------------------------- // The stop() method is called when the page containing the applet is // no longer on the screen. //-------------------------------------------------------------------------- public void stop() { if (mCZoomer != null) { mCZoomer.stop(); mCZoomer = null; } } //-------------------------------------------------------------------------- // The run() method is called when the applet's thread is started. //-------------------------------------------------------------------------- public void run() { repaint(); String speed=getParameter("speed"); if (speed==null) speed="5"; mSpeed=new Integer(speed).intValue(); String strImage=getParameter("imageFileName"); mImage = getImage(getCodeBase(), strImage); // Load the image MediaTracker tracker = new MediaTracker(this); tracker.addImage(mImage, 0); // Wait until image is fully loaded showStatus("Loading image " + strImage); try { tracker.waitForAll(); } catch (InterruptedException e) { } showStatus(""); // get image width and height. mImgWidth = mImage.getWidth(this); mImgHeight = mImage.getHeight(this); mOffScrImage=createImage(size().width, size().height); mOffScrGraphics=mOffScrImage.getGraphics(); mScaledWidth=size().width; mScaledHeight=size().height; mDirection=-mSpeed; mLoaded=true; while (true) { repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { } mScaledWidth+=mDirection; mScaledHeight+=mDirection; if (mDirection==-mSpeed) { if (mScaledWidth<=5 || mScaledHeight<=5) mDirection=mSpeed; } else if (mDirection==mSpeed) { if (mScaledWidth>=size().width || mScaledHeight>=size().height) mDirection=-mSpeed; } } } }