imageset.java~1~

来自「基于J2ME的手机游戏软件。可以控制游戏人物在地图上上下左右行走;可以在地图上放」· JAVA~1~ 代码 · 共 131 行

JAVA~1~
131
字号
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import java.io.IOException;

public final class ImageSet
{
	private int totalStates;//总的状态数
	private Image[][] stateFrames;//状态帧集合
	private int[] stateAnimTime;//每个状态动画的过场时间
	private int[] stateFrameWidth;//每个状态帧的帧宽
	private int[] stateFrameHeight;//每个状态帧的帧高
	//error: 0001 裁剪的图片尺寸太大,超过原图大小
	//error: 0002 帧图片不存在
	
	public ImageSet(int numState)//构造器:初始化构造的状态个数
	{
		stateAnimTime = new int[numState];
		stateFrameWidth = new int[numState];
		stateFrameHeight = new int[numState];
		stateFrames = new Image[numState][];
	}
	
	public int addState(Image[] frames,int animTime)//增加状态
	{
		int state = ++totalStates;
		if(state > stateFrames.length)
		{
			stateAnimTime = Tool.expandArray(stateAnimTime,1);
			stateFrameWidth = Tool.expandArray(stateFrameWidth,1);
			stateFrameHeight = Tool.expandArray(stateFrameHeight,1);
			stateFrames = Tool.expandArray(stateFrames,1);
		}
		stateAnimTime[state - 1] = animTime;
		stateFrameWidth[state - 1] = frames[0].getWidth();
		stateFrameHeight[state - 1] = frames[0].getHeight();
		stateFrames[state - 1] = frames;
		return state - 1;
	}
	
	public int getTotalFrames(int state)
	{
		return stateFrames[state].length;
	}
	
	public int getTotalStates()
	{
		return totalStates;
	}
	
	public int getAnimTime(int state)
	{
		return stateAnimTime[state];
	}
	
	public int getAnimTimePerFrame(int state)
	{
		return stateAnimTime[state] / stateFrames[state].length;
	}
	
	public void draw(Graphics target,int state,int frame,int targetX,int targetY,int anchor)
	{
		if(stateFrames[state][frame] != null)
		{
			target.drawImage(stateFrames[state][frame],
						targetX,targetY,anchor);
		}
		else 
		{
			System.out.println("error;0002");
		}
	}
	
	public Image getFrame(int state,int frame)
	{
		return stateFrames[state][frame];
	}
	
	public static Image getImageRegion(Image source,int x,int y,int width,int height)
	//获得子图形文件
	{
	//	Image result = Image.createImage(width,height);
		if(x + width > source.getWidth() || y + height > source.getHeight())
		{
			System.out.println("error:0001");
			System.out.println(x + width);
			System.out.println(y + height);
		}
		
		
		int[] rgb = new int[width * height];
	    source.getRGB(rgb,0,width,x,y,width,height);
	    Image result = Image.createRGBImage(rgb,width,height,true);
        		
	// 	result.getGraphics().drawImage(source,-x,-y,Graphics.TOP|Graphics.LEFT);
		return result;
	}
	
	public static Image[] extractFrames(Image source,//源图像文件
										int sourceX,int sourceY,//分割帧的起始点
										int framesWide,int framesHigh,//帧的个数
										int frameWidth,int frameHeight)//单帧长和宽							
	{
		Image[] result = new Image[framesWide * framesHigh];
		int frameCount = 0;
		for(int y = 0;y < framesHigh;y++)
			for(int x = 0;x < framesWide;x++)
			{
				result[frameCount++] = getImageRegion(source,sourceX + (x * frameWidth),
															sourceY + (y * frameHeight),
															frameWidth,frameHeight);
			}
		return result;
	}
	
	public static Image loadClippedImage(String fileName,
											int posX,int posY,
											int width,int height)
	{
		try
		{
			Image fileImage = Image.createImage(fileName);
			return getImageRegion(fileImage,posX,posY,width,height);
		}
		catch(IOException ioe)
		{
			System.out.println("error:0005");
			
		}
		return null;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?