📄 imageset.java
字号:
/**
* A container for sets of image frames; typically sprites. A single set
* is made up of one or more states. Each state represents an animation sequence
* including Image objects for the frames, animation timing and frame dimensions.
* <p>
* An example use of this class would be to animate a little dude. If he had two
* states of existence, standing (which has short breathing animation) and
* walking (which has a much longer animation). This would be implemented by
* creating an ImageSet object and then adding two states, each with their own
* Image array for all the animation frames (use the static methods at the end
* of this class to load a clipped file image and then extract the image frame
* array from it). You can then use a Sprite class associated with this ImageSet
* to draw the character to the screen as well as keep track of animation frames.
* <p>
* This version expands on the original ImageSet concept by adding an option for
* the images in a state to be reflected dynamically using the Nokia UI. This
* saves image memory space becuase you no longer need to cache reflected images
* you can render them on the fly.
* <p>
* This version also adds an option for a very simple state that has only a
* single image frame. This means you can do without using an array for all the
* image access (something done very often). Array access is a lot slower than
* a single image.
* <p>
* @see Sprite
*/
//#ifdef nokia
import com.nokia.mid.ui.DirectGraphics;
import com.nokia.mid.ui.DirectUtils;
//#endif
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import java.io.IOException;
public class ImageSet
{
private int totalStates; // incremented by addState method only
// can either use multiple frames for each state or single (much faster)
private Image[][] stateFramesMulti;
private Image[] stateFramesSingle;
// reflection types are based on nokia UI.
private int[] stateAnimTime, stateReflectionOf, stateReflectionType;
private boolean singleImage;
public static final int FLIP_HORIZONTAL = 8192;
public static final int FLIP_VERTICAL = 16384;
public ImageSet(int numStates, boolean isSingleImage)
{
singleImage = isSingleImage;
stateAnimTime = new int[numStates];
stateReflectionOf = new int[numStates];
stateReflectionType = new int[numStates];
if (singleImage)
stateFramesSingle = new Image[numStates];
else
stateFramesMulti = new Image[numStates][];
}
/**
* Quick version of the addState method that takes an image arrray and anim
* time.
* @param frames The image frames.
* @param animTime The time to animate through all those images.
* @return The number of the new state.
*/
public final int addState(Image frames[], int animTime)
{
return addState(frames, animTime, 0, 0);
}
/**
* Adds a new state with which is a reflection of another one. Whenever
* the images for this state are rendered the images from to reflectionOf
* state will be drawn instead using the reflection specified in the
* reflectionType parameter.
* @param frames The images for the state, or null if reflectionOf is set.
* @param animTime The animation time for this state.
* @param reflectionOf The number of another state in this ImageSet to
* create a reflection of.
* @param reflectionType The type of reflection (FLIP_HORIZONTAL |
* FLIP_VERTICAL)
* @return The number of the new state.
*/
public final int addState(Image frames[], int animTime, int reflectionOf,
int reflectionType)
{
int state = totalStates++;
if (state >= stateAnimTime.length)
expandArrays();
stateAnimTime[state] = animTime;
stateFramesMulti[state] = frames;
stateReflectionOf[state] = reflectionOf;
stateReflectionType[state] = reflectionType;
return state;
}
/**
* Adds a new state with which is a reflection of another one using
* a single image source (for speed).
* @param reflectionOf The number of another state in this ImageSet to
* create a reflection of.
* @param reflectionType The type of reflection (FLIP_HORIZONTAL |
* FLIP_VERTICAL)
* @return The number of the new state.
*/
public final int addState(Image frame, int reflectionOf, int reflectionType)
{
int state = totalStates++;
if (state >= stateAnimTime.length)
expandArrays();
stateReflectionOf[state] = reflectionOf;
if (reflectionOf == -1)
stateFramesSingle[state] = frame;
else
stateReflectionType[state] = reflectionType;
return state;
}
public void expandArrays()
{
// expand the number of states
if (singleImage)
stateFramesSingle = expandArray(stateFramesSingle, 1);
else
{
stateFramesMulti = expandArray(stateFramesMulti, 1);
stateAnimTime = expandArray(stateAnimTime, 1);
}
stateReflectionOf = expandArray(stateReflectionOf, 1);
stateReflectionType = expandArray(stateReflectionType, 1);
}
// Enahcned versions of expand array copied in from a tools class.
public final static int[] expandArray(int[] oldArray, int expandBy)
{
int[] newArray = new int[oldArray.length + expandBy];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
return newArray;
}
public final static Image[] expandArray(Image[] oldArray, int expandBy)
{
Image[] newArray = new Image[oldArray.length + expandBy];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
return newArray;
}
public final static Image[][] expandArray(Image[][] oldArray, int expandBy)
{
Image[][] newArray = new Image[oldArray.length + expandBy][];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
return newArray;
}
public final int getTotalFrames(int state)
{
//#ifdef debug
if (state >= totalStates)
System.out.println("oops, trying to get state: " + state + " when totalStates = " + totalStates);
//#endif
if (singleImage)
return 1;
else
return stateFramesMulti[state].length;
}
public final int getAnimTime(int state)
{
return stateAnimTime[state];
}
public final int getAnimTimePerFrame(int state)
{
if (singleImage) return 0;
return stateAnimTime[state] / stateFramesMulti[state].length;
}
/**
* Draw a specific frame of this sprite onto a graphics object
*/
public final void draw(Graphics target, int state, int frame, int targetX, int targetY)
{
//#ifdef debug
if (state >= totalStates)
System.out.println("oops, bad state " + state + " total states = " + totalStates);
if (!singleImage && frame >= stateFramesMulti[state].length)
System.out.println("oops, bad frame " + frame + " in draw(" +
frame + ", " + state + ")");
//#endif
if (singleImage)
{
if (stateReflectionOf[state] != -1)
{
drawReflectedImage(target, stateFramesSingle[stateReflectionOf[state]],
targetX, targetY,
stateReflectionType[state]);
}
else
{
target.drawImage(stateFramesSingle[state],
targetX, targetY, Tools.GRAPHICS_TOP_LEFT);
}
}
else
{
// note: reflections on multi frame states not supported (yet)
target.drawImage(stateFramesMulti[state][frame],
targetX, targetY, Tools.GRAPHICS_TOP_LEFT);
}
}
/**
* get a specific frame
*/
public final Image getFrame(int state, int frame)
{
//#ifdef debug
if (state >= totalStates)
System.out.println("oops, bad state " + state);
if (singleImage && frame >= stateFramesMulti[state].length)
System.out.println("oops, bad frame " + state + ", " + frame + " in getFrame()");
//#endif
if (singleImage)
return stateFramesSingle[state];
else
return stateFramesMulti[state][frame];
}
//
// STATIC IMAGE TOOLS
//
public final static Image loadClippedImage(String filename, int originX, int originY, int width,
int height)
{
try
{
// load full image from file and create a mutable version
Image fileImage = Image.createImage(filename);
return getImageRegion(fileImage, originX, originY, width, height);
}
catch (IOException ioe)
{
System.out.println("can't load file: " + filename);
return null;
}
}
public final static Image loadClippedImage(String filename, int originX, int originY)
{
try
{
// load full image from file and create a mutable version
Image fileImage = Image.createImage(filename);
return getImageRegion(fileImage, originX, originY, fileImage.getWidth(), fileImage.getHeight());
}
catch (IOException ioe)
{
System.out.println("can't load file: " + filename);
return null;
}
}
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 = null;
//#ifdef nokia
result = DirectUtils.createImage(width, height, 0x00000000);
//#else
result = Image.createImage(width, height);
//#endif
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() + ")");
// set the clipping region so we ignore the portion of the source image to
// the right and bottom of the region we want
result.getGraphics().setClip(0, 0, width, height);
// draw the image, offset by the region starting position
result.getGraphics().drawImage(source, -x, -y, Tools.GRAPHICS_TOP_LEFT);
return result;
}
public final static Image[] extractFrames(Image sourceImage, int sourceX, int sourceY,
int framesWide, int framesHigh,
int frameWidth, int frameHeight)
{
// auto-detect the frame width if it wasn't supplied (based on total image
// size.
if (frameWidth == 0) frameWidth = sourceImage.getWidth() / framesWide;
if (frameHeight == 0) frameHeight = sourceImage.getHeight() / framesHigh;
// 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 final static void drawReflectedImage(Graphics g, Image source, int x, int y,
int flipType)
{
DirectGraphics rdg = DirectUtils.getDirectGraphics(g);
rdg.drawImage(source, x, y, Tools.GRAPHICS_TOP_LEFT, flipType);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -