用APPLET实现图片淡入淡出,应该怎么做?

chinapcer 2002-12-23 12:26:57
应该有哪个类的哪个方法呢?
...全文
43 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
希偌 2002-12-24
  • 打赏
  • 举报
回复
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;

public class applet2 extends Applet implements Runnable {
Image img[]=new Image[30];
Image imgx=null;
int imgIndex=0;
int imgAdd=1;
Thread thread=null;
MediaTracker mt;
ImageFilter imgf=null;
FilteredImageSource fis=null;
Image im=null;
Graphics g2=null;
public void init() {
imgx=this.getImage(this.getCodeBase(),"d.jpg");//这里装载图片
mt=new MediaTracker(this);
thread=new Thread(this);
loadImage(mt);
try {
mt.waitForAll();
} catch(Exception ex) {System.err.println(ex.toString());}
im=this.createImage(imgx.getWidth(this),imgx.getHeight(this));
g2=im.getGraphics();
}

public void loadImage(MediaTracker mt) {
for(int i=0;i<30;i++) {
imgf=new myImage(imgx.getWidth(this),imgx.getHeight(this),i*8);
fis=new FilteredImageSource(imgx.getSource(),imgf);
img[i]=this.createImage(fis);
mt.addImage(img[i],0);
}
}

public void run() {
try {
while(true) {
this.repaint();
Thread.sleep(100);
}
}catch(Exception e) {
System.out.println(e.toString());
}
}

public void paint(Graphics g) {
g2.drawImage(img[imgIndex],0,0,this);
g.drawImage(im,0,0,this);
}

public void update(Graphics g) {
if(imgIndex>=29)
imgAdd=-1;
if(imgIndex<=0)
imgAdd=1;
imgIndex+=imgAdd;
g2.clearRect(0,0,imgx.getWidth(this),imgx.getHeight(this));
g2.drawImage(img[imgIndex],0,0,this);
g.drawImage(im,0,0,this);
}

public void start() {
thread.start();
}

public void stop() {
if(thread!=null) {
thread.stop();
thread=null;
}
}
}

class myImage extends RGBImageFilter {
int width=0;
int height=0;
int alpha=0;
public myImage(int width,int height,int alpha) {
this.canFilterIndexColorModel=true;
this.width=width;
this.height=height;
this.alpha=alpha;
}

public int filterRGB(int x,int y,int rgb) {
DirectColorModel dcm=(DirectColorModel)ColorModel.getRGBdefault();
int red=dcm.getRed(rgb);
int green=dcm.getGreen(rgb);
int blue=dcm.getBlue(rgb);
return alpha<<24|red<<16|green<<8|blue;
}
}
chinapcer 2002-12-23
  • 打赏
  • 举报
回复
UP,分不够可以加
chinapcer 2002-12-23
  • 打赏
  • 举报
回复
能给一个完整代码吗?
马上给分
showcode 2002-12-23
  • 打赏
  • 举报
回复
我理解的是透明度的改变
你可以这样
取得每个像素的RGB,pic.getRGB(x,y)
new RGB=x001000000+pic.getRGB(x,y)
其中01~ff表示透明度)\0%~100%
oic.setRGB(x,y,newRGB)

pic.drawImage(pic,x,y)
chinapcer 2002-12-23
  • 打赏
  • 举报
回复
UP.
在线等待

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