⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fading.java

📁 J2ME手机游戏开发技术详解,适合初学者阅读使用
💻 JAVA
字号:
/** * * By:      Oscar Wivall * Date:    2004-05-06 * * Description: Create cool effects by fading in/out the images in MIDP 2.0. *              This midlet shows how to change the alpha value of an Image. * *              This MIDlet is tested on Z1010 and K700. * */import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.io.*;import java.io.*;/**   Main class. Create TheCanvas, Add a exit button and display the canvas.*/public class Fading extends javax.microedition.midlet.MIDlet                         implements CommandListener{    private Command mExitCommand;    public void startApp() {        mExitCommand = new Command("Exit", Command.SCREEN, 0);        TheCanvas tc = new TheCanvas();        tc.addCommand(mExitCommand);        tc.setCommandListener(this);        Display d = Display.getDisplay(this);        d.setCurrent(tc);    }    public void pauseApp() {            }    public void destroyApp(boolean unconditional) {    }        public void commandAction(Command c, Displayable d){        notifyDestroyed();    }}/** TheCanvas class: loads both of the images used in this MIDlet,*                  uses graphics do do the backbuffer drawing,*                  uses ImageEffect do do the Image blending.*/class TheCanvas extends Canvas                implements Runnable{    private Image orangeImage;    private Image greenImage;    private Image mBufferImage;        private Graphics mGraphics;        private boolean mTrucking = true;    private boolean mUpdate = true;        private int mAlpha = 0;    private int mValue = 5;    private int[]rawInt; // this is the array who will hold the image ARGB values.                          // to do the blending we will change the Alpha value.        private Runtime rt;    public TheCanvas(){                rt = Runtime.getRuntime(); // to get the amount of free memory.                // Load images. Use mGraphics to draw on the backbuffer.        mBufferImage = Image.createImage(getWidth(), getHeight());        mGraphics = mBufferImage.getGraphics();        try{            orangeImage = Image.createImage("/orange.png");            greenImage = Image.createImage("/green.png");        }catch(Exception e){}        // Allocate mamory for the image array.        // Use the getRGB method to get all ARGB values from the image to the rawInt //array.        rawInt = new int[orangeImage.getWidth() * orangeImage.getHeight()];        orangeImage.getRGB(rawInt, 0, orangeImage.getWidth(), 0, 0, orangeImage.getWidth(), orangeImage.getHeight());                Thread t = new Thread(this);        t.start();    }    // draw the image to the canvas.    public void paint(Graphics g){        g.drawImage(mBufferImage, this.getWidth()/2, this.getHeight()/2, g.VCENTER|g.HCENTER);        g.drawString("freeMemory=" + String.valueOf(rt.freeMemory()), 0, 100, 0);        mUpdate = true;    }    public void run() {        while(mTrucking){            if(mUpdate){                // change the alpha value each loop. 255=opaque, 0=transparent.                mAlpha+= mValue;                if(mAlpha>=255)                    mValue = mValue *-1;                else if(mAlpha<=0)                    mValue = mValue *-1;                // Use the blend method in ImageEffect the change the Alpha value.                // create a new Image from the new rawInt array.                ImageEffect.blend(rawInt, mAlpha);                Image fadingImage = Image.createRGBImage(rawInt, orangeImage.getWidth(), orangeImage.getHeight(), true);                mGraphics.setColor(0xFFFFFF);                mGraphics.fillRect(0, 0, getWidth(), getHeight());                mGraphics.drawImage(greenImage,this.getWidth()/2, this.getHeight()/2, Graphics.VCENTER|Graphics.HCENTER);                mGraphics.drawImage(fadingImage,this.getWidth()/2, this.getHeight()/2, Graphics.VCENTER|Graphics.HCENTER);                System.gc();                                mUpdate = false;            }            repaint();        }    }    }/* * This class changes the alpha value of an image array. * All the pixels in the image contains Alpha, Red, Green, Blue (ARGB) colors * where each of the color is a value from 0 to 255. * If Alpha is 255 the pixel will be opaque, if its 0 the pixel is transparent. * 0xFFFF0000 = 11111111 11111111 00000000 00000000 - this is a red opaque pixel. * * To get the RGB values from the array we can use the AND '&' operator. * (11111101 & 01111110) = 01111100, only the 1:s where & = 1 will get through. * * to change 11111111 to 11111111 00000000 00000000 00000000 we can use the * shift left operator '<<', (00000001 << 7) = 10000000 in dec (1<<7) = 128 * * To change the alpha value we loop through all the pixels in the image. * * With the blend method its also possible to mask and dontmask specific colors.*/class ImageEffect{        // raw is the image array.    public static void blend(int[] raw, int alphaValue, int maskColor, int dontmaskColor){        int len = raw.length;                // Start loop through all the pixels in the image.        for(int i=0; i<len; i++){            int a = 0;            int color = (raw[i] & 0x00FFFFFF); // get the color of the pixel.            if(maskColor==color){                a = 0;            }else if(dontmaskColor==color){                a = 255;            }else if(alphaValue>0){                a = alphaValue;     // set the alpha value we vant to use 0-255.            }                        a = (a<<24);    // left shift the alpha value 24 bits.            // if color = 00000000 11111111 11111111 00000000            // and alpha= 01111111 00000000 00000000 00000000            // then c+a = 01111111 11111111 11111111 00000000            // and the pixel will be blended.            color += a;            raw[i] = color;        }    }    public static void blend(int[] raw, int alphaValue){        blend(raw, alphaValue, 0xFFFFFFFF, 0xFFFFFFFF);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -