程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> 手機上實現任意角度翻轉算法[適用任何機型]

手機上實現任意角度翻轉算法[適用任何機型]

編輯:J2ME

web2.0package rorate;

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;

       }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved