//改变云层透明度---------------------------------
iw=imBack.getWidth(this); //获取宽、高
ih=91;
grabber(imBack,iw,ih); //获取pixels[]
changeBack1Alpha();
ImageProducer ip=
new MemoryImageSource(iw,ih,pixels,0,iw);
imBack1a=createImage(ip);
//改变海水透明度---------------------------------
iw=imBack.getWidth(this);
ih=imBack.getHeight(this);
grabber(imBack,iw,ih); //获取pixels[]
changeBack2Alpha();
ip=
new MemoryImageSource(iw,ih,pixels,0,iw);
imBack2a=createImage(ip);
//制作一棵树---------------------------------------
grabber(imTree,iw,ih); //获取pixels[]
changeBack3Alpha();
ip=
new MemoryImageSource(iw,ih,pixels,0,iw);
imTreea=createImage(ip);
//改变fish图像背景的Alpha值------------------
iw=img.getWidth(this);
ih=img.getHeight(this);
grabber(img,iw,ih);
changeAlpha();
ip=
new MemoryImageSource(iw,ih,pixels,0,iw);
imgA=createImage(ip);
}
public synchronized void
actionPerformed(ActionEvent e) {
if(e.getSource() == bStop) { tFlag = false;}
if(e.getSource() == bStart) {
tFlag = true;
notify();
}
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void stop(){tFlag = false;}
public void run() {
sound=getAudioClip(getCodeBase(), "spacemusic.au");
sound.loop();
while(tFlag) {
move();
try {
synchronized(this){
while(!tFlag) wait();
}
}
catch(InterruptedException e) {}
}
}
//装载图像
public void loadImage(){
//利用MediaTracker跟踪图像的加载
MediaTracker tracker =
new MediaTracker(this);
imBack =getImage(getCodeBase(),
"image/back0.jpg");
imTree=getImage(getCodeBase(),
"image/tree.jpg");
img=getImage(getCodeBase(),"image/fish.gif");
tracker.addImage(imBack,0);
tracker.addImage(imTree,1);
tracker.addImage(img,2);
try{
tracker.waitForAll(); //等待图像完全加载
}
catch(InterruptedException e){ }
}
public void grabber(Image img,int w, int h){
pixels=new int[w*h];
try{
PixelGrabber pg=
new PixelGrabber(img,0,0,w,h,pixels,0,w);
pg.grabPixels();
}
catch (InterruptedException e1) { }
}
//第1种方法取ARGB,改变云层alpha值
public void changeBack1Alpha(){
ColorModel cm=ColorModel.getRGBdefault();
for(int i=0;i<ih;i++){
for(int j=0;j<iw;j++){
int red,green,blue;
red =cm.getRed(pixels[i*iw+j]);
green=cm.getGreen(pixels[i*iw+j]);
blue =cm.getBlue(pixels[i*iw+j]);
if(i<ih){
int a=220-2*i; //透明度变化
pixels[i*iw+j]=a<<24|red<<16|
green<<8|blue;//颜色不变 }
}
}
}
//改变海水的Alpha值
public void changeBack2Alpha(){
ColorModel cm=ColorModel.getRGBdefault();
for(int i=220;i<256;i++){
for(int j=0;j<iw;j++){
int red,green,blue;
red =cm.getRed(pixels[i*iw+j]);
green=cm.getGreen(pixels[i*iw+j]);
blue =cm.getBlue(pixels[i*iw+j]);
int a=3*i-540; //透明度变化(120-225)
pixels[(i-220)*iw+j]=a<<24|red<<16|
green<<8|blue;//颜色不变
}
}
}
//制作一棵树
public void changeBack3Alpha(){
ColorModel cm=ColorModel.getRGBdefault();
for(int i=0;i<ih;i++){
for(int j=0;j<iw;j++){
int red,green,blue;
red =cm.getRed(pixels[i*iw+j]);
green=cm.getGreen(pixels[i*iw+j]);
blue =cm.getBlue(pixels[i*iw+j]);
if (red>=40&&red<=90&&
green>=110&&green<=130&&
blue>=100&&blue<=180) {
red = 255; green = 255;
blue = 255; //设置为白色
pixels[i*iw+j]=0<<24|red<<16|
green<<8|blue;//全透明
}
else if(red==255&&green==255&&
blue==255){
pixels[i*iw+j]=0<<24|red<<16|
green<<8|blue;//全透明
}
}
}
}
//第2种方法取ARGB,改变Alpha值
public void changeAlpha(){
for(int i=0;i<iw*ih;i++){
int p=pixels[i];
int red,green,blue;
red =0xff&(p>>16);
green=0xff&(p>>8);
blue =0xff&(p);
if(red>230&&green>230&&blue>230)
pixels[i]=0<<24|red<<16|green<<8|blue; }
}
int x=256,y=150; //前景图初始坐标
public void move(){
Graphics g = getGraphics();
buff =new BufferedImage(2*iw,2*ih,
BufferedImage.TYPE_INT_ARGB);
tempGr=buff.createGraphics();
try{
Thread.sleep(50);
x-=2;
offGr.drawImage(imBack,0,0,this);
if(x>130){
offGr.drawImage(imgA,x,y,this);
y-=2;
}
else{
tempGr.drawImage(imgA,0,0,this);
filtIm=new BufferedImage(2*iw,2*ih,
BufferedImage.TYPE_INT_ARGB);
AffineTransform transform=
AffineTransform.getRotateInstance(
Math.toRadians(-60),34,34);
AffineTransformOp op=
new AffineTransformOp(transform,
AffineTransformOp.
TYPE_BILINEAR);
op.filter(buff,filtIm);
if(x==130) x=x-6;
offGr.drawImage(filtIm,x,y-80,null);
y+=2;
}
offGr.drawImage(imBack1a,0,0,this);
offGr.drawImage(imBack2a,0,220,this);
offGr.drawImage(imTreea,0,0,this);
g.drawImage(offIm,0,0,this);//一次性画图 if(x<-60){
x=256;
Thread.sleep(200);
}
if(y>220){ x=256; y=150; }
}
catch(InterruptedException e1){ }
}
public void update(Graphics g){
paint(g);
}
}
运行程序将产生鱼儿跳跃的动画。可以修改sleep(ms)中时间,调节鱼儿跳跃的速度,读者不妨一试。
参考文献
[1]Cay S.Hostmann, Gary Cornell(朱志等译). Java2核心技术,卷II:高级特性. 北京:机械工业出版社,2001.
[2]Herbert Schildt(张玉清等译). Java2参考大全(第四版). 北京:清华大学出版社, 2002.
[3]孙燮华. Java程序设计教程. 北京:清华大学出版社, 2007.
|