{geshibot lang="javascript"}import javax.microedition.lcdui.Graphics;
/
* 图片任意角度翻转算法
* 同时实现了Nokia特有API
* author. BB
* Sprite.java
*
*
*/
public class Sprite {
/** SIN TABLE **/
public final static int SIN_TABLE[] =
{
0, 4, 8, 13, 17, 22, 26, 31, 35, 39,
44, 48, 53, 57, 61, 65, 70, 74, 78, 83,
87, 91, 95, 99, 103, 107, 111, 115, 119, 123,
127, 131, 135, 138, 142, 146, 149, 153, 156, 160,
163, 167, 170, 173, 177, 180, 183, 186, 189, 192,
195, 198, 200, 203, 206, 208, 211, 213, 216, 218,
220, 223, 225, 227, 229, 231, 232, 234, 236, 238,
239, 241, 242, 243, 245, 246, 247, 248, 249, 250,
251, 251, 252, 253, 253, 254, 254, 254, 254, 254,
255, 254, 254, 254, 254, 254, 253, 253, 252, 251,
251, 250, 249, 248, 247, 246, 245, 243, 242, 241,
239, 238, 236, 234, 232, 231, 229, 227, 225, 223,
220, 218, 216, 213, 211, 208, 206, 203, 200, 198,
195, 192, 189, 186, 183, 180, 177, 173, 170, 167,
163, 160, 156, 153, 149, 146, 142, 138, 135, 131,
127, 123, 119, 115, 111, 107, 103, 99, 95, 91,
87, 83, 78, 74, 70, 65, 61, 57, 53, 48,
44, 39, 35, 31, 26, 22, 17, 13, 8, 4,
0, -4, -8, -13, -17, -22, -26, -31, -35, -39,
-44, -48, -53, -57, -61, -65, -70, -74, -78, -83,
-87, -91, -95, -99, -103, -107, -111, -115, -119, -123,
-127, -131, -135, -138, -142, -146, -149, -153, -156, -160,
-163, -167, -170, -173, -177, -180, -183, -186, -189, -192,
-195, -198, -200, -203, -206, -208, -211, -213, -216, -218,
-220, -223, -225, -227, -229, -231, -232, -234, -236, -238,
-239, -241, -242, -243, -245, -246, -247, -248, -249, -250,
-251, -251, -252, -253, -253, -254, -254, -254, -254, -254,
-255, -254, -254, -254, -254, -254, -253, -253, -252, -251,
-251, -250, -249, -248, -247, -246, -245, -243, -242, -241,
-239, -238, -236, -234, -232, -231, -229, -227, -225, -223,
-220, -218, -216, -213, -211, -208, -206, -203, -200, -198,
-195, -192, -189, -186, -183, -180, -177, -173, -170, -167,
-163, -160, -156, -153, -149, -146, -142, -138, -135, -131,
-127, -123, -119, -115, -111, -107, -103, -99, -95, -91,
-87, -83, -78, -74, -70, -65, -61, -57, -53, -48,
-44, -39, -35, -31, -26, -22, -17, -13, -8, -4
};
/** 透明 **/
public static int TRANSPARENT = 0;
/** 不透明 **/
public static int OPAQUE = 15;
/**
* 像素值
* 包含动画帧,主下标标识动画帧数,副下标的值代表像素值
*/
public short pixels[][];
/*
* 精灵的宽度
* 该版本默认精灵各个帧的宽度一样大小
*/
public short width;
/*
* 精灵的高度
* 该版本默认精灵各个帧的高度一样大小
*/
public short height;
/*
* 动画帧数
*/
public byte numOfFrame;
/*
* 当前动画帧数
*/
public int curFrame;
/*
* 构造函数
* 确保传入的各个值为合理的值
* 使用的话请自己做判断
* @param _pixels 动画的像素值
* @param _width 宽度
* @param _height 高度
*/
public Sprite(short[][] _pixels, short _width, short _height){
pixels = _pixels;
width = _width;
height = _height;
numOfFrame = (byte)_pixels.length;
curFrame = 0;
}
/*
* 构造函数
*/
public Sprite(){
}
/*
* 投影
* @param cData
* @param l
* @param i1
* @param j1
* @param k1
* @return
*/
private final short project(short cData[], int l, int i1, int j1, int k1) {
int j3 = (j1 & 0xffff) >> 8;
int k3 = (k1 & 0xffff) >> 8;
int l3 = (256 – j3) * (256 – k3);
int i4 = j3 * (256 – k3);
int j4 = (256 – j3) * k3;
int k4 = j3 * k3;
int l2 = j1 >> 16;
int i3 = k1 >> 16;
l2 %= l;
i3 %= i1;
i3 *= l;
short word0 = cData[l2 + i3];
short word2 = cData[(l2 + (i3 + l)) % (cData.length – 1)];
l2 = ++l2 % l;
short word1 = cData[l2 + i3];
short word3 = cData[(l2 + (i3 + l)) % (cData.length – 1)];
int l1 = word0 >> 12 & 0xf;
int i2 = word1 >> 12 & 0xf;
int j2 = word2 >> 12 & 0xf;
int k2 = word3 >> 12 & 0xf;
int k5 = l1 * l3 + i2 * i4 + j2 * j4 + k2 * k4 >> 16;
l1 = word0 >> 8 & 0xf;
i2 = word1 >> 8 & 0xf;
j2 = word2 >> 8 & 0xf;
k2 = word3 >> 8 & 0xf;
int l4 = l1 * l3 + i2 * i4 + j2 * j4 + k2 * k4 >> 16;
l1 = word0 >> 4 & 0xf;
i2 = word1 >> 4 & 0xf;
j2 = word2 >> 4 & 0xf;
k2 = word3 >> 4 & 0xf;
int i5 = l1 * l3 + i2 * i4 + j2 * j4 + k2 * k4 >> 16;
l1 = word0 & 0xf;
i2 = word1 & 0xf;
j2 = word2 & 0xf;
k2 = word3 & 0xf;
int j5 = l1 * l3 + i2 * i4 + j2 * j4 + k2 * k4 >> 16;
return (short) ((k5 << 12) + (l4 << 8) + (i5 << 4) + j5);
}
/*
* 翻转
* @param count 在360度内翻转的个数
* @return 返回一个翻转后的精灵
*/
public Sprite rotate(int count) {
Sprite sprite = new Sprite();
short radius = 30;
if (width == 40 && height == 40)
radius = 57;
else if (width == 8 && height == 8)
radius = 12;
else if (width == 6 && height == 12)
radius = 14;
else if (width == 24 && height == 24)
radius = 34;
else if (width == 26 && height == 26)
radius = 27;
else if (width == 8 && height == 15)
radius = 17;
else if (width == 10 && height == 16)
radius = 19;
else if (width == 11 && height == 15)
radius = 19;
else
System.out.println("Wrong size: " + width + " " + width + " "
+ count);
sprite.width = radius;
sprite.height = radius;
sprite.numOfFrame = (byte)count;
sprite.pixels = new short[count][];
for (int j1 = 0; j1 < count; j1++) {
sprite.pixels[j1] = new short[sprite.width * sprite.height];
int k1 = j1 * (360 / count);
short l1 = width;
short i2 = height;
width = sprite.width;
height = sprite.height;
int j2 = width / 2;
int k2 = l1 / 2;
int l2 = i2 / 2;
int i3 = SIN_TABLE[k1 % 360] << 8;
int j3 = SIN_TABLE[(k1 + 90) % 360] << 8;
int k4 = 0;
int i4 = -j2 * j3;
int j4 = -j2 * i3;
for (int l4 = 0; l4 < sprite.pixels[j1].length; l4++)
sprite.pixels[j1][l4] = (short) (TRANSPARENT << 12);
for (int i5 = 0; i5 < height; i5++) {
int k3 = -j2 * j3;
int l3 = -j2 * i3;
for (int j5 = 0; j5 < width; j5++) {
//x
int k5 = (k3 – j4 >> 16) + k2;
//y
int l5 = (i4 + l3 >> 16) + l2;
if (k5 >= 0 && l5 >= 0 && l5 < i2 && k5 < l1)
sprite.pixels[j1][k4] = project(pixels[0], l1, i2,
(k3 – j4) + (k2 << 16), i4 + l3 + (l2 << 16));
k3 += j3;
l3 += i3;
k4++;
}
i4 += j3;
j4 += i3;
k4 -= width;
k4 += sprite.width;
}
height = i2;
width = l1;
}
return sprite;
}
/*
* 绘制精灵
* 在这里实现了Nokia的特有API
* 大家可以扩展该方法
* @param g
* @param x
* @param y
* @param manipulate
*/
public void draw(Graphics g, int x, int y, int manipulate) {
drawPixels(g, true, 0, width, x, y, width,
height, manipulate, 4444);
}
public void drawPixels(Graphics g, boolean transparency,
int offset, int scanlength, int x, int y, int width, int height,
int manipulation, int format) {
int l1 = map2Manipulation(manipulation);
int j1;
int k1;
if ((l1 & 4) != 0) {
j1 = height;
k1 = width;
} else {
j1 = width;
k1 = height;
}
short newPixels[] = new short [j1 * k1];
if(manipulation == 0){
newPixels = pixels[curFrame];
}
else
for (int i2 = 0; i2 < k1; i2++) {
for (int j2 = 0; j2 < j1; j2++) {
int j = j2;
int k = i2;
if ((l1 & 1) != 0)
j = j1 – 1 – j;
if ((l1 & 2) != 0)
k = k1 – 1 – k;
if ((l1 & 4) != 0) {
int k2 = j;
j = k;
k = k2;
}
newPixels[j1 * i2 + j2] = pixels[curFrame][width * k + j];
}
}
int off = offset;
int vw = x + j1;
int vh = y + k1;
for(int idy = y; idy < vh; idy++){
int voff = off;
for(int idx = x; idx < vw; idx++){
short pixel = newPixels[voff++];
int k3 = idx;
for(; idx < vw – 1 && newPixels[voff] == pixel; voff++)
idx++;
if((pixel >> 12 & 0xff) != 0)
{
int l3 = 0xf0 & pixel << 4;
l3 |= 0xf000 & pixel << 8;
l3 |= 0xf00000 & pixel << 12;
g.setColor(l3);
g.drawLine(k3, idy, idx, idy);
}
}
off += j1;
}
}
private static int map2Manipulation(int i) throws IllegalArgumentException {
int j = 0;
if ((i & 0x2000) != 0)
j ^= 1;
if ((i & 0x4000) != 0)
j ^= 2;
switch (i & 0xffff9fff) {
case 90: // 'Z'
j ^= 6;
break;
case 180:
j ^= 3;
break;
case 270:
j ^= 5;
break;
default:
throw new IllegalArgumentException();
case 0: // '\0'
break;
}
return j;
}
}{/geshibot}