j2me实现图像旋转

/*
 * Created on 2004-12-4
 *
 * Abstract :这是一个显示图像旋转算法的例子,
 * 为了显示如何旋转一副图像,创建一个MIDlet,然后在上面显示一个Canvas的子类ViewCanvas,
 * ViewCanvas是一个自动化对象,不停的旋转图像然后显示。
 * 使用者只需要把originImage = Image.createImage("/resource/popo.png");这一行里的图像文件改成
 * 真实存在的图像文件就可以运行了。
 */
package SayWord;

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * @author 快让我睡个好觉  wzhh@ustc.edu
 *
 * 这是一个MIDlet子类
 */
public class Hello extends MIDlet {

/**
 * 
 */
private Display display;
private ViewCanvas canvas;   

public Hello() {
super();
display =Display.getDisplay(this);
canvas  = new ViewCanvas(this);
}

protected void startApp() throws MIDletStateChangeException {
display.setCurrent( canvas ); // 设置显示画布对象
}

protected void pauseApp() {

}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

}

public void exitMIDlet()
    {
  try
  {
    destroyApp( true);
  }
  catch(MIDletStateChangeException e)
  {
  }
      notifyDestroyed();
    }
}

// 自动化对象,同时也是Canvas的子类
class ViewCanvas extends Canvas implements CommandListener, Runnable
{
      private Command cmExit;           // Exit midlet
      private String keyText = "0";     // Key code text,用来记录用户的击键,在这个例子里不使用
      private int angle = 0;            // 旋转的角度
      private Image originImage;        // 原始图像
      private Hello midlet;
      private boolean alive = true;
      private int interval = 100;
      private Thread thread = null;
      /*--------------------------------------------------
      * Constructor
      *-------------------------------------------------*/
      public ViewCanvas(Hello midlet)
      {
        this.midlet = midlet;
        try
{
         originImage = Image.createImage("/resource/popo.png");
}
        catch(Exception e)
{
         System.out.println("load image fail!!!!!"); 
        }
        // Create exit command and listen for events
        cmExit = new Command("退出", Command.EXIT, 1);
        addCommand(cmExit);
        setCommandListener(this);
        thread= new Thread(this);
        thread.start();
      }
      
    // 这是一个把图像绕中心点旋转一个角度的程序代码
    // 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);
   }
  
      /*--------------------------------------------------
      * Paint the text representing the key code
      *-------------------------------------------------*/
      protected void paint(Graphics g)
      {
        // Clear the background (to white)
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage( TransferImage(originImage, angle),0,0,Graphics.TOP | Graphics.LEFT);        
        angle = (angle + 30) % 360; // 下一个旋转角度
      }
      public void run() {
       while(true) 
       {
       if (alive) 
       {
       repaint();
       try 
{
       Thread.sleep(interval);

       catch (InterruptedException e) 
{
       }
       }
       }
      }

      /*--------------------------------------------------
      * Command event handling
      *-------------------------------------------------*/
      public void commandAction(Command c, Displayable d)
      {
        if (c == cmExit)
          midlet.exitMIDlet();
      }
      /*--------------------------------------------------
      * Key code event handling
      *-------------------------------------------------*/
      protected void keyPressed(int keyCode)
      {
        keyText = getKeyName(keyCode);
        repaint();
      }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值