/** * 工具类 * @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; } }