This article describes how to split a larger picture into an image array while the program is running. The most important thing to achieve this is to use the graphics coordinate transformation and the ability to draw pictures. That is, using the translate () and DrawImage () methods.
We design a Imageutil class, as follows:
/**
* Copyright_2006, eric zhan
* Created on 2006-6-8
*/
package com.j2medev.image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class ImageUtil {
public static Image[] splitImage(Image img, int rows, int cols) {
if(img == null)
return null;
Image[] result = new Image[rows * cols];
int w = img.getWidth()/cols;
int h = img.getHeight()/rows;
for(int i = 0;i<result.length;i++){
result[i] = Image.createImage(w,h);
Graphics g = result[i].getGraphics();
g.translate((-i%cols)*w,(-i/cols)*h);
g.drawImage(img,0,0,Graphics.LEFT|Graphics.TOP);
}
return result;
}
}
The static method Splitimage (image Img,int rows,int cols) Splits the image object specified by the parameter img into an image array of rows cols columns. Here we store the array as a one-dimensional array, and of course you can store it as a two-dimensional array. Depending on the height and width of the IMG and the number of rows and columns in the destination array, you can calculate the height and width of each small picture, and the height and width of each picture should be equal. The translate (int x,int y) method of graphics can transform the origin of the graphics context to (x,y) so that subsequent drawing actions are based on the new origin. Then we call DrawImage () to put some of the IMG content in the new image in the painting. After the loop ends, return the image array.
The following is a test midlet to see the effect, the code is as follows:
/**
* copyright_2006, Eric Zhan
* Created on 2006-6-8
*/
Package com.j2medev.image;
Import ja Va.io.IOException;
Import Javax.microedition.lcdui.Canvas;
Import Javax.microedition.lcdui.Display;
Import Javax.microedition.lcdui.Form;
Import Javax.microedition.lcdui.Graphics;
Import Javax.microedition.lcdui.Image;
Import Javax.microedition.midlet.MIDlet;
Import javax.microedition.midlet.MIDletStateChangeException;
public class Imagemidlet extends MIDlet {
protected void Destroyapp (Boolean arg0) throws Midletstatechangeexcepti on {
}
protected void Pauseapp () {
//TODO auto-generated method stub
}
protected void startApp () thr oWS midletstatechangeexception {
//TODO auto-generated stub
Display display = Display.getdisplay (this); br> Image img = null;
try {
img = image.createimage ("/test.png");
The catch (IOException ex) {
Form form = new Form ("error");
Form.append ("ERror to load the IMG);
Display.setcurrent (form);
return;
}
Display.setcurrent (New Imagecanvas (IMG));
}
}
Class Imagecanvas extends Canvas {
Private Image img = null;
Public Imagecanvas (Image _img) {
This . img = _img;
}
public void Paint (Graphics g) {
int color = G.getcolor ();
G.setcolor (0xFFFFFF);
G.fillrect (0, 0, getwidth (), getheight ());
G.setcolor (color);
if (img!= null) {
//before split
G.drawimage (IMG, 0, 0, graphics.left | Graphics.top);
//after
int distance = Img.getheight () + 6;
image[] Sprites = Imageutil.splitimage (IMG, 3, 4);
if (sprites!= null) {
for (int i = 0; i < 3; i++) {
for (int j = 0; J < 4; J +) {
G.drawim Age (Sprites[4 * i + j], J
* sprites[4 * i + j].getwidth () +2*j, distance
+ i * sprites[4 * i + j].getheight () +2*i,
Graphics.left | Graphics.top);
}
}
}
}
}
}
Prepare a picture test.png, you can run. We suggest that the height of the picture should be proportional to the number of rows, and the width of the picture is proportional to the number of columns. To show the effect of segmentation, add a little space between the small pictures.