📄 imagechanger.java
字号:
/*
* @(#)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);
}
}
}
count++;
}
}
}
public void start() {
for (int i = 0; i < images.length; i++) {
try {
URL imgUrl = new URL(imageFile[i]);
images[i] = getImage(imgUrl);
} catch (MalformedURLException e) {
images[i] = getImage(getDocumentBase(), imageDir + "/" + imageFile[i]);
}
}
offImage = createImage(mySize.width, mySize.height);
offScreen = offImage.getGraphics();
onScreen = getGraphics();
mosaicImage = createImage(mySize.width, mySize.height);
tracker = new MediaTracker(this);
for (int i = 0; i < images.length; i++) {
tracker.addImage(images[i], i);
}
makeFadeImages();
if (kicker == null) {
kicker = new Thread(this);
kicker.start();
}
}
void makeFadeImages() {
int[][] pix = new int[FADEMAX][FADEWID*FADEWID];
for (int i = 0; i < FADEMAX; i++) {
for (int y = 0; y < FADEWID; y++) {
for (int x = 0; x < FADEWID; x++) {
pix[i][x+y*FADEWID] = (0xff*(i+1)/FADEMAX)<<24 | (fadeColor.getRGB() & 0x00ffffff);
}
}
MemoryImageSource mis = new MemoryImageSource(FADEWID, FADEWID, pix[i], 0, FADEWID);
fadeImage[i] = createImage(mis);
}
}
public void stop() {
if (kicker != null) {
kicker.stop();
}
kicker = null;
}
public void paint(Graphics g) {
g.drawImage(offImage, 0, 0, null);
}
public void update(Graphics g) {
paint(g);
}
void drawMessage(String message) {
offScreen.setColor(bgColor);
offScreen.fillRect(0, 0, mySize.width, mySize.height);
offScreen.setColor(Color.cyan);
offScreen.drawString(message, 10, 20);
}
void scrollImage(Image fore, Image back, int dx, int dy) {
int fx = (mySize.width - fore.getWidth(null)) / 2;
int fy = (mySize.height - fore.getHeight(null)) / 2;
int bx = (mySize.width - back.getWidth(null)) / 2;
int by = (mySize.height - back.getHeight(null)) / 2;
int width = fore.getWidth(null);
int height = fore.getHeight(null);
while (fx + width > 0 && fx < mySize.width
&& fy + height > 0 && fy < mySize.height) {
fx += dx;
fy += dy;
offScreen.setColor(bgColor);
offScreen.fillRect(0, 0, mySize.width, mySize.height);
offScreen.drawImage(back, bx, by, null);
offScreen.drawImage(fore, fx, fy, null);
onScreen.drawImage(offImage, 0, 0, null);
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
}
}
}
void slideImage(Image back, Image fore, int dx, int dy) {
int fx = 0;
int fy = 0;
int width = fore.getWidth(null);
int height = fore.getHeight(null);
int bx = (mySize.width - back.getWidth(null)) / 2;
int by = (mySize.height - back.getHeight(null)) / 2;
int endx = (mySize.width - width) / 2;
int endy = (mySize.height - height) / 2;
if (dx != 0) {
fx = (dx > 0)?-width:mySize.width;
fy = endy;
} else {
fy = (dy > 0)?-height:mySize.height;
fx = endx;
}
while (true) {
if (dx != 0) {
fx += dx;
if ((dx > 0 && fx > endx) || (dx < 0 && fx < endx)) break;
} else {
fy += dy;
if ((dy > 0 && fy > endy) || (dy < 0 && fy < endy)) break;
}
offScreen.setColor(bgColor);
offScreen.fillRect(0, 0, mySize.width, mySize.height);
offScreen.drawImage(back, bx, by, null);
offScreen.drawImage(fore, fx, fy, null);
onScreen.drawImage(offImage, 0, 0, null);
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
}
}
}
void tearImage(Image image, int dx, int dy) {
int w = 0;
int h = 0;
int x1 = (mySize.width - image.getWidth(null)) / 2;
int y1 = (mySize.height - image.getHeight(null)) / 2;
int x2 = x1+image.getWidth(null);
int y2 = y1+image.getHeight(null);
while (w < image.getWidth(null) && h < image.getHeight(null)) {
if (dx > 0) {
w += dx;
offScreen.clipRect(x1, 0, w, mySize.height);
} else if (dy > 0) {
h += dy;
offScreen.clipRect(0, y1, mySize.width, h);
} else if (dx < 0) {
x2 += dx;
w -= dx;
offScreen.clipRect(x2, 0, w, mySize.height);
} else if (dy < 0) {
y2 += dy;
h -= dy;
offScreen.clipRect(0, y2, mySize.width, h);
}
offScreen.drawImage(image, x1, y1, null);
onScreen.drawImage(offImage, 0, 0, null);
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
}
offScreen.dispose();
offScreen = offImage.getGraphics();
}
}
void tearOpenImage(Image image, int dx, int dy) {
int x = mySize.width / 2;
int y = mySize.height / 2;
int w = 0;
int h = 0;
int nx = (mySize.width - image.getWidth(null)) / 2;
int ny = (mySize.height - image.getHeight(null)) / 2;
while (x > nx && y > ny) {
if (dx > 0) {
x -= dx;
w += dx * 2;
offScreen.clipRect(x, 0, w, mySize.height);
} else if (dy > 0) {
y -= dy;
h += dy * 2;
offScreen.clipRect(0, y, mySize.width, h);
}
offScreen.drawImage(image, nx, ny, null);
onScreen.drawImage(offImage, 0, 0, null);
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
}
offScreen.dispose();
offScreen = offImage.getGraphics();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -