📄 colorchangemotion.java
字号:
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
class myRGBFilter extends RGBImageFilter
{
private int transValue;
private int bgRed;
private int bgGreen;
private int bgBlue;
public myRGBFilter(int tV,int backgroundRGB)
{
canFilterIndexColorModel = true;
transValue = tV;
bgRed = (backgroundRGB >> 16) & 255;
bgGreen = (backgroundRGB >> 8) & 255;
bgBlue = (backgroundRGB & 255);
}
public void setTransValue(int newValue)
{
transValue = newValue;
}
public int getTransValue()
{
return transValue;
}
public int filterRGB(int x,int y,int rgb)
{
int red = (bgRed * (255 - transValue) + ((rgb >> 16) & 255) * transValue) / 255;
int green = (bgGreen * (255 - transValue) + ((rgb >> 8) & 255) * transValue) / 255;
int blue = (bgBlue * (255 - transValue) + (rgb & 255) * transValue) / 255;
return (rgb & 0xff000000) + (red << 16) + (green << 8) + blue;
}
}
public class colorChangeMotion extends Applet implements Runnable
{
protected Image origImage;
protected Image cycledImage;
protected myRGBFilter colorFilter;
Thread animatorThread;
static final int delay = 10;
int trans,direction;
public void init()
{
trans = 255;
direction = -1;
origImage = getImage(getDocumentBase(),getParameter("image"));
MediaTracker mt = new MediaTracker(this);
mt.addImage(origImage,0);
try
{
mt.waitForID(0);
}
catch(Exception e)
{
}
colorFilter = new myRGBFilter(trans,Color.GRAY.getRGB());
cycledImage = createImage(new FilteredImageSource(origImage.getSource(),colorFilter));
}
public void start()
{
if(animatorThread == null)
animatorThread = new Thread(this);
animatorThread.start();
}
public void stop()
{
animatorThread = null;
}
public void run()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
long startTime = System.currentTimeMillis();
while(Thread.currentThread() == animatorThread)
{
trans += direction;
if(trans >= 255 || trans <= 0)
direction = -direction;
colorFilter.setTransValue(trans);
cycledImage.flush();
MediaTracker myTracker = new MediaTracker(this);
myTracker.addImage(cycledImage,0);
try
{
myTracker.waitForID(0);
}
catch(Exception e)
{
}
repaint();
try
{
startTime += delay;
Thread.sleep(Math.max(0,startTime - System.currentTimeMillis()));
}
catch(InterruptedException ie)
{
break;
}
}
}
public void paint(Graphics g)
{
g.drawImage(cycledImage,0,0,this);
}
public void update(Graphics g)
{
paint(g);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -