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

📄 imageitem.java

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

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

import com.podome.log.Logger;
import com.podome.style.Style;

public class ImageItem extends UIBase {
	
	private Image image;
	
	public ImageItem(Image img){
		this.image = img;
		this.setLayout(Style.LAYOUT_RIGHT);
	}
	
    public ImageItem(Image image,String label){
		this(image);
		super.setString(label);
	}

	protected void setAutoArea(int x, int y,int maxWidth){
		
		if( this.owner == null ) return;
		Style selfStyle = this.getStyle();
		this.setX(x);
		this.setY(y);
		int imageWidth=0,imageHeight=0;		
			
		if( this.image != null){
			imageWidth  = image.getWidth();
			imageHeight = image.getHeight();
		}		
		//计算字符长度
		if(this.string != null){
		    this.strWidth   = selfStyle.dfont.stringWidth(string);
		    this.lineHeight = selfStyle.dfont.getHeight();
		}
		int factWidth =0,factHeight = 0;
		if(this.layout == Style.LAYOUT_RIGHT ||
		   this.layout == Style.LAYOUT_LEFT ){
			factWidth  = imageWidth + this.strWidth;
			factHeight  = Math.max(imageHeight,this.lineHeight);			
		}
		else if(this.layout == Style.LAYOUT_BOTTOM |
				this.layout == Style.LAYOUT_TOP ){
			factWidth  = Math.max(imageWidth,this.strWidth);
			factHeight = imageHeight+this.lineHeight;
		}
		//增加空白边
		if(this.image != null || this.string != null){
			factWidth  += selfStyle.paddingLeft + selfStyle.paddingRight;
			factHeight += selfStyle.paddingTop  + selfStyle.paddingBottom;
		}
		this.bonds[1][0] = this.bonds[0][0] + factWidth;
		this.bonds[1][1] = this.bonds[0][1] + factHeight;
	}
	
	public void paint(Graphics g){
		Style s = this.getStyle();
		this.paint(g, s);
	}
	
	private void paint(Graphics g,Style s){
		int x = this.getX();
		int y = this.getY();
		int w = bonds[1][0]-bonds[0][0];
		int h = bonds[1][1]-bonds[0][1];
	    Image offImage = Image.createImage(w,h);	    	
	    Graphics offGraphics = offImage.getGraphics();
	    //设置背景颜色	       
	    offGraphics.setColor(this.hasFocus()?s.bColorFocus:s.bColorNormal);
	    offGraphics.fillRoundRect(0, 0, w, h, s.rountWidth, s.roundHeight);	        		
        if(!this.hasFocus()){
        	if( s.bImageLeftTop != null){
        	    offGraphics.drawRegion(s.bImageLeftTop, x, y<0?0:y, w, h, Sprite.TRANS_NONE, 0, y<0?-y:0, Graphics.LEFT|Graphics.TOP);
        	}
        }
        int xImageOff=0,yImageOff=0;
        if(this.layout == Style.LAYOUT_RIGHT){
            //画图片
        	if(this.image != null){
        		xImageOff= image.getWidth()+s.paddingLeft;
        		yImageOff= image.getHeight() + s.paddingTop;
        	    offGraphics.drawImage(image, s.paddingLeft, s.paddingTop, Graphics.LEFT|Graphics.TOP);
        	}
        	if(this.string != null){
        	//设置文字颜色
                offGraphics.setColor(this.hasFocus()?s.fColorFocus:s.fColorNormal);
                offGraphics.setFont(s.dfont);
                offGraphics.drawString(this.string, xImageOff+s.paddingLeft, (yImageOff-this.lineHeight)/2+s.paddingTop, Graphics.LEFT | Graphics.TOP);
            }
        }
        else{
        	//画图片
        	if(this.image != null){
        		xImageOff= image.getWidth()+s.paddingLeft;
        		yImageOff= image.getHeight() + s.paddingTop;
        	    offGraphics.drawImage(image, s.paddingLeft, s.paddingTop, Graphics.LEFT|Graphics.TOP);
        	}
        	if(this.string != null){
        	//设置文字颜色
                offGraphics.setColor(this.hasFocus()?s.fColorFocus:s.fColorNormal);
                offGraphics.setFont(s.dfont);
                offGraphics.drawString(this.string, (xImageOff-this.strWidth)/2+s.paddingLeft, yImageOff+s.paddingTop, Graphics.LEFT | Graphics.TOP);
            }
        	
        }        
	    //设置可见区域	  
        if( y + h > owner.vHeight){
        	h = owner.vHeight - y;
        }
        g.setClip(x, y, w, h);
        g.drawImage(offImage, x, y, Graphics.TOP | Graphics.LEFT);		
	}
	
	public String getTypeName() {
		return "ImageItem";
	}

}

⌨️ 快捷键说明

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