天天看點

J2ME 工具方法整理,部分借鑒自網絡

/**

* 工具類

* @author Ckenny

*

*/

public class Util {

/**

* 圖檔縮放

* 取出原圖的RGB數組(srcData),然後根據縮放比例取得縮放後圖檔的RGB數組(toData)的各個位置的顔色值

* 最後利用toData生成圖檔并傳回

*

* @param src 原圖

* @param toWidth 縮放後的寬度

* @param toHeight 縮放後的高度

* @return 縮放後的圖檔

*/

public static Image ZoomImage(Image src,int toWidth,int toHeight){

Image result = null;

if(src!=null&&toWidth>0&&toHeight>0){

try{

int srcWidth = src.getWidth();

int srcHeight = src.getHeight();

int scaleWidth = srcWidth/toWidth;

int scaleHeight = srcHeight/toHeight;

int[] toData = new int[toWidth*toHeight];

int[] srcData = new int[srcWidth*srcHeight];

src.getRGB(srcData, 0, srcWidth, 0, 0, srcWidth, srcHeight);

for(int toY = 0; toY < toHeight; toY++){

for(int toX = 0; toX < toWidth; toX++){

int scaleX = toX*scaleWidth;

int scaleY = toY*scaleHeight;

toData[toY*toWidth+toX] = srcData[scaleY*srcHeight+scaleX];

}

}

result = Image.createRGBImage(toData, toWidth, toHeight, false);

}catch(Exception e){

return result;

}

}

return result;

}

/**

* 繪制一個指定顔色的矩形,在矩形區域内顔色沿垂直方向漸變

* 初始化一個指定大小矩形區域内的顔色數組,

* @param color 指定的顔色

* @param width 矩形寬度

* @param height 矩形高度

*/

public static void drawVerticalShadeRect(Graphics g,int x,int y,int color,int width,int height){

int[] data = new int[width*height];

for(int row = 0; row < height; row++){

int a = row;

if(a>255)

a = 255;

int rowColor = color|(a<<24);

for(int cal = 0; cal < width; cal++){

data[row*cal+cal]=rowColor;

}

}

g.drawRGB(data, 0, width, x, y, width, height, true);

}

/**

* 繪制一個指定顔色的矩形,在矩形區域内顔色沿水準方向漸變

* 初始化一個指定大小矩形區域内的顔色數組,

* @param color 指定的顔色

* @param width 矩形寬度

* @param height 矩形高度

*/

public static void drawHorizontalShadeRect(Graphics g,int x,int y,int color,int width,int height){

int[] data = new int[width*height];

for(int row = 0; row < width; row++){

int a = row;

if(a>255)

a = 255;

int rowColor = color|(a<<24);

for(int cal = 0; cal < width; cal++){

data[row*cal+cal]=rowColor;

}

}

g.drawRGB(data, 0, width, x, y, width, height, true);

}

/**

* 繪制一個矩形,在矩形區域内顔色值由color1到color2沿指定的方向漸變

* 1,定義一個width*height的顔色矩陣數組

* 2,取出color1和color2的顔色RGB值,然後計算出color1到color2 RGB的各個內插補點

* 3,再根據比例計算width*height顔色矩陣數組中從color1 到color2的各個RGB值并生産相應的顔色值指派給對應的數組元素

* 4,繪制該顔色到width*height區域

* @param color 指定的顔色

* @param width 矩形寬度

* @param height 矩形高度

*/

public static void drawShadeRect(Graphics g,int x,int y,int color1,int color2,int width,int height){

int[] shadeArea = new int[(width*height)];

if(height>3){

int[] color1RGB = new int[]{(color1&0x00ff0000)>>16,(color1&0x0000ff00)>>8,(color1&0x000000ff)};

int[] color2RGB = new int[]{(color2&0x00ff0000)>>16,(color2&0x0000ff00)>>8,(color2&0x000000ff)};

int readSpread = color1RGB[0] - color2RGB[0];

int greenSpread = color1RGB[1] - color2RGB[1];

int blueSpread = color1RGB[2] - color2RGB[2];

for(int i = 0; i< height; i++){

int color = 0;

if(i==height-1){

color = color2RGB[0]<<16|color2RGB[1]<<8|color2RGB[2];

}else{

int shadeAreaRead = color1RGB[0]+readSpread*i/height;

int shadeAreaGreen = color1RGB[1]+greenSpread*i/height;

int shadeAreaBlue = color1RGB[2]+blueSpread*i/height;

if(shadeAreaRead<0||shadeAreaGreen<0||shadeAreaBlue<0||shadeAreaRead>255||shadeAreaGreen>255||shadeAreaBlue>255){

color = 0xfffff;

}else{

color = shadeAreaRead<<16|shadeAreaGreen<<8|shadeAreaBlue;

}

}

for(int row = 0; row < width; row++){

shadeArea[i+row*width]=color;

}

}

}else{

for(int i = 0; i< height; i++){

int color = i==0?color1:color2;

for(int row = 0; row < width; row++){

shadeArea[i+row*width]=color;

}

}

}

g.drawRGB(shadeArea, 0, width, x, y, width, height, true);

}

/**

* 字元串分行處理

* @param str 原字元串

* @param rowWidth 行寬

* @param font 目前使用的字型

* @return 各行字元串

*/

public static Vector stringLayout(String str,int rowWidth,Font font){

int lineBegin = 0;

str=str+"";

Vector lines = new Vector(5,2);

for(int i = 0; i < str.length(); i++){

char c = str.charAt(i);

if(font.stringWidth(str.substring(lineBegin,i+1))>=rowWidth||c=='/n'||i==str.length()-1){

lines.addElement(str.substring(lineBegin,i));

if(c=='/n')

lineBegin = i+1;

else

lineBegin = i;

}

}

return lines;

}

}

繼續閱讀