Effects.java
上传用户:haobig99
上传日期:2022-06-15
资源大小:369k
文件大小:5k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
  3.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4.  *
  5.  * This code is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License version 2 only, as
  7.  * published by the Free Software Foundation.  Sun designates this
  8.  * particular file as subject to the "Classpath" exception as provided
  9.  * by Sun in the LICENSE file that accompanied this code.
  10.  *
  11.  * This code is distributed in the hope that it will be useful, but WITHOUT
  12.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14.  * version 2 for more details (a copy is included in the LICENSE file that
  15.  * accompanied this code).
  16.  *
  17.  * You should have received a copy of the GNU General Public License version
  18.  * 2 along with this work; if not, write to the Free Software Foundation,
  19.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22.  * CA 95054 USA or visit www.sun.com if you need additional information or
  23.  * have any questions.
  24.  */
  25. package com.sun.lwuit.util;
  26. import com.sun.lwuit.Image;
  27. import com.sun.lwuit.RGBImage;
  28. /**
  29.  * Static utility class useful for simple visual effects that don't quite fit 
  30.  * anywhere else in the core API.
  31.  *
  32.  * @author Shai Almog
  33.  */
  34. public class Effects {
  35.     private Effects() {}
  36.     /**
  37.      * Takes the given image and appends an effect of reflection bellow it that
  38.      * is similar to the way elements appear in water beneath them. This method
  39.      * shouldn't be used when numAlpha is very low.
  40.      * 
  41.      * @param source image to add the reflection effect to
  42.      * @return new image with a reflection effect for the source image
  43.      */
  44.     public static Image reflectionImage(Image source) {
  45.         return reflectionImage(source, 0.5f, 120);
  46.     }
  47.     
  48.     /**
  49.      * Takes the given image and appends an effect of reflection bellow it that
  50.      * is similar to the way elements appear in water beneath them. This method
  51.      * shouldn't be used when numAlpha is very low.
  52.      * 
  53.      * @param source image to add the reflection effect to
  54.      * @param mirrorRatio generally less than 1, a mirror ration of 0.5f will create a mirror image half the
  55.      * height of the image, 0.75f will create a 3 quarter height mirror etc.
  56.      * @param alphaRatio starting point for the alpha value in the mirror, this should be a number between 0 - 255
  57.      * (recommended larger than 0) indicating the opacity of the closest pixel. For a mirror thats completely
  58.      * opaque use 255. A recommended value would be between 128 to 90.
  59.      * @return new image with a reflection effect for the source image
  60.      */
  61.     public static Image reflectionImage(Image source, float mirrorRatio, int alphaRatio) {
  62.         return reflectionImage(source, mirrorRatio, alphaRatio, 0);
  63.     }
  64.     /**
  65.      * Takes the given image and appends an effect of reflection bellow it that
  66.      * is similar to the way elements appear in water beneath them. This method
  67.      * shouldn't be used when numAlpha is very low.
  68.      *
  69.      * @param source image to add the reflection effect to
  70.      * @param mirrorRatio generally less than 1, a mirror ration of 0.5f will create a mirror image half the
  71.      * height of the image, 0.75f will create a 3 quarter height mirror etc.
  72.      * @param alphaRatio starting point for the alpha value in the mirror, this should be a number between 0 - 255
  73.      * (recommended larger than 0) indicating the opacity of the closest pixel. For a mirror thats completely
  74.      * opaque use 255. A recommended value would be between 128 to 90.
  75.      * @param spacing the distance in pixels between the image and its reflection
  76.      * @return new image with a reflection effect for the source image
  77.      */
  78.     public static Image reflectionImage(Image source, float mirrorRatio, int alphaRatio, int spacing) {
  79.         int w = source.getWidth();
  80.         int h = source.getHeight();
  81.         int mirrorHeight = ((int)(h  * mirrorRatio)) * w;
  82.         // create an array big enough to hold the mirror data
  83.         RGBImage rgbImg = new RGBImage(new int[w * (h + spacing) + mirrorHeight], w, h + ((int)(h  * mirrorRatio) + spacing));
  84.         source.toRGB(rgbImg, 0, 0, 0, 0, w, h);
  85.         int[] imageData = rgbImg.getRGB();
  86.         for(int iter = 0 ; iter < mirrorHeight ; iter++) {
  87.             int sourcePos = w * h - iter - 1;
  88.             int off = iter % w;
  89.             off = w - off + iter - off;
  90.             int mirrorPos = imageData.length - (mirrorHeight - off) + (spacing * w);
  91.             int color = imageData[sourcePos];
  92.             // if the color is not transparent
  93.             if((color & 0xff000000) != 0 && mirrorPos < imageData.length) {
  94.                 int alpha = (int)(alphaRatio * ((float)mirrorHeight - iter) / ((float)mirrorHeight));
  95.                 imageData[mirrorPos] = (imageData[sourcePos] & 0xffffff) | ((alpha << 24) & 0xff000000);
  96.             }
  97.         }
  98.         return rgbImg;
  99.     }
  100. }