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

📄 processbar.java

📁 j2me开发框架
💻 JAVA
字号:
package com.podome.ui;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.podome.log.Logger;
import com.podome.style.Style;
import com.podome.tool.Config;
import com.podome.tool.EffectHelper;

public class ProcessBar extends UIBase implements EventDispatch {
    /**
     * 显示精度
     */
	/*************进度条出现的位置**********/
	private boolean precision = false;
	private int     process   = 1; //当前进度
	
	private int     x,y;       //X Y坐标
	private int     offset = -1;
	
	ProcessEvent processEventListener;
	boolean isModel = false;
	
	private Menu    menu = null;
	private Command cmdOK = null;
	private Command cmdCancel = null;
	
	
	
	private static int[] colorArr = {
		 0xE2E2E2,0xC9C9C9,0xB0B0B0,0x989898,0x7F7F7F,
		 0x161616,0x3F3F3F,0x686868,0x909090,0xB9B9B9
	};
	private static Image activeImage;
	
	static{
		int w = colorArr.length + 5;
		int h = colorArr.length;
		Image lineImage = Image.createImage(w,h);
		Graphics imageG = lineImage.getGraphics();
		for(int i=0;i<colorArr.length;i++){
			imageG.setColor(colorArr[i]);
			imageG.drawLine(i, i, i+5, i);
		}
		int imgArr[] = new int[w*h];
		lineImage.getRGB(imgArr, 0, w, 0, 0, w, h);	
	    for(int i=0;i < imgArr.length; i++){		    	
	    	if(imgArr[i] == -1 ){		    		
		        imgArr[i] -= 0xFF000000;
	    	}
		}
	    activeImage = Image.createRGBImage(imgArr, w, h, true);
	}
	
	public ProcessBar(boolean isPrecision){
		this.setPrecision(isPrecision);
		Style s = getStyle();
		this.bonds[0][0] = 0;
	    this.bonds[0][1] = 0;
	    this.bonds[1][0] = Stage.getCW();
	    this.bonds[1][1] = s.dfont.getHeight() + 10;
	    if(!this.precision ){
	    	this.bonds[1][1] += 20;
	    }
	    x = 0;
	    y = Stage.getCH() - Menu.getHeight() - getHeight();
	}   
	
	public void setPrecision(boolean isPrecision){
		if(this.precision != isPrecision )
			this.precision = isPrecision;
	}
	
	public int getX(){
		return this.x;
	}
	
	public int getY(){		
		return this.y;
	}
	
	public int getHeight(){		
		return bonds[1][1] - bonds[0][1];
	}
	
	public int getWidth(){		
		return bonds[1][0] - bonds[0][0];
	}	
	public void setString(String s){
		super.setString(s);
		this.strWidth = getStyle().dfont.stringWidth(s);
		if(this.precision){
			this.strWidth += getStyle().wCharCH*3;
		}
	}
	/**
	 * 画进度条
	 * @param g
	 * @param s
	 */
	public void paint(Graphics g){
		Style s = this.getStyle();
		
		int x = getX();
		int y = getY();
		int w = getWidth();
		int h = getHeight();
		g.setClip(x, y, w, h);	
		g.setColor(s.bColorNormal); //设置背景颜色
		g.fillRoundRect(x, y, w-1, h+5, 10, 10); //画背景
		g.setColor(s.sColorNormal);
		g.drawRoundRect(x, y, w-1, h+5, 10, 10); //画边框
		g.setClip(x+1, y+1, w-1, h-1);
		g.setFont(s.dfont);		
		if( this.precision ){			
			g.setColor(s.bColorFocus);
			g.fillRect(x, y, w*this.process/100 , h); 
			g.setColor(s.fColorNormal);
			g.drawString( this.getString() + "  " + this.process + "%", x+(w-this.strWidth)/2, y+(h-s.dfont.getHeight())/2, Graphics.LEFT | Graphics.TOP);
		}
		else{
		    //画文字在上面
			 g.drawString(this.getString(), (w-strWidth)/2, y+h/4-3, Graphics.LEFT | Graphics.TOP);
			 
			 //画动态进度在下面
			 x +=50;
			 w -= 100;
			 y += h/2 + 5;
			 h = 12;			 
			 g.setColor(s.bColorFocus); //设置背景颜色
			 g.fillRoundRect(x+1, y, w-2, h-1, 8, 8); //画背景
			 g.setColor(s.sColorFocus);
			 g.drawRoundRect(x+1, y, w-2, h-1, 8, 8); //画边框
			 g.setClip(x+2, y+1, w-2, h);			 
			 paintActiveStatus(g , this.offset , y+1, Stage.getCW(), 10);
		}		
	}
	
