/*
* @(#)ImageChanger.java
*
* Copyright (c) 1996,1997 NAKAGAWA Masami
*
* Permission to use, copy, modify, and distribute this software
* for NON-COMMERCIAL purpose and without fee is hereby granted.
*/
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.net.*;
/** A class of GIF(JPEG) image changer to decorate a Web page
* @author NAKAGAWA Masami
* @version 2.01, 9 Aug 1997
*/
public class ImageChanger extends Applet implements Runnable {
final int FADEMAX = 16;
final int FADEWID = 64;
final int EFFECTNUM = 20;
final int PADX = 10;
final int PADY = 10;
final int NONE = 0;
final int SCROLLUP = 1;
final int SCROLLDOWN = 2;
final int SCROLLRIGHT = 3;
final int SCROLLLEFT = 4;
final int TEARVERTICAL = 5;
final int TEARHORIZONTAL = 6;
final int OPEN = 7;
final int CLOSE = 8;
final int FADE = 9;
final int MOSAIC = 10;
final int TEARUP = 11;
final int TEARDOWN = 12;
final int TEARRIGHT = 13;
final int TEARLEFT = 14;
final int SLIDEUP = 15;
final int SLIDEDOWN = 16;
final int SLIDERIGHT = 17;
final int SLIDELEFT = 18;
final int SHRINKVERTICAL = 19;
final int SHRINKHORIZONTAL = 20;
Dimension mySize;
MediaTracker tracker;
Image[] images;
String[] imageFile;
int[] change;
int[] time;
String imageDir = ".";
int imageNum;
int defaultChange;
int defaultTime = 5000;
Color fadeColor = Color.black;
Color bgColor = Color.black;
Color fontColor = Color.white;
Color fontShadowColor;
Color fontLineColor;
Color fontBackColor;
int scrollSpeed = 6;
int changeSpeed = 3;
int fadeTime = 80;
Image[] fadeImage = new Image[FADEMAX];
Image mosaicImage;
Thread kicker;
int current;
Graphics offScreen;
Graphics onScreen;
Image offImage;
boolean drawWhileLoading = true;
int mosaicWidth = 16;
int mosaicHeight = 16;
int mosaicSpeed = 4;
Random rand;
URL[] url;
boolean randomOrder = false;
boolean randomEffect = false;
boolean cursorIn = false;
boolean autoLink = false;
boolean linked = false;
AppletContext context;
String[] title;
Point[] titlePos;
Rectangle[] titleBackArea;
Font font;
FontMetrics metrics;
String target;
String frame;
public void init() {
setBackground(bgColor);
String s;
s = getParameter("imageDir");
if (s != null) imageDir = s;
s = getParameter("change");
if (s != null) defaultChange = Integer.parseInt(s);
s = getParameter("time");
if (s != null) defaultTime = Integer.parseInt(s);
s = getParameter("fadeColor");
if (s != null) fadeColor = new Color(Integer.parseInt(s, 16));
s = getParameter("bgColor");
if (s != null) bgColor = new Color(Integer.parseInt(s, 16));
s = getParameter("fontColor");
if (s != null) fontColor = new Color(Integer.parseInt(s, 16));
s = getParameter("fontLineColor");
if (s != null) fontLineColor = new Color(Integer.parseInt(s, 16));
s = getParameter("fontShadowColor");
if (s != null) fontShadowColor = new Color(Integer.parseInt(s, 16));
s = getParameter("fontBackColor");
if (s != null) fontBackColor = new Color(Integer.parseInt(s, 16));
s = getParameter("scrollSpeed");
if (s != null) scrollSpeed = Integer.parseInt(s);
s = getParameter("changeSpeed");
if (s != null) changeSpeed = Integer.parseInt(s);
s = getParameter("fadeTime");
if (s != null) fadeTime = Integer.parseInt(s) / FADEMAX;
s = getParameter("drawWhileLoading");
if (s != null) drawWhileLoading = Boolean.valueOf(s).booleanValue();
s = getParameter("mosaicWidth");
if (s != null) mosaicWidth = Integer.parseInt(s);
s = getParameter("mosaicHeight");
if (s != null) mosaicHeight = Integer.parseInt(s);
s = getParameter("mosaicSpeed");
if (s != null) mosaicSpeed = Integer.parseInt(s);
s = getParameter("randomOrder");
if (s != null) randomOrder = Boolean.valueOf(s).booleanValue();
s = getParameter("randomEffect");
if (s != null) randomEffect = Boolean.valueOf(s).booleanValue();
s = getParameter("fontSize");
font = new Font("Dialog", Font.PLAIN, (s == null)?12:Integer.parseInt(s));
s = getParameter("target");
if (s != null) target = s;
s = getParameter("frame");
if (s != null) frame = s;
s = getParameter("autoLink");
if (s != null) autoLink = Boolean .valueOf(s).booleanValue();
mySize = size();
rand = new Random();
context = getAppletContext();
initImages();
initTitles();
}
private void initImages() {
String s = getParameter("images");
StringTokenizer t1 = new StringTokenizer(s, ",");
imageNum = t1.countTokens();
images = new Image[imageNum];
imageFile = new String[imageNum];
change = new int[imageNum];
time = new int[imageNum];
url = new URL[imageNum];
int count = 0;
while (t1.hasMoreTokens()) {
s = t1.nextToken();
StringTokenizer t2 = new StringTokenizer(s, "|");
imageFile[count] = t2.nextToken().trim();
change[count] = defaultChange;
if (t2.hasMoreTokens()) {
s = t2.nextToken().trim();
try {
if (s != null && !s.equals("-")) change[count] = Integer.parseInt(s);
} catch (NumberFormatException e) {
}
}
time[count] = defaultTime;
if (t2.hasMoreTokens()) {
s = t2.nextToken().trim();
try {
if (s != null && !s.equals("-")) time[count] = Integer.parseInt(s);
} catch (NumberFormatException e) {
}
}
if (t2.hasMoreTokens()) {
s = t2.nextToken().trim();
try {
if (s != null && !s.equals("-")) url[count] = new URL(s);
} catch (MalformedURLException e) {
try {
url[count] = new URL(getDocumentBase(), s);
} catch (MalformedURLException ex) {
}
}
}
count++;
}
}
private void initTitles() {
int count = 0;
title = new String[imageNum];
titlePos = new Point[imageNum];
if (fontBackColor != null) {
titleBackArea = new Rectangle[imageNum];
}
metrics = getFontMetrics(font);
String s = getParameter("titles");
if (s != null) {
StringTokenizer st1 = new StringTokenizer(s, ",");
while (st1.hasMoreTokens()) {
String t = st1.nextToken();
if (!t.equals("-")) {
StringTokenizer st2 = new StringTokenizer(t, "|");
if (st2.hasMoreTokens()) {
title[count] = st2.nextToken();
int x = PADX;
int y = PADY + metrics.getAscent();
int width = metrics.stringWidth(title[count]);
int height = metrics.getAscent();
if (st2.hasMoreTokens()) {
String u = st2.nextToken();
if ("n".equals(u)) {
x = (mySize.width - width) / 2;
y = PADY + height;
} else if ("ne".equals(u)) {
x = mySize.width - PADX - width;
y = PADY + height;
} else if ("c".equals(u)) {
x = (mySize.width - width) / 2;
y = (mySize.height + height) / 2;
} else if ("sw".equals(u)) {
x = PADX;
y = mySize.height - PADY;
} else if ("s".equals(u)) {
x = (mySize.width - width) / 2;
y = mySize.height - PADY;
} else if ("se".equals(u)) {
x = mySize.width - PADX - width;
y = mySize.height - PADY;
}
}
titlePos[count] = new Point(x, y);
if (fontBackColor != null) {
titleBackArea[count] = new Rectangle(x - 5, y - height - 5,
width + 10, height + 10);
}