关键点是强转的时机 /** * 函数功能:旋转图片 * * @param angle * 角度 范围0-90(度) * @return 顺时针旋转angle角度后的图片 */ public Image timg(int angle) { a = angle * Math.PI / 180; tw = (int) (w * Math.cos(a) + h * Math.sin(a) + 1); th = (int) (h * Math.cos(a) + w * Math.sin(a) + 1); for (int i = 0; i < tw; i++) { for (int j = 0; j < th; j++) { tdata[i + tw * j] = 0xffffff; } } for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { tdata[(int) ((h - j) * Math.sin(a) + i * Math.cos(a)) + tw * (int) (i * Math.sin(a) + j * Math.cos(a))] = temp[i + w * j]; } } return Image.createRGBImage(tdata, tw, th, true); } 附游戏工具类(测试版) package engine; import java.io.IOException; import javax.microedition.lcdui.Image; public class Tools { private Tools() { } /** * @param enemy * 敌人图片文件名 * @param blast * 爆炸图片文件名 * @param gun * 浮游炮图片文件名 */ public static void init(String enemy, String blast, String gun) { try { img_temp = Image.createImage(enemy); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } img_enemy = getArrayOfImage(img_temp, 6, 1); img_temp = null; try { img_temp = Image.createImage(blast); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } img_blast = getArrayOfImage(img_temp, 1, 4); img_temp = null; try { img_gun = Image.createImage(gun); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } w = img_gun.getWidth(); h = img_gun.getHeight(); tdata = new int[(w + 1) * (h + 1) * 2]; temp = new int[w * h]; } /** * @param type * 敌人种类 绿色敌人为0,黄色1白色2紫色3蓝色4幽灵5 * @return 相应种类的敌人图片 */ public static Image getEnemyImg(int type) { return img_enemy[type]; } /** * @param num * 爆炸图片编号 * @return 相应编号的图片 */ public static Image getBlastImg(int num) { return img_blast[num]; } /** * 函数功能:将整图分解成row*col张小图片 * * @param img * 图片资源 * @param row * 图片有几行 * @param col * 图片有几列 * @return row*col大小的图片数组 */ public static Image[] getArrayOfImage(Image img, int row, int col) { Image[] imgs = null; int width = img.getWidth() / col; int height = img.getHeight() / row; int len = row * col; imgs = new Image[len]; for (int i = 0; i < len; i++) { imgs[i] = Image.createImage(img, i % col * width, i / col * height, width, height, 0); } return imgs; } /** * 函数功能:旋转图片 * * @param angle * 角度 范围0-90(度) * @return 顺时针旋转angle角度后的图片 */ public Image timg(int angle) { a = angle * Math.PI / 180; tw = (int) (w * Math.cos(a) + h * Math.sin(a) + 1); th = (int) (h * Math.cos(a) + w * Math.sin(a) + 1); for (int i = 0; i < tw; i++) { for (int j = 0; j < th; j++) { tdata[i + tw * j] = 0xffffff; } } for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { tdata[(int) ((h - j) * Math.sin(a) + i * Math.cos(a)) + tw * (int) (i * Math.sin(a) + j * Math.cos(a))] = temp[i + w * j]; } } return Image.createRGBImage(tdata, tw, th, true); } /** * 浮游炮图片旋转角度 弧度值 */ double a; /** * 浮游炮图片宽度 */ static int w; /** * 浮游炮图片高度 */ static int h; /** * 旋转后浮游炮图片宽度 */ int tw; /** * 旋转后浮游炮图片高度 */ int th; /** * 旋转后浮游炮图片RGB值数组 */ static int[] tdata = null; /** * 浮游炮图片RGB值数组 */ static int[] temp = null; private static Image img_temp = null; /** * 敌人图片数组 */ private static Image[] img_enemy = null; /** * 爆炸图片数组 */ private static Image[] img_blast = null; /** * 浮游炮图片 */ public static Image img_gun = null; }