	/**
	 * @param x 矩形开始X坐标
	 * @param y 矩形开始Y坐标
	 * @param w 矩形宽度
	 * @param h 矩形高度
	 */
	private void paintActiveStatus(Graphics g ,int x,int y,int w,int h){
		int startY = y + ( h- colorArr.length ) / 2;
		for(int startX = x;startX < w ; startX += 10){			
			g.drawImage(activeImage, startX, startY, Graphics.LEFT | Graphics.TOP);
	    }		
	} 
	
	/**
	 * 显示状态
	 */
	public void show(String cmdOK,String cmdCancel){
		EffectHelper.addToRepaint(this);
		this.setCommand(cmdOK, cmdCancel);
	}
	
	/**
	 * 
	 * @param message
	 */
    public void update(String message){
		this.setString(message);
	}
    
    /**
	 * 更新进度值
	 * @param process
	 */
	public void update(int p){
		if(this.process != p &&
		   p > 0 && p <= 100){
			this.process = p;
		}		
	}
	
	/**
	 * 更新显示信息和进度值
	 * @param message
	 * @param process
	 */
	public void update(String message,int process ){
		this.update(process);
		this.update(message);
	}
	
	/**
	 * 结束状态显示
	 */
	public void hide(){
		EffectHelper.removeRepaint(this);
		Stage.setBlockListener(null);
		Stage.setUserEventBlock(false);
		Stage.getInstance().notifyRepaintCurrent();
	}
	public void move(){
		if( this.precision ){
			this.process++;
			if(this.process >= 100)	{
				this.hide();	
			}
		}
		else{
			this.offset++;
			if(this.offset >= colorArr.length + 10 ){
			    this.offset = 0;			
			}
		}
		if(! this.precision || this.process < 100 ){
		    Stage.getInstance().notifyRepaint(this);
		}
	}
	
	
	public void setProcessEventListener(ProcessEvent lstr){
		this.processEventListener = lstr;
	}
	
	public void keyPressed(int keyCode) {
		
		
	}
	
	private void setCommand(String lableOK,String labelCancel){		
		if(cmdOK == null)
		    cmdOK = new Command(lableOK,false);
		else 
			cmdOK.setLabel(lableOK);
		
		if(cmdCancel == null)
		    cmdCancel = new Command(labelCancel,false);
		else
			cmdCancel.setLabel(labelCancel);
		
		if( menu == null)
		    menu = new Menu();
		if(Menu.isRightUsage()){			
		    menu.setLeft(cmdOK);
		    menu.setRight(cmdCancel);
		}
		else{			
			menu.setLeft(cmdCancel);
		    menu.setRight(cmdOK);
		}
		
		menu.setCommandDispatcher(this);
		
		if(Stage.currentPage().menu != null){
			menu.setStyle( Stage.currentPage().menu.getStyle());		
		}		
		Stage.setBlockListener(menu);
		Stage.getInstance().notifyRepaint(menu);
		Stage.getInstance().notifyRepaint(this);		
		Stage.setUserEventBlock(true);	    
	}
	

	public void notifyCommand(Command cmd) {		
		if( this.processEventListener != null ){
			this.hide();
			processEventListener.perform(cmd.getLabel());
		}
	}
	
	public String getTypeName() {
		return "ProcessBar";
	}
	
	public Style getStyle(){
		if(this.style != null){
			return this.style;
		}
		else
			return Style.getDefaultStyle();
	}

	public boolean isActive() {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean responseKeyCode(int keyCode) {
		// TODO Auto-generated method stub
		return true;
	}

}

⌨️ 快捷键说明

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