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];
}
}
}
}
- [nanjixing8119] 我也问和这个朋友同样的问题[ andyhl333 发表于 2011-05-26 18:55 11/24 20:56
- [andyhl333] 前言上说要学习本书就要具备以下基础: 1,具有计算机使用经验; 2,具有二进制、十六进制等基础知 05/26 18:55
- [masmaster] 学习~~~ 07/10 14:28
- [761114] 说的太好了,你叫我怎么夸你呢!这样吧,给你一句话吧。你太有才了!!!!!!! 06/09 22:51
- [hao76762401] 顶那三条原则! 05/23 19:32
- [1119174611] 一、没有通过检测点不要向下学习, 二、没有做完当前实验不要向下学习。 我觉得 二、没有做完当 05/10 00:37
- [hejianliangjia] 看到这篇文章大家很幸运,我是吃了不少苦头才悟到,不过 “一、没有通过检测点不要向下学习, 二、 05/04 01:01
- [游客] 哈哈,向自己学,汇编的书好难懂的,看不下去了 05/01 20:32
- [lenient315] 说的不错 看完王爽老师的书后看看LZ推荐的 04/21 11:27
- [qingjiaozhe2010] 太深奥了,现在还不理解 04/08 11:33