j2me图片任意角度翻转算法
importjavax.microedition.lcdui.Graphics;
/*
*图片任意角度翻转算法
*同时实现了Nokia特有API
*author.BB
*Sprite.java
*
*
*/
publicclassSprite{
/**SINTABLE**/
publicfinalstaticintSIN_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
};
/**透明**/
publicstaticintTRANSPARENT=0;
/**不透明**/
publicstaticintOPAQUE=15;
/**
*像素值
*包含动画帧,主下标标识动画帧数,副下标的值代表像素值
*/
publicshortpixels[][];
/*
*精灵的宽度
*该版本默认精灵各个帧的宽度一样大小
*/
publicshortwidth;
/*
*精灵的高度
*该版本默认精灵各个帧的高度一样大小
*/
publicshortheight;
/*
*动画帧数
*/
publicbytenumOfFrame;
/*
*当前动画帧数
*/
publicintcurFrame;
/*
*构造函数
*确保传入的各个值为合理的值
*使用的话请自己做判断
*@param_pixels动画的像素值
*@param_width宽度
*@param_height高度
*/
publicSprite(short[][]_pixels,short_width,short_height){
pixels=_pixels;
width=_width;
height=_height;
numOfFrame=(byte)_pixels.length;
curFrame=0;
}
/*
*构造函数
*/
publicSprite(){
}
/*
*投影
*@paramcData
*@paraml
*@parami1
*@paramj1
*@paramk1
*@return
*/
privatefinalshortproject(shortcData[],intl,inti1,intj1,intk1){
intj3=(j1&0xffff)>>8;
intk3=(k1&0xffff)>>8;
intl3=(256-j3)*(256-k3);
inti4=j3*(256-k3);
intj4=(256-j3)*k3;
intk4=j3*k3;
intl2=j1>>16;
inti3=k1>>16;
l2%=l;
i3%=i1;
i3*=l;
shortword0=cData[l2+i3];
shortword2=cData[(l2+(i3+l))%(cData.length-1)];
l2=++l2%l;
shortword1=cData[l2+i3];
shortword3=cData[(l2+(i3+l))%(cData.length-1)];
intl1=word0>>12&0xf;
inti2=word1>>12&0xf;
intj2=word2>>12&0xf;
intk2=word3>>12&0xf;
intk5=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;
intl4=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;
inti5=l1*l3+i2*i4+j2*j4+k2*k4>>16;
l1=word0&0xf;
i2=word1&0xf;
j2=word2&0xf;
k2=word3&0xf;
intj5=l1*l3+i2*i4+j2*j4+k2*k4>>16;
return(short)((k5<<12)+(l4<<+(i5<<4)+j5);
}
/*
*翻转
*@paramcount在360度内翻转的个数
*@return返回一个翻转后的精灵
*/
publicSpriterotate(intcount){
Spritesprite=newSprite();
shortradius=30;
if(width==40&&height==40)
radius=57;
elseif(width==8&&height==
radius=12;
elseif(width==6&&height==12)
radius=14;
elseif(width==24&&height==24)
radius=34;
elseif(width==26&&height==26)
radius=27;
elseif(width==8&&height==15)
radius=17;
elseif(width==10&&height==16)
radius=19;
elseif(width==11&&height==15)
radius=19;
else
System.out.println("Wrongsize:"+width+""+width+""+count);
sprite.width=radius;
sprite.height=radius;
sprite.numOfFrame=(byte)count;
sprite.pixels=newshort[count][];
for(intj1=0;j1<count;j1++){
sprite.pixels[j1]=newshort[sprite.width*sprite.height];
intk1=j1*(360/count);
shortl1=width;
shorti2=height;
width=sprite.width;
height=sprite.height;
intj2=width/2;
intk2=l1/2;
intl2=i2/2;
inti3=SIN_TABLE[k1%360]<<8;
intj3=SIN_TABLE[(k1+90)%360]<<8;
intk4=0;
inti4=-j2*j3;
intj4=-j2*i3;
for(intl4=0;l4<sprite.pixels[j1].length;l4++)
sprite.pixels[j1][l4]=(short)(TRANSPARENT<<12);
for(inti5=0;i5<height;i5++){
intk3=-j2*j3;
intl3=-j2*i3;
for(intj5=0;j5<width;j5++){
//x
intk5=(k3-j4>>16)+k2;
//y
intl5=(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;
}
returnsprite;
}
/*
*绘制精灵
*在这里实现了Nokia的特有API
*大家可以扩展该方法
*@paramg
*@paramx
*@paramy
*@parammanipulate
*/
publicvoiddraw(Graphicsg,intx,inty,intmanipulate){
drawPixels(g,true,0,width,x,y,width,
height,manipulate,4444);
}
publicvoiddrawPixels(Graphicsg,booleantransparency,
intoffset,intscanlength,intx,inty,intwidth,intheight,
intmanipulation,intformat){
intl1=map2Manipulation(manipulation);
intj1;
intk1;
if((l1&4)!=0){
j1=height;
k1=width;
}else{
j1=width;
k1=height;
}
shortnewPixels[]=newshort[j1*k1];
if(manipulation==0){
newPixels=pixels[curFrame];
}
else
for(inti2=0;i2<k1;i2++){
for(intj2=0;j2<j1;j2++){
intj=j2;
intk=i2;
if((l1&1)!=0)
j=j1-1-j;
if((l1&2)!=0)
k=k1-1-k;
if((l1&4)!=0){
intk2=j;
j=k;
k=k2;
}
newPixels[j1*i2+j2]=pixels[curFrame][width*k+j];
}
}
intoff=offset;
intvw=x+j1;
intvh=y+k1;
for(intidy=y;idy<vh;idy++){
intvoff=off;
for(intidx=x;idx<vw;idx++){
shortpixel=newPixels[voff++];
intk3=idx;
for(;idx<vw-1&&newPixels[voff]==pixel;voff++)
idx++;
if((pixel>>12&0xff)!=0)
{
intl3=0xf0&pixel<<4;
l3|=0xf000&pixel<<8;
l3|=0xf00000&pixel<<12;
g.setColor(l3);
g.drawLine(k3,idy,idx,idy);
}
}
off+=j1;
}
}
privatestaticintmap2Manipulation(inti)throwsIllegalArgumentException{
intj=0;
if((i&0x2000)!=0)
j^=1;
if((i&0x4000)!=0)
j^=2;
switch(i&0xffff9fff){
case90://'Z'
j^=6;
break;
case180:
j^=3;
break;
case270:
j^=5;
break;
default:
thrownewIllegalArgumentException();
case0://'\0'
break;
}
returnj;
}
}