HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Applets Make them Worth the Wait"
Vol. 1, Issue 2, P.52

	

Listing 1

import java.awt.*;
import java.net.*;
import java.applet.*;

public class FlashSplash extends Applet
        implements AppletStub, Runnable {
    //
    // Private member variables
    //
    private String m_strTargetAppletClassName;
    private Applet m_appletTarget;
    private URL m_urlSplashImage;
    private Image m_imgSplash;
    private boolean m_fStarted;

    //
    // In init, a background thread responsible for downloading
    // the target Applet class is created after the splash image is 
downloaded.
    //
    public void init() {
        // Gets splash image URL and target class. This code assumes 
URLs are
        // relative URLs.
        try {
            m_urlSplashImage = new URL(getDocumentBase(),
                    getParameter("splash-SplashImage"));
            m_strTargetAppletClassName =
                    getParameter("splash-TargetApplet");
        } catch(MalformedURLException e) {
            System.out.println("FlashSplash loading problem: " + e);
            m_urlSplashImage = null;
        }

        // Download image.
        try {
            MediaTracker mt = new MediaTracker(this);
            m_imgSplash = getImage(m_urlSplashImage);
            mt.addImage(m_imgSplash, 0);
            mt.waitForID(0);
            repaint();
        } catch(Exception e) {
            System.out.println("Splash image loading problem: " + e);
            m_urlSplashImage = null;
        }

        // Start background thread to download target Applet class.
        Thread t = new Thread(this);
        t.start();
    }

    //
    // start() method passes through to target Applet, if created.
    //
    public void start() {
        if(null != m_appletTarget)
            m_appletTarget.start();
        m_fStarted = true;
    }

    //
    // stop() method passes through to target Applet, if created.
    //
    public void stop() {
        if(null != m_appletTarget)
            m_appletTarget.stop();
        m_fStarted = false;
    }

    //
    // destroy() passes through to target Applet, if created.
    //
    public void destroy() {
        if(null != m_appletTarget)
            m_appletTarget.destroy();
    }

    //
    // In paint, only draw the splash image.
    //
    public void paint(Graphics g) {
        if(null != m_imgSplash)
            g.drawImage(m_imgSplash, 0, 0, this);
    }

    //
    // Runnable interface methods: run.
    // run() is responsible for loading the target applet class
    // and creating an instance.
    //
    public void run() {
        try {
            System.out.println("Target applet: " + 
                               m_strTargetAppletClassName);
            Class c = Class.forName(m_strTargetAppletClassName);
            Applet a = (Applet)c.newInstance();
            a.setStub((AppletStub)this);
            setLayout(new BorderLayout());
            add("Center", a);
            validate();
            
            a.init();
            m_appletTarget = a;
            if(m_fStarted)
                m_appletTarget.start();
        } catch (Exception e) {
            System.out.println("Trouble loading target Applet class: "
                               + e);
            m_appletTarget = null;
        }
    }

    //
    // Methods of the AppletStub interface. These implementations
    // make FlashSplashes simple proxy AppletStubs. These methods
    // are already implemented sufficiently by inherited implementations
    // from the Applet class: isActive, getDocumentBase, getCodeBase,
    // getParameter, getAppletContext. To be implemented: appletResize
    //
    public void appletResize(int width, int height) {
        resize(width, height);
    }
}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.