汇编网首页登录博客注册
acool的学习博客
博客首页博客互动【做检测题】论坛求助

我的博客

个人首页 |  我的文章 |  我的相册 |  我的好友 |  最新访客 |  文章收藏 |  论坛提问 |  友情链接 |  给我留言  
图片载入中
  •  --
  • 『姓名』:来,留,去
  • 『性别』:男『发送消息
  • 个人说明:.
    .  
      世间是一个大苦海。 
      人在海中。 
      肉身是船。 
      魂儿是船里的人。 
      船载着人,一直向彼岸行驶。
      ...
    .
  • 详细信息『加为好友』
学习动态
最新留言
友情链接

[2009-01-13 14:28] [转]drawRegion

public void drawRegion(Image src,
                       int x_src,
                       int y_src,
                       int width,
                       int height,
                       int transform,
                       int x_dest,
                       int y_dest,
                       int anchor)
Copies a region of the specified source image to a location within the destination, possibly transforming (rotating and reflecting) the image data using the chosen transform function. 
用来反转图片用,不过听说经常会出现问题

下面的例子出处:http://blog.csdn.net/lyerliu/archive/2008/08/31/2857345.aspx

package hxy.DrawRegion;
import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.*;

/*
create time: 2008-8-31 Administrator
*/

public class TestDrawRegion extends MIDlet {
TestCanvas canvas;

public static Display display;

public TestDrawRegion() {
   canvas = new TestCanvas();
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
   // TODO Auto-generated method stub

}

protected void pauseApp() {
   // TODO Auto-generated method stub

}

protected void startApp() throws MIDletStateChangeException {
   // TODO Auto-generated method stub
   display.getDisplay(this).setCurrent(canvas);
}

}

class TestCanvas extends Canvas {

int[] newdata;

boolean USE_DRAW_RGB = false;

protected void paint(Graphics g) {
   g.setColor(0xffffff);
   g.fillRect(0, 0, getWidth(), getHeight());
   g.setColor(0x000000);
   g.drawString("Test System Region:", 2, 2, g.LEFT | g.TOP);
   try {
    Image img = Image.createImage(getClass().getResourceAsStream("/t27.png"));
    if (img != null)
     testDrawRegion(img, g);
   } catch (IOException e) {
    e.printStackTrace();
   }

}

// trans in WTK
// TRANS_NONE 0 //无翻转
// TRANS_MIRROR_ROT180 1 //X翻转+180度翻转 即是Y翻转
// TRANS_MIRROR 2 //X 翻转
// TRANS_ROT180 3 //顺时针180度翻转 即是XY翻转

// TRANS_MIRROR_ROT270 4 //X翻转+270度翻转
// TRANS_ROT90 5 //顺时针90度翻转
// TRANS_ROT270 6 //270度翻转
// TRANS_MIRROR_ROT90 7 //X翻转+90度翻转

public void testDrawRegion(Image img, Graphics g) {

   // 用系统方法来画图,自动实现翻转
   int trans = 0;
   int LINE_SPACE = 60;
   for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 4; j++)
     g.drawRegion(img, 0, 0, img.getWidth(),img.getHeight(), trans++, 2 + LINE_SPACE * j, 20 + LINE_SPACE * i, g.LEFT | g.TOP);
   }
  
   System.out.println("img.getWidth()="+img.getWidth());
   System.out.println("img.getHeight()="+img.getHeight());

  
   // 手动实现图片翻转
   g.drawString("use drawrgb Region:", 2, 120, g.LEFT | g.TOP);
   if (img == null)
    return;
   int width = img.getWidth();
   int height = img.getHeight();
   int[] data = new int[width * height];
   newdata = new int[width * height];
   img.getRGB(data, 0, width, 0, 0, width, height);

   trans = 0;
   for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 4; j++) {
     switch (trans) {
     case 0:
      g.drawRGB(data, 0, width, 2 + LINE_SPACE * j, 140 + LINE_SPACE * i, width, height, true);
      break;
     case 1: // Y翻转
      trans_MIRROR_ROT180(data, width, height);
      break;
     case 2: // X 翻转
      trans_MIRROR(data, width, height);
      break;
     case 3: // XY翻转
      trans_ROT180(data, width, height);
      break;
     case 4:
      trans_MIRROR_ROT270(data, width, height);
      break;
     case 5:
      trans_TRANS_ROT90(data, width, height);
      break;
     case 6:
      trans_ROT270(data, width, height);
      break;
     case 7:
      trans_MIRROR_ROT90(data, width, height);
      break;
     default:
      break;
     }

     if (USE_DRAW_RGB) {
      g.drawRGB(newdata, 0, width, 2 + LINE_SPACE * j, 140 + LINE_SPACE * i, width, height, true);
     } else {
      Image tempImg = Image.createRGBImage(newdata, width, height, true);
      g.drawImage(tempImg, 2 + LINE_SPACE * j, 140 + LINE_SPACE * i, g.LEFT | g.TOP);
     }
     trans++;
    }
   }
}

// X翻转
public void trans_MIRROR(int[] data0, int width, int height) {

   for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
     int i = row * width; // 该行第一个像素在数据中的偏移量
     newdata[i + col] = data0[(i + width) - col - 1]; // 本行前面的像素和后边的像素对调
    }
   }
}

// Y翻转
public void trans_MIRROR_ROT180(int[] data0, int width, int height) {
   for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
     int i, j;
     i = row * width; // 要调换行的第一个像素 偏移量
     j = (height - row - 1) * width; // 对应掉换行的行第一个像素 偏移量

     newdata[i + col] = data0[j + col];
    }
   }
}

// XY翻转
public void trans_ROT180(int[] data0, int width, int height) {
   for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
     int i, j;
     i = row * width;
     j = (height - row - 1) * width;

     newdata[i + col] = data0[(j + width) - col - 1];
    }
   }
}

public void trans_MIRROR_ROT270(int[] data0, int width, int height) {
   for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
     int i, j;
     i = col * height;
     j = row * width;

     newdata[i + row] = data0[j + col];
    }
   }
}

public void trans_TRANS_ROT90(int[] data0, int width, int height) {
   for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
     int i, j;
     i = col * height;
     j = row * width;

     newdata[i + height - row - 1] = data0[j + col];
    }
   }
}

public void trans_ROT270(int[] data0, int width, int height) {
   for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
     int i, j;
     i = height * (width - 1 - col);
     j = row * width;
     newdata[i + row] = data0[j + col];
    }
   }
}

public void trans_MIRROR_ROT90(int[] data0, int width, int height) {
   for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
     int i, j;
     i = height * (width - 1 - col);
     j = row * width;
     newdata[i + height - 1 - row] = data0[j + col];
    }
   }
}
}
评论次数(0)  |  浏览次数(562)  |  类型(JAVA) |  收藏此文  | 
 
 请输入验证码  (提示:点击验证码输入框,以获取验证码