JavaScript Tip of the Week for December 9, 1996: Image Menus for Everyone | Source Code | WebReference

JavaScript Tip of the Week for December 9, 1996: Image Menus for Everyone | Source Code

JavaScript Tip of the Weekfor December 9, 1996: Source Code: Image Menus for Everyone

Important: If you want to use the dynamic image menu, you must first download the dynamic image class file (only 3k) and place it in a working directory. Just refer to the class file in the APPLET tag as you would any other file.

To include the applet(s) for each dynamic image in your page, use the applet tag and these paramters, with your own images and sounds included.
<APPLET CODE = "dynimg.class" WIDTH = 148 HEIGHT = 27>
<PARAM NAME = "inactive_image" VALUE = "this_week_inact.jpg">
<PARAM NAME = "active_image" VALUE = "this_week_act.jpg">
<PARAM NAME = "active_sound" VALUE = "drip.au">
<PARAM NAME = "clicked_image" VALUE = "this_week_selected.jpg">
<PARAM NAME = "clicked_sound" VALUE = "click.au">
<PARAM NAME = "bgcolor" VALUE = "#FFFFFF">
<PARAM NAME = "url" VALUE = "../this_week/index.html">
<PARAM NAME = "target" VALUE = "_self">
</APPLET>
Here is the source code for the dynamic image menus Java applet:
/* This code is Copyright (c) 1996 Nick Heinle, all rights reserved.
 * In order to receive the right to license this code for use on your
 * site the original code must be copied from the Web site 
 * webreference.com/javascript/. License is granted to user to reuse 
 * this code on their own Web site if and only if this entire copyright
 * notice is included. Code written by Nick Heinle of webreference.com.
 */
import java.applet.*;
import java.awt.*;
import java.net.*;
public class dynimg extends java.applet.Applet implements Runnable {
    
    AudioClip sndclicked; 
    AudioClip sndactive;  
    Color bgColor = getBackground();
    Image imginact;      
    Image imgact;      
    Image imgclicked;    
    Image imgcurrent;
    String href;
    Thread running;
  
    public void init() { 
    String p;
    p = getParameter("bgcolor");
        if (p != null) {
        bgColor = convColor(p);
        setBackground(bgColor);
        }
    p = getParameter("url");
        if (p != null) href = p;
    p = getParameter("target");
        if (p != null) target = p;
    p = getParameter("inactive_image");
        if (p != null) imginact = getImage(getCodeBase(), p);
    p = getParameter("active_image");
        if (p != null) imgact = getImage(getCodeBase(), p);
    p = getParameter("clicked_image");
        if (p != null) imgclicked = getImage(getCodeBase(), p);
    p = getParameter("clicked_sound");
        if (p != null) sndclicked = getAudioClip(getCodeBase(), p);
    p = getParameter("active_sound");
        if (p != null) sndactive = getAudioClip(getCodeBase(), p);
    }
    public void start() {
        if (running == null) {
        running = new Thread(this);
        running.start();
        }
    }
    public void stop() {
        if (running != null) {
        running.stop();
        running = null;
        }   
    }
    public void run() {
    imgcurrent = imginact;
    repaint();
    getAppletContext().showStatus("Dynamic Image, Copyright (c) 1996 Nick Heinle");
        try { Thread.sleep(2000); }
        catch (InterruptedException e) {}
        getAppletContext().showStatus("");
    }
    public boolean mouseDown (Event e, int x, int y) {
        if (imgclicked!= null){
        imgcurrent = imgclicked;
        repaint();
        }
        if (sndclicked != null) sndclicked.play();
    return true;
    }
    public boolean mouseUp (Event e, int x, int y) {
        if (imgact != null){
        imgcurrent = imgact;
        repaint(); 
        }
        if (href != null) {
        URL url;
            try { 
            url = new URL(getDocumentBase(), href);
                if (target != null) getAppletContext().showDocument(url, target);
                else getAppletContext().showDocument(url);
            } 
            catch (MalformedURLException err) {}
        }
    return true;
    }
    public boolean mouseEnter(Event e, int x, int y) {
        if (imgact != null){
        imgcurrent = imgact;
        repaint(); 
        }
        if (sndactive != null) sndactive.play();
        if (href != null) getAppletContext().showStatus(href);
    return true;
    }
    public boolean mouseExit(Event e, int x, int y) {
        if (imginact != null){
        imgcurrent = imginact;
        repaint(); 
        }
        if (href != null) getAppletContext().showStatus("");
    return true;
    }
    Color convColor(String c) {
        int val = 0;
        try {
            if (c.startsWith("#")) {
            val = Integer.parseInt(c.substring(1), 16);
            } 
            else if (c.startsWith("0") && c.length() > 1) {
            val = Integer.parseInt(c.substring(1), 8);
            } 
            else {
            val = Integer.parseInt(c, 10);
            }
            return new Color(val);
        } 
        catch (NumberFormatException e) {
        return null;
        }
    }
    public void paint(Graphics g) {
    g.drawImage(imgcurrent, 0, 0, this);
    }
    public void update(Graphics g) {
    paint(g);
    }
}