/* * JZOOImageFader.java * * Copyright 1998-1999 by Kevan Stannard * See http://www.jzoo.com for more details. * All rights reserved. */ import java.applet.*; import java.awt.*; import java.awt.image.*; import java.util.*; import java.net.*; public class JZOOImageFader extends Applet implements Runnable { private final static String version = "1.0"; private static String ipLicense = ""; Thread thread = null; MediaTracker tracker = new MediaTracker(this); Vector image = new Vector(); Vector link = new Vector(); int appletWidth = 0; int appletHeight = 0; Image buffer = null; Graphics bufferGraphics = null; final int LOADING_IMAGES = 1; final int FADING = 2; final int READY = 3; int status = 0; int numImages = 0; int currImage = 0; int nextImage = 0; int delay = 10 * 5; // 5 seconds int speed = 1; int currDelay = delay; int fade = 0; int image1[] = null; int image2[] = null; int result[] = null; Color bgColor = null; public String toString() { StringBuffer info = new StringBuffer(); info.append("JZOOImageFader\r\n"); info.append("Version: " + version + "\r\n"); info.append("License: " + ipLicense + "\r\n"); info.append("See http://www.jzoo.com for details\r\n"); return info.toString(); } public void init() { // check ip address try { System.out.println(InetAddress.getLocalHost()); } catch (UnknownHostException uhe) { } // print some details System.out.println(this); // start loading images numImages = 0; while (true) { String imageName = getParameter("image" + (numImages + 1)); String imageLink = getParameter("link" + (numImages + 1)); if (imageName == null) { break; } if (imageLink == null) { imageLink = "null"; } //System.out.println("Loading image: " + imageName); Image nextImage = getImage(getCodeBase(), imageName); tracker.addImage(nextImage, numImages++); image.addElement(nextImage); link.addElement(imageLink); } // ensure all images are loading tracker.checkAll(true); status = LOADING_IMAGES; // create buffer image appletWidth = size().width; appletHeight = size().height; buffer = createImage(appletWidth, appletHeight); bufferGraphics = buffer.getGraphics(); // create arrays to hold pixels image1 = new int[appletWidth * appletHeight]; image2 = new int[appletWidth * appletHeight]; result = new int[appletWidth * appletHeight]; // get parameters String param = getParameter("speed"); if (param != null) { speed = Integer.parseInt(param); } param = getParameter("delay"); if (param != null) { delay = Integer.parseInt(param) * 10; } } public void start() { if (thread == null) { thread = new Thread(this); thread.start(); } } public void stop() { if (thread != null && thread.isAlive()) { thread.stop(); thread = null; } } public boolean mouseDown(Event e, int x, int y) { // only follow link if not fading // get link String newLink = (String) link.elementAt(currImage); URL newURL = null; if (!newLink.equals("null")) { try { newURL = new URL(getCodeBase(), newLink); } catch(Exception e2) { newURL = null; } if (newURL == null) { return true; } AppletContext context = getAppletContext(); context.showDocument(newURL); } return true; } public void update(Graphics g) { paint(bufferGraphics); g.drawImage(buffer, 0, 0, this); } public void paint(Graphics g) { if (status == LOADING_IMAGES) { g.drawString("Loading", 20, 20); } if (status == FADING) { Image fadeImage = getFadeImage(fade); g.drawImage(fadeImage, 0, 0, this); fade += speed; if (fade >= 256) { currImage = nextImage; status = READY; } } if (status == READY) { if (numImages == 0) { g.drawString("No images to draw", 20, 20); } else { Image img = (Image) image.elementAt(currImage); g.drawImage(img, 0, 0, this); } } } public void run() { while (true) { repaint(); if (status == LOADING_IMAGES) { //System.out.println(tracker.checkAll()); if (tracker.checkAll()) { status = READY; } } if (status == READY) { currDelay--; if (currDelay <= 0) { currDelay = delay; nextImage = (currImage + 1) % numImages; // get pixels of images Image i1 = (Image) image.elementAt(currImage); Image i2 = (Image) image.elementAt(nextImage); try { PixelGrabber pg1 = new PixelGrabber(i1, 0, 0, appletWidth, appletHeight, image1, 0, appletWidth); PixelGrabber pg2 = new PixelGrabber(i2, 0, 0, appletWidth, appletHeight, image2, 0, appletWidth); pg1.grabPixels(); pg2.grabPixels(); } catch (Exception e) { } // initialise a fade fade = 0; status = FADING; } } try { thread.sleep(100); } catch (Exception e) { } } } public Image getFadeImage(int amount) { //System.out.println(amount); int i1amount = 256-amount; int i2amount = amount; for (int i=0; i> 16) * i1amount) + (((image2[i] & 0x00ff0000) >> 16) * i2amount)) >> 8; int g = ((((image1[i] & 0x0000ff00) >> 8) * i1amount) + (((image2[i] & 0x0000ff00) >> 8) * i2amount)) >> 8; int b = ((((image1[i] & 0x000000ff)) * i1amount) + (((image2[i] & 0x000000ff)) * i2amount)) >> 8; result[i] = 0xff000000 | r<<16 | g<<8 | b; } return createImage(new MemoryImageSource(appletWidth, appletHeight, result, 0, appletWidth)); } }