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

📄 imagemanager.java

📁 Java手机游戏源代码(企业公司源代码)供初学者参考。
💻 JAVA
字号:
package com.gt.mobile;

import java.io.IOException;
import java.util.Hashtable;

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

public class ImageManager {

	private static Hashtable imageHashtable = new Hashtable();

	private static String directory = "";

	public static final void setDirectory(String dir) {
		directory = "/" + dir;
	}

	public static final Image getPngImage(String name) {
		return getPngImage(name, true);
	}

	public static final Image getPngImage(String name, boolean cache) {
//		System.out.println(directory + "/" + name + ".png");
		Image image = null;
		try {
			image = (Image) imageHashtable.get(name);
			if (image == null) {
				image = Image.createImage(directory + "/" + name + ".png");
				if (cache) {
					imageHashtable.put(name, image);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return image;
	}

	public static final void removePngImage(String name) {
		imageHashtable.remove(name);
	}

	public static final Image getGradualImage(int fromColor, int toColor,
			int width, int height, boolean isHorizontal) {
		Image image = Image.createImage(width, height);
		Graphics g = image.getGraphics();
		int fr = fromColor / 256 / 256;
		int fg = (fromColor / 256) % 256;
		int fb = fromColor & 0xff;
		int tr = toColor / 256 / 256;
		int tg = (toColor / 256) % 256;
		int tb = toColor & 0xff;
		int rd;
		int gd;
		int bd;
		if (isHorizontal) {
			rd = (tr - fr) * 100 / width;
			gd = (tg - fg) * 100 / width;
			bd = (tb - fb) * 100 / width;
			for (int i = 0; i < width; i++) {
				g.setColor(fr + (i * rd) / 100, fg + (i * gd) / 100, fb
						+ (i * bd) / 100);
				g.drawRect(i, 0, width - i, height);
			}
		} else {
			rd = (tr - fr) * 100 / height;
			gd = (tg - fg) * 100 / height;
			bd = (tb - fb) * 100 / height;
			for (int i = 0; i < height; i++) {
				g.setColor(fr + (i * rd) / 100, fg + (i * gd) / 100, fb
						+ (i * bd) / 100);
				g.drawRect(0, i, width, height - i);
			}
		}
		return Image.createImage(image);
	}

	public static Image[] getSplitRectImages(String imageName, int bgColor,
			int width, int height) {
		Image srcImage = getPngImage(imageName, false);
		int rectsWidth = srcImage.getWidth() / width
				+ (srcImage.getWidth() % width > 0 ? 1 : 0);
		int rectsHeight = srcImage.getHeight() / height
				+ (srcImage.getHeight() % height > 0 ? 1 : 0);
		// System.out.println("rectsWidth:"+rectsWidth+"
		// rectsHeight:"+rectsHeight);
		Image[] rectImages = new Image[rectsWidth * rectsHeight];
		Graphics tmpg = null;
		for (int i = 0; i < rectsHeight; i++) {
			for (int j = 0; j < rectsWidth; j++) {
				rectImages[i * rectsWidth + j] = Image.createImage(width,
						height);
				tmpg = rectImages[i * rectsWidth + j].getGraphics();
				tmpg.setColor(bgColor);
				tmpg.fillRect(0, 0, width, height);
				tmpg.drawImage(srcImage, -j * width, -i * height, 0);
				rectImages[i * rectsWidth + j] = Image.createImage(rectImages[i
						* rectsWidth + j]);
			}
		}
		return rectImages;
	}

	public static void paintNumber(Graphics g, int x, int y) {

	}

	public static Image ZoomImage(Image src, int desW, int desH) {
		Image desImg = null;
		int srcW = src.getWidth(); // 原始图像宽
		int srcH = src.getHeight(); // 原始图像高
		int[] srcBuf = new int[srcW * srcH]; // 原始图片像素信息缓存

		src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);

		// 计算插值表
		int[] tabY = new int[desH];
		int[] tabX = new int[desW];

		int sb = 0;
		int db = 0;
		int tems = 0;
		int temd = 0;
		int distance = srcH > desH ? srcH : desH;
		for (int i = 0; i <= distance; i++) { /* 垂直方向 */
			tabY[db] = sb;
			tems += srcH;
			temd += desH;
			if (tems > distance) {
				tems -= distance;
				sb++;
			}
			if (temd > distance) {
				temd -= distance;
				db++;
			}
		}

		sb = 0;
		db = 0;
		tems = 0;
		temd = 0;
		distance = srcW > desW ? srcW : desW;
		for (int i = 0; i <= distance; i++) { /* 水平方向 */
			tabX[db] = (short) sb;
			tems += srcW;
			temd += desW;
			if (tems > distance) {
				tems -= distance;
				sb++;
			}
			if (temd > distance) {
				temd -= distance;
				db++;
			}
		}

		// 生成放大缩小后图形像素buf
		int[] desBuf = new int[desW * desH];
		int dx = 0;
		int dy = 0;
		int sy = 0;
		int oldy = -1;
		for (int i = 0; i < desH; i++) {
			if (oldy == tabY[i]) {
				System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
			} else {
				dx = 0;
				for (int j = 0; j < desW; j++) {
					desBuf[dy + dx] = srcBuf[sy + tabX[j]];
					dx++;
				}
				sy += (tabY[i] - oldy) * srcW;
			}
			oldy = tabY[i];
			dy += desW;
		}

		// 生成图片
		desImg = Image.createRGBImage(desBuf, desW, desH, false);
		return desImg;
	}

	public static final Image scaleImage(Image srcImage, int destWidth,
			int destHeight) {
		int srcWidth = srcImage.getWidth();
		int srcHeight = srcImage.getHeight();

		Image tmpImage = Image.createImage(destWidth, destHeight);
		Graphics g = tmpImage.getGraphics();

		int delta = (srcWidth << 16) / destWidth;
		int pos = delta / 2;

		for (int x = 0; x < destWidth; x++) {
			g.setClip(x, 0, 1, srcHeight);
			g.drawImage(srcImage, x - (pos >> 16), 0, Graphics.LEFT
					| Graphics.TOP);
			pos += delta;
		}

		Image destImage = Image.createImage(destWidth, destHeight);
		g = destImage.getGraphics();

		delta = (srcHeight << 16) / destHeight;
		pos = delta / 2;

		for (int y = 0; y < destHeight; y++) {
			g.setClip(0, y, destWidth, 1);
			g.drawImage(tmpImage, 0, y - (pos >> 16), Graphics.LEFT
					| Graphics.TOP);
			pos += delta;
		}

		return destImage;
	}

	
	
	
	
	
	
	// 这是一个把图像绕中心点旋转一个角度的程序代码
	// OriginImage传入原始图像,函数的返回值是旋转后的图像
	// 这段代码只能旋转30度的整数倍角度,如果要旋转更精细
	// 改一下tabCos,tabSin就行了,这两个数组保存的是cos和sin乘于4096的值
	// 由于j2me不支持符点运算以及三角函数,所以用查表的方式计算sin,cos
	/*
	 * int[] tabCos = { 4096, 3547, 2048, 0, -2048, -3547, -4096, -3547, -2048,
	 * 0, 2048, 3547 };
	 * 
	 * int[] tabSin = { 0, 2048, 3547, 4096, 3547, 2048, 0, -2048, -3547, -4096,
	 * -3547, -2048 };
	 * 
	 * private Image TransferImage(Image OriginImage, int angle) { int w =
	 * OriginImage.getWidth(); int h = OriginImage.getHeight(); int ARGBData[] =
	 * new int[w * h]; int TranARGBData[] = new int[w * h];
	 * OriginImage.getRGB(ARGBData, 0, w, 0, 0, w, h); int centerX = w / 2; int
	 * centerY = h / 2; int i = ((360 - angle) % 360) / 30; for (int y1 = 0; y1 <
	 * h; y1++) { for (int x1 = 0; x1 < w; x1++) { // 这是坐标变换,不清楚的同志查一下坐标变换公式吧
	 * int x2 = (((x1 - centerX) * tabCos[i]) >> 12) - (((y1 - centerY) *
	 * tabSin[i]) >> 12) + centerX; int y2 = (((x1 - centerX) * tabSin[i]) >>
	 * 12) + (((y1 - centerY) * tabCos[i]) >> 12) + centerY; if ((x2 >= 0) &&
	 * (x2 < w) && (y2 >= 0) && (y2 < h)) { TranARGBData[y1 * w + x1] =
	 * ARGBData[y2 * w + x2]; } else { TranARGBData[y1 * w + x1] = 255 << 24; } } }
	 * return Image.createRGBImage(TranARGBData, w, h, true); }
	 */

	// 缩放
	// 明暗
	/**
	 * 
	 * @param imgSource
	 *            源图像
	 * 
	 * @param cx
	 *            旋转点相对于源图像坐上角横坐标
	 * 
	 * @param cy
	 *            旋转点相对于源图像坐上角纵坐标
	 * 
	 * @param theta
	 *            图像逆时针旋转的角度
	 * 
	 * @param dd
	 *            含2个元素的整形数组,存放新图像相对源图像沿x轴和y轴的位置偏移量
	 * 
	 * @return 旋转后的图像
	 * 
	 */

	/*
	 * public Image rotate(Image imgSource, int cx, int cy, double theta,
	 * double[] dd) {
	 * 
	 * if (Math.abs(theta % 360) < 0.1) return imgSource; // 角度很小时直接返回
	 * 
	 * int w1 = imgSource.getWidth(); // 原始图像的高度和宽度
	 * 
	 * int h1 = imgSource.getHeight();
	 * 
	 * int[] srcMap = new int[w1 * h1];
	 * 
	 * imgSource.getRGB(srcMap, 0, w1, 0, 0, w1, h1); // 获取原始图像的像素信息
	 * 
	 * int dx = cx > w1 / 2 ? cx : w1 - cx; // 计算旋转半径
	 * 
	 * int dy = cy > h1 / 2 ? cy : h1 - cy;
	 * 
	 * double dr = Math.sqrt(dx * dx + dy * dy);
	 * 
	 * int wh2 = (int) (2 * dr + 1); // 旋转后新图像为正方形,其边长+1是为了防止数组越界
	 * 
	 * int[] destMap = new int[wh2 * wh2]; // 存放新图像象素的数组
	 * 
	 * double destX, destY;
	 * 
	 * double radian = theta * Math.PI / 180; // 计算角度计算对应的弧度值
	 * 
	 * for (int i = 0; i < w1; i++) {
	 * 
	 * for (int j = 0; j < h1; j++) {
	 * 
	 * if (srcMap[j * w1 + i] >> 24 != 0) { // 对非透明点才进行处理 //
	 * 得到当前点经旋转后相对于新图像左上角的坐标
	 * 
	 * destX = dr + (i - cx) * Math.cos(radian) + (j - cy)* Math.sin(radian);
	 * 
	 * destY = dr + (j - cy) * Math.cos(radian) - (i - cx)* Math.sin(radian); //
	 * 从源图像中往新图像中填充像素
	 * 
	 * destMap[(int) destY * wh2 + (int) destX] = srcMap[j * w1 + i]; } } }
	 * dd[0] = cx-dr; // 返回位置偏移分量
	 * 
	 * dd[1] = cy-dr;
	 * 
	 * return Image.createRGBImage(destMap, wh2, wh2, true );// 返回旋转后的图像 }
	 */
}

⌨️ 快捷键说明

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