/*-----------------------------------------------------------------*\ AdBanner Applet - by Jay Tomlin Multiple-image slide show... each image has its own associated URL, to which the user is sent upon clicking (the URL target changes with the image) Usage: For best results, the width w and height h of the applet should match the dimensions of the images, all of which should be the same size. The number of images need not equal the number of URLs--a difference between the two results in an interesting pattern of offset--but when the same number of each is provided, clicking on an image loads its associated URL - into the main browser window if 'targetWindow' is set to "_top" or left blank or - into the specified window (or frame) if 'targetWindow' is set to any other value The time interval between frame changes, in integer seconds, is indicated by the frameDelay parameter. \*-----------------------------------------------------------------*/ import java.applet.*; import java.awt.*; import java.net.*; import java.util.*; public class AdBanner extends java.applet.Applet implements Runnable { // Declare the class variables String imageArray, urlArray, targetWindow; StringTokenizer imageTokens, urlTokens; int frameDelay, currentImage, currentURL, imageCount, urlCount; Image IMAGES[]; URL URLS[]; Thread kicker; public void init() { // Get parameters from HTML Tag, providing defaults where appropriate imageArray = getParameter("imageArray"); urlArray = getParameter("urlArray"); targetWindow = getParameter("targetWindow"); frameDelay = new Integer(getParameter("frameDelay")).intValue(); if (frameDelay == 0) { frameDelay = 5; } // default 5 second delay if (targetWindow==null) { targetWindow = "_top";} // default to main window // Come to my place if you need help: if (urlArray==null) { urlArray = "http://php.indiana.edu/~ltomlin/java.html"; } // Split the imageArray and urlArray into tokens String delim = new String(","); imageTokens = new StringTokenizer(imageArray, delim, false); urlTokens = new StringTokenizer(urlArray, delim, false); // Now create two arrays, IMAGES, and URLS (to hold Image & URL objects) // 1. Create the Image array object with the proper number of images: IMAGES = new Image[imageTokens.countTokens()]; int i=0; while(imageTokens.hasMoreTokens()) { getAppletContext().showStatus("Loading image " + i+1); try { String picture = imageTokens.nextToken().toString(); IMAGES[i] = getImage(getCodeBase(), picture); i++; } catch (NoSuchElementException e) {;} } // ends while() // 2. Create the URL array object with the proper number of URLs: URLS = new URL[urlTokens.countTokens()]; int j=0; while(urlTokens.hasMoreElements()) { try { try { URLS[j] = new URL(urlTokens.nextElement().toString()); j++; } catch (MalformedURLException e) {;} } catch (NoSuchElementException e) {;} } // ends while() imageCount = i; // the total actaul number of images loaded (no offset) urlCount = j; // the total actual number of URLs loaded (no offset) currentImage = 0; currentURL = 0; } // ends init() public void start() { // Start the thread going if (kicker == null) { kicker = new Thread(this); kicker.start(); // starts the run() method } // ends if } // ends start() public void paint(Graphics g) { update(g); // this should reduce flicker } // ends paint() public void update(Graphics g) { if (IMAGES[currentImage] != null) { g.drawImage(IMAGES[currentImage], 0, 0, this); } // ends unless() } //ends update() public boolean handleEvent(Event e) { // When the mouse is over the applet, display the currentURL in the browser's Status Window if (e.id == Event.MOUSE_MOVE) { this.getAppletContext().showStatus(URLS[currentURL].toString()); } // ends if // On click, open the current URL in the target Window: if (e.id == Event.MOUSE_UP) { // not mouse down! this.getAppletContext().showDocument(URLS[currentURL], targetWindow); } // ends if // When the mouse leaves the applet, clear the browser's Status Window if (e.id == Event.MOUSE_EXIT) { this.getAppletContext().showStatus(""); } // ends if return true; } // ends handleEvent() public void run() { while (kicker != null) { // slide-show thread goes here for(;;) { // loop forever if (currentImage >= imageCount) { currentImage=0; } if (currentURL >= urlCount) { currentURL=0; } repaint(); try { kicker.sleep(1000 * frameDelay); } catch (InterruptedException e) {;} currentImage++; currentURL++; } // ends for(;;) } // ends while() } // ends run() public void stop() { kicker = null; } } //////////////////////////////////////////////////////////////////////// ends class