Author: zookeeper Source: www.j2mefans.com
We have discussed how to separate a small image from an image that stores the same type of images used in all games in different States, and all the small images of the same type are stored in an image array to form an animation frame. Here we will use these two methods for a demonstration.
Let's take a look at it first!
We can see that there are two color numbers in the image. The preceding method is used to separate the digital animation.
What we need to know about this image is the size of each small image. The size of the entire image is 100*30 (length * width), and the size of each small number is 10*15 (length * width ).
The following is the source code, with comments added:
Import javax. microedition. MIDlet .*;
Import javax. microedition. lcdui .*;
Import java. Io. ioexception;
/**
* MIDlet derived class
*/
Public class spritetest extends MIDlet implements commandlistener, runnable
{
Private Static int num_frame_width = 10;
Private Static int num_frame_height = 15;
Private mycanvas; // screen painting class
Private command quit;
Private image [] numframes; // Stores image Arrays
Private Boolean running;
Private int currentframe; // record the current frame of the image.
/**
* Custom Image Layout to draw images.
*/
Class mycanvas extends canvas
{
/**
* Draw the current frame of the screen.
*/
Protected void paint (Graphics graphics)
{
// Clear screen
Graphics. setcolor (0 );
Graphics. fillrect (0, 0, getwidth (), getheight ());
// Draw the current image frame
Graphics. drawimage (numframes [currentframe], getwidth ()/2,
Getheight ()/2,
Graphics. hcenter | graphics. vcenter );
}
}
/**
* Extract each number from the number.png image and play it to the screen in sequence.
*/
Public spritetest ()
{
Try
{
// Load the large image and extract each number
Image numimage = image. createimage ("/number.png ");
Numframes = extractframes (numimage, 0,
0, 10, 2, num_frame_width,
Num_frame_height );
}
Catch (ioexception IOE)
{
System. Out. println ("unable to load image ");
}
// Construct a canvas
Mycanvas = new mycanvas ();
// And a way to quit
Quit = new command ("quit", command. Exit, 2 );
Mycanvas. addcommand (quit );
Mycanvas. setcommandlistener (this );
// Create the thread that will carry out of the animation.
Running = true;
Thread t = new thread (this );
T. Start ();
}
/** This is the main function we use. For his explanation, refer to the address below.
* Http://www.j2mefans.com/blog/trackback.asp? Tbid = 87
*
* Extracts a portion of an image using clipping.
* @ Param source the source image.
* @ Param x the starting X position of the clipping rectangle.
* @ Param y the starting y position of the clipping rectangle.
* @ Param width the width of the clipping rectangle.
* @ Param height the height of the clipping rectangle.
* @ Return a new image object containing only the portion of the image
* Withing the X, Y, width, height rectangle.
*/
Public Final Static Image getimageregion (image source, int X, int y, int width, int height)
{
// Create a placeholder for our resulting image region
Image result = image. createimage (width, height );
If (x + width> source. getwidth () | Y + height> source. getheight ())
System. Out. println ("Warning: Attempting extract using (" +
X + "," + Y + "," + width + "," + height + ") When image is" +
"(" + Source. getwidth () + "," + source. getheight () + ")");
// Draw the image, offset by the region starting position
Result. getgraphics (). drawimage (source,-X,-y, graphics. Top | graphics. Left );
Return result;
}
/** This is the main function we use. For his explanation, refer to the address below.
* Http://www.j2mefans.com/blog/trackback.asp? Tbid = 87
*
* Gets an array of images by breaking a larger image into smaller frames.
* @ Param sourceimage the image to extract the frames from.
* @ Param sourcex the starting X position in the source image to use.
* @ Param sourcey the starting y position in the source image to use.
* @ Param frameswide the number of frames should ss the source image to extract.
* @ Param frameshigh the number of frames down the source image to extract.
* @ Param framewidth the width of each of those frames.
* @ Param frameheight the height of each of those frames.
* @ Return an array containing an image for each frame.
*/
Public final static image [] extractframes (image sourceimage, int sourcex,
Int sourcey,
Int frameswide, int frameshigh,
Int framewidth, int frameheight)
{
// Extract all the frames from the source Image
Image [] frames = new image [frameswide * frameshigh];
Int framecount = 0;
For (int fy = 0; FY <frameshigh; FY ++)
For (int fx = 0; FX <frameswide; FX ++)
Frames [framecount ++] =
Getimageregion (sourceimage, sourcex + (FX * framewidth ),
Sourcey + (FY * frameheight ),
Framewidth, frameheight );
Return frames;
}
Public void run ()
{
While (running)
{
// Update the current frame of the image
Currentframe ++;
If (currentframe> 19)
Currentframe = 0;
// Request the canvas to be re-drawn.
Mycanvas. repaint ();
Try
{
// Hang around a bit.
Thread. Sleep (100 );
}
Catch (interruptedexception E)
{
}
}
}
/**
* Handles Application Manager notification the MIDlet is starting (or
* Resuning from a pause). In this case we set the canvas as the current
* Display screen.
* @ Throws midletstatechangeexception
*/
Protected void Startapp () throws midletstatechangeexception
{
Display. getdisplay (this). setcurrent (mycanvas );
}
/**
* Handles Application Manager notification the MIDlet is about to be paused.
* We don't bother doing anything for this case.
*/
Protected void pauseapp ()
{
}
/**
* Handles Application Manager notification the MIDlet is about to be
* Destroyed. We don't bother doing anything for this case.
*/
Protected void destroyapp (Boolean unconditional)
Throws midletstatechangeexception
{
}
/**
* The commandlistener interface method called when the user executes
* A command, in this case it can only be the quit command we created in
* Consutructor and added to the canvas.
* @ Param command the command that was executed.
* @ Param displayable the displayable that command was embedded.
*/
Public void commandaction (command, displayable)
{
Try
{
If (command = quit)
{
Running = false;
Destroyapp (true );
Yydestroyed ();
}
}
Catch (midletstatechangeexception me)
{
System. Out. println (Me + "caught .");
}
}
}