📄 fade.java
字号:
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.StringTokenizer;
/*
*该程序如果将APPLET的长宽调得过高,则会占用大量数组和资源
*
*/
public class fade extends Applet
implements Runnable
{
Image offImage;//次图象
Image image[];//存放图象的数组
Graphics offGraphics;//次画面
Thread animationThread;
MediaTracker mediaTrack;//媒体追踪
int maxPixels;
int w;
int h;
int numImages;//图象数
int imagePixel[][];
//二维数组存放每幅图象的象素
int currentImage[];//以当前applet长宽为最大象素的数组,保存当前图象的完全处理后的象素
int frameDelay;
int bkColorRBG;//背景色
int red[][];
int green[][];
int blue[][];
int imageF;//当前一张图片
int imageT;//下一张图片
int alpha;
double imageFinc;//当前图片系数,用来与每个象素的RGB相乘,从而实现图片的颜色的深浅
double imageTinc;//下一图片系数
double increment;//上面两个系数在每次线程苏醒后的增量
String link;//连接页
String frame;
String ca = "WWW.YNU.EDU.CN-LIWEI";
boolean registered;//是否注册
public void init()
{
w = size().width;//当前applet的宽
h = size().height;//当前applet的高
maxPixels = w * h;//当前的象素
mediaTrack = new MediaTracker(this);
if(getDocumentBase().toString().startsWith("file"))//本地使用则注册标志为真
registered = true;
if(!ca.equals(getParameter("REGISTER")))
registered =false;
StringTokenizer stringtokenizer = new StringTokenizer(getParameter("GENERAL"), "|", false);
//参数为"5|3|000000|33|40|0|1|"
numImages = Integer.parseInt(stringtokenizer.nextToken());//第一个为图片数
if(!stringtokenizer.nextToken().equalsIgnoreCase("177"))//第二个为7时表示已经注册
registered = false;
bkColorRBG = (new Color(Integer.parseInt(stringtokenizer.nextToken(), 16))).getRGB();
//integer.parseint后的参数16表示以前面参数作为16进制返回此参数的10进制000000
frameDelay = Integer.parseInt(stringtokenizer.nextToken());//33帧延迟
stringtokenizer.nextToken();//40
stringtokenizer.nextToken();//0
increment = (double)Integer.parseInt(stringtokenizer.nextToken()) * 0.01D;
//1,增量
stringtokenizer = new StringTokenizer(getParameter("LINK"), "|", false);
//query.html| | |
link = stringtokenizer.nextToken();//连接页
frame = stringtokenizer.nextToken();
image = new Image[numImages];//构造图片
currentImage = new int[maxPixels];//构造一个图象数组,大小为最大象素
imagePixel = new int[numImages][maxPixels];//二维数组,每张图象一个,该数组大小为最大象素
red = new int[numImages][maxPixels];
green = new int[numImages][maxPixels];
blue = new int[numImages][maxPixels];
stringtokenizer = new StringTokenizer(getParameter("IMAGES"), "|", false);
//装载图象
for(int i = 0; i < numImages; i++)
{
image[i] = getImage(getCodeBase(), stringtokenizer.nextToken());
mediaTrack.addImage(image[i], 0);
}
}
public void paint(Graphics g)
{
if(offImage != null)
{
offGraphics.drawImage(morfImages(increment), 0, 0, null);
//在次画面上画
g.drawImage(offImage, 0, 0, this);
//画面上画次图象
return;
}
if(!registered)
{
g.drawString("You have the unregistered", 0, 10);
g.drawString("version of this program", 0, 20);
g.drawString("You need the registered version", 0, 30);
g.drawString("for this Applet to work on the internet", 0, 40);
g.drawString("Click HERE for registration instructions", 0, 50);
return;
}
StringTokenizer stringtokenizer = new StringTokenizer(getParameter("LOAD MESSAGE"), "|", false);
//"000000|FFFFFF|Loading..."
g.setColor(new Color(Integer.parseInt(stringtokenizer.nextToken(), 16)));
//背景色
g.fillRect(0, 0, w, h);
g.setColor(new Color(Integer.parseInt(stringtokenizer.nextToken(), 16)));
//前景色
g.drawString(stringtokenizer.nextToken(), 10, 20);
//信息内容
try
{
mediaTrack.waitForAll();
}
catch(InterruptedException _ex)
{
stop();
}
offImage = createImage(size().width, size().height);
//建立次图象
offGraphics = offImage.getGraphics();
//建立次画面
/*
*该循环主要目的是将所有图象的固定资料保存
*/
for(int j = 0; j < numImages; j++)
{
int ai[] = new int[image[j].getHeight(this) * image[j].getWidth(this)];
//以当前图象的最大象素建立数组
PixelGrabber pixelgrabber = new PixelGrabber(image[j], 0, 0, image[j].getWidth(this), image[j].getHeight(this), ai, 0, image[j].getWidth(this));
//Create a PixelGrabber object to grab the (x, y, w, h) rectangular section of
//pixels from the specified image into the given array
try
{
pixelgrabber.grabPixels();
//Request the Image or ImageProducer to start delivering pixels and wait
// for all of the pixels in the rectangle of interest to be delivered or
//until the specified timeout has elapsed.
}
//将图象象素保存在一个ai数组内
catch(InterruptedException _ex) { }
for(int k = 0; k < h; k++)//h是applet的高
{
for(int l = 0; l < w; l++)//w是applet的宽
{
int i = k * w + l;//当前记数的个数,实际上是将二维数组转化为一维数组的计算公式
if(k < image[j].getHeight(this) && l < image[j].getWidth(this))
//如果当前的记数高小于当前图象的高,记数宽小于当前图象的宽
imagePixel[j][i] = ai[k * image[j].getWidth(this) + l];
//这里不用k * w + l的原因是ai中装的是对应图片的象素,而非applet
//image数组装ai的对应元素
else
imagePixel[j][i] = bkColorRBG;
//图象以外的部分为bkcolorrbg
}
}
//保存每个象素的GRB
for(int i1 = 0; i1 < maxPixels; i1++)
{
red[j][i1] = (imagePixel[j][i1] & 0xff0000) >> 16;
green[j][i1] = (imagePixel[j][i1] & 0xff00) >> 8;
blue[j][i1] = imagePixel[j][i1] & 0xff;
}
}//for循环,循环数为图片数
}
/*
*对图象进行处理
*/
final Image morfImages(double d)
{
checkMax();//检查是否需要更换图片
/*
*对每个象素进行处理,当前图象渐减,次图象渐增
*/
for(int i = 0; i < maxPixels; i++)
{
int j = (int)((double)red[imageF][i] * imageFinc + (double)red[imageT][i] * imageTinc);
int k = (int)((double)green[imageF][i] * imageFinc + (double)green[imageT][i] * imageTinc);
int l = (int)((double)blue[imageF][i] * imageFinc + (double)blue[imageT][i] * imageTinc);
currentImage[i] = toHex(alpha,j,k,l);
}
imageTinc = imageTinc + d;
imageFinc = imageFinc - d;
return createImage(new MemoryImageSource(w, h, currentImage, 0, w));
//ImageProducer
}
public boolean mouseUp(Event event, int i, int j)
{
if(registered)
{
if(link != null && !link.equalsIgnoreCase(" "))
try
{
URL url = new URL(getDocumentBase(), link);
if(!frame.equalsIgnoreCase(" "))
getAppletContext().showDocument(url, frame);
else
getAppletContext().showDocument(url);
}
catch(MalformedURLException _ex) { }
} else
{
try
{
URL url1 = new URL(getDocumentBase(), "unregister.html");
getAppletContext().showDocument(url1);
}
catch(MalformedURLException _ex) { }
}
return true;
}
final void checkMax()
{
if(imageTinc >= 1.0D)
{
imageTinc = 0.0D;
imageFinc = 1.0D;
swichImages();
}
}
final void swichImages()
{
imageF++;
imageT++;
if(imageT == numImages)
imageT = 0;
if(imageF == numImages)
imageF = 0;
}
final int toHex(int i, int j, int k, int l)
{
return i | j << 16 | k << 8 | l;
}
public void stop()
{
animationThread = null;
}
public void start()
{
if(animationThread == null)
{
animationThread = new Thread(this);
Thread.currentThread().setPriority(10);
animationThread.start();
}
}
public void run()
{
while(animationThread != null)
{
try
{
Thread.sleep(frameDelay);
}
catch(InterruptedException _ex) { }
repaint();
}
}
public void update(Graphics g)
{
paint(g);
}
public void paintFrame(Graphics g)
{
}
public fade()
{
//frameDelay = 33;
imageF = 0;
imageT = 1;
alpha = 0xff000000;
imageTinc = 0.0D;
imageFinc = 1.0D;
registered = true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -