日期:2014-05-20  浏览次数:20767 次

j2me中drawRGB()方法的用法
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class AlphaBlending extends MIDlet implements CommandListener {
    
    private Display display;
    private Form. form;
    private Command exitCommand;
    private Command startCommand;
    private Command backCommand;
    private AlphaCanvas alphaCanvas;
    
    private static final String IMAGE_NAME = "/image.png";
    
    
    public AlphaBlending() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 2);
        startCommand = new Command("Start", Command.SCREEN, 1);
        backCommand = new Command("Back", Command.BACK, 1);
        alphaCanvas = new AlphaCanvas(IMAGE_NAME);
        form. = new Form("Alpha Blending Demo");
        form.append(new StringItem(null, "Press 'Start' to run demo."));
        form.addCommand(exitCommand);
        form.addCommand(startCommand);
        form.setCommandListener(this);
    }
    
    
    public void startApp() {
        display.setCurrent(form);
    }
    
    
    public void pauseApp() {}
    
    
    public void destroyApp(boolean unconditional) {}
    
    
    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }else if (c == startCommand) {
            display.setCurrent(alphaCanvas);
            alphaCanvas.addCommand(backCommand);
            alphaCanvas.setCommandListener(this);
            
            Thread t = new Thread(alphaCanvas);
            t.start();
        }else if (c == backCommand) {
            
            display.setCurrent(form);
        }
    }
    
    
}


 

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;

public class AlphaCanvas extends Canvas implements Runnable {
    
    private Image backgroundImage;
    private int[] maskArray;
    private int imageWidth;
    private int imageHeight;
    private int x;
    private int y;
    private int maskHeight = 0;
    
    public AlphaCanvas(String imageName) {
        //create the background image
        try {
            backgroundImage = Image.createImage(imageName);
        }catch(IOException ioe) {
            ioe.printStackTrace() ;
        }
        imageWidth = backgroundImage.getWidth();
        imageHeight = backgroundImage.getHeight();
        
        //create a semi-transparent red mask to cover the background image
        maskArray = new int[imageWidth*imageHeight];
        for (int i = 0; i < imageWidth*imageHeight; i++) {
            maskArray[i] = 0x11FFF000;//alpha coefficient set to 0.3
        }
        
        x = (getWidth() - imageWidth)/2;
        y = (getHeight() - imageHeight)/2;
    }
    
    public void paint(Graphics g) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());//paint Canvas background white
        g.drawImage(backgroundImage, x, y, Graphics.TOP|Graphics.LEFT);
        
        //render the semi-transparent mask
        if (maskHeight != 0){
            g.drawRGB(maskArray, 0, imageWidth, x, y, imageWidth, maskHeight, true);
        }
    }
    
    public void run() {
        for(int i = 1; i <= imageHeight; i++) {
            maskHeight = i;
            repaint();
            try {
                Thread.sleep(50);
            }catch(InterruptedException ie) {}
        }
    }
    
}

?