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

J2ME颜色选择器
有时在项目中可能会碰到自定义软件皮肤颜色或字体颜色的功能,那么颜色选择器正好派上了用场了。

此类为直接继承自Form类,所以使用时可以直接new 一个ColorSelectorUI对像然后显示即可,

如果要保存设置的颜色值,可以直接在此类中加Command即可。

package com.flyeverzhang.test;   
  
import javax.microedition.lcdui.CustomItem;   
import javax.microedition.lcdui.Form;   
import javax.microedition.lcdui.Gauge;   
import javax.microedition.lcdui.Graphics;   
import javax.microedition.lcdui.Item;   
import javax.microedition.lcdui.ItemStateListener;   
  
  
/**  
* J2ME 颜色选择器  
* @author flyeverzhang  
* 2009-7-31  
*/  
public class ColorSelectorUI extends Form implements ItemStateListener{   
       
    private Gauge red = new Gauge("R",true,255,0);//红色分量   
    private Gauge green = new Gauge("G",true,255,0);//蓝色分量   
    private Gauge blue = new Gauge("B",true,255,255);//绿色分量   
    private ShowColor sc = null ;   
    public ColorSelectorUI( ) {   
        super("颜色选择器");   
           
        sc = new ShowColor("颜色预览");   
        this.append(this.red);   
        this.append(this.green);   
        this.append(this.blue);   
        this.append(sc);   
           
        //注册监听器   
        this.setItemStateListener(this);   
    }   
    /**  
    * 颜色预览窗口  
    * @author flyeverzhang  
    * 2009-7-31  
    */  
    class ShowColor extends CustomItem {   
           
        protected ShowColor(String label) {   
            super(label);   
        }   
  
        protected int getMinContentHeight() {   
            return 30;   
        }   
  
        protected int getMinContentWidth() {   
            return 150;   
        }   
  
        protected int getPrefContentHeight(int arg0) {   
            return this.getMinContentHeight();   
        }   
  
        protected int getPrefContentWidth(int arg0) {   
            return this.getMinContentWidth();   
        }   
  
        protected void paint(Graphics g, int w, int h) {   
            g.setColor(red.getValue(),green.getValue(),blue.getValue());   
            g.fillRect(0, 0, w - 1, h - 1);            
        }   
           
        public void reDraw(){   
            this.repaint();   
        }   
    }   
  
    public void itemStateChanged(Item item) {   
        sc.reDraw();   
    }   
       
       
}