Transform images in j2me CLDC 1.0 (No sprites)
Rotate and mirror images in j2me with this code Transform
public class ImageUtil {
/**
* No need to use javax.microedition.lcdui.game.Sprite
*/
public final static int TRANS_MIRROR = 2;
public final static int TRANS_NONE = 0;
public final static int TRANS_ROT90 = 5;
public final static int TRANS_ROT180 = 3;
public final static int TRANS_ROT270 = 6;
public final static int TRANS_MIRROR_ROT90 = 7;
public final static int TRANS_MIRROR_ROT180 = 1;
public final static int TRANS_MIRROR_ROT270 = 4;
public static final Image transformImage(Image srcImage, int transform) {
Image result = null;
int srcW = srcImage.getWidth();
int srcH = srcImage.getHeight();
Graphics g = null;
switch (transform) {
//No Translate/rotate
case TRANS_NONE:
result = Image.createImage(srcImage);
break;
//Rotate Mirror
case TRANS_MIRROR:
result = Image.createImage(srcW, srcH);
g = result.getGraphics();
for (int i = 0; i < srcW; i++) {
g.setClip(i, 0, 1, srcH);
g.drawImage(srcImage, i * 2 - srcW, 0, Graphics.TOP
| Graphics.LEFT);
}
break;
//Rotate 90 degreescase TRANS_ROT90:
result = Image.createImage(srcH, srcW);
g = result.getGraphics();
for (int i = 0; i < srcH; i++) {
for (int j = 0; j < srcW; j++) {
g.setClip(i, j, 1, 1);
g.drawImage(srcImage, i - j, i + j - srcH, Graphics.TOP
| Graphics.LEFT);
}
}
break;
//Mirror Rotate 270 degreescase TRANS_MIRROR_ROT270:
result = Image.createImage(srcH, srcW);
g = result.getGraphics();
for (int i = 0; i < srcH; i++) {
for (int j = 0; j < srcW; j++) {
g.setClip(i, j, 1, 1);
g.drawImage(srcImage, i - j, j - i, Graphics.TOP
| Graphics.LEFT);
}
}
break;
//180 degrees
case TRANS_ROT180:
result=transformImage(srcImage,TRANS_MIRROR_ROT180);
result=transformImage(result,TRANS_MIRROR);
break;
//180 degrees
case TRANS_MIRROR_ROT180:
result = Image.createImage(srcW, srcH);
g = result.getGraphics();
for (int i = 0; i < srcH; i++) {
g.setClip(0, i, srcW, 1);
g.drawImage(srcImage, 0, 2*i-srcH, Graphics.TOP
| Graphics.LEFT);
}
break;
//mirror rotated 90 degreescase TRANS_MIRROR_ROT90:
result=transformImage(srcImage,TRANS_ROT90);
result=transformImage(result,TRANS_MIRROR_ROT180);
break;
//TRANS_MIRROR_ROT270
case TRANS_ROT270:
result=transformImage(srcImage,TRANS_MIRROR_ROT270);
result=transformImage(result,TRANS_MIRROR_ROT180);
break;
}
return result;
}
}
public class ImageUtil {
/**
* No need to use javax.microedition.lcdui.game.Sprite
*/
public final static int TRANS_MIRROR = 2;
public final static int TRANS_NONE = 0;
public final static int TRANS_ROT90 = 5;
public final static int TRANS_ROT180 = 3;
public final static int TRANS_ROT270 = 6;
public final static int TRANS_MIRROR_ROT90 = 7;
public final static int TRANS_MIRROR_ROT180 = 1;
public final static int TRANS_MIRROR_ROT270 = 4;
public static final Image transformImage(Image srcImage, int transform) {
Image result = null;
int srcW = srcImage.getWidth();
int srcH = srcImage.getHeight();
Graphics g = null;
switch (transform) {
//No Translate/rotate
case TRANS_NONE:
result = Image.createImage(srcImage);
break;
//Rotate Mirror
case TRANS_MIRROR:
result = Image.createImage(srcW, srcH);
g = result.getGraphics();
for (int i = 0; i < srcW; i++) {
g.setClip(i, 0, 1, srcH);
g.drawImage(srcImage, i * 2 - srcW, 0, Graphics.TOP
| Graphics.LEFT);
}
break;
//Rotate 90 degreescase TRANS_ROT90:
result = Image.createImage(srcH, srcW);
g = result.getGraphics();
for (int i = 0; i < srcH; i++) {
for (int j = 0; j < srcW; j++) {
g.setClip(i, j, 1, 1);
g.drawImage(srcImage, i - j, i + j - srcH, Graphics.TOP
| Graphics.LEFT);
}
}
break;
//Mirror Rotate 270 degreescase TRANS_MIRROR_ROT270:
result = Image.createImage(srcH, srcW);
g = result.getGraphics();
for (int i = 0; i < srcH; i++) {
for (int j = 0; j < srcW; j++) {
g.setClip(i, j, 1, 1);
g.drawImage(srcImage, i - j, j - i, Graphics.TOP
| Graphics.LEFT);
}
}
break;
//180 degrees
case TRANS_ROT180:
result=transformImage(srcImage,TRANS_MIRROR_ROT180);
result=transformImage(result,TRANS_MIRROR);
break;
//180 degrees
case TRANS_MIRROR_ROT180:
result = Image.createImage(srcW, srcH);
g = result.getGraphics();
for (int i = 0; i < srcH; i++) {
g.setClip(0, i, srcW, 1);
g.drawImage(srcImage, 0, 2*i-srcH, Graphics.TOP
| Graphics.LEFT);
}
break;
//mirror rotated 90 degreescase TRANS_MIRROR_ROT90:
result=transformImage(srcImage,TRANS_ROT90);
result=transformImage(result,TRANS_MIRROR_ROT180);
break;
//TRANS_MIRROR_ROT270
case TRANS_ROT270:
result=transformImage(srcImage,TRANS_MIRROR_ROT270);
result=transformImage(result,TRANS_MIRROR_ROT180);
break;
}
return result;
}
}
Comments