📄 imageset.java
字号:
/*
* ImageSet.java
*
* Created on 2005年12月13日, 上午10:27
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import java.io.IOException;
/**
*
* @author YBGame_xupan
*/
public class ImageSet {
//private int totalStates;
// private Image[][] stateFrames;
//private int[] stateAnimTime,stateFrameWidth,stateFrameHeight;
/*
*
// Creates a new instance of ImageSet
public ImageSet(int numStates) {
stateAnimTime=new int[numStates];
stateFrameWidth=new int[numStates];
stateFrameHeight=new int[numStates];
stateFrames=new Image[numStates][];
}
//增加精灵
public final void addState(Image frames[],int animTime){
int state=totalStates++;
if (state>=stateFrames.length){
stateAnimTime=Tools.expandArray(stateAnimTime, 1);
stateFrameWidth=Tools.expandArray(stateFrameWidth, 1);
stateFrames=Tools.expandArray(stateFrames, 1);
}
stateAnimTime[state]=animTime;
stateFrameWidth[state]=frames[0].getWidth();
stateFrameHeight[state]=frames[0].getHeight();
stateFrames[state]=frames;
}
public final int getTotalFrames(int state){
return stateFrames[state].length;
}
public final int getAnimTime(int state){
return stateAnimTime[state];
}
public final int getAnimTimePerFrame(int state){
return stateAnimTime[state]/stateFrames[state].length;
}
public final void draw(Graphics target,int state,int frame,int targetX,int targetY){
if (stateFrames[state][frame]!=null){
target.drawImage(stateFrames[state][frame], targetX, targetY, Tools.GRAPHCS_TOP_LEFT);
}
}
public final Image getFrame(int state,int frame){
return stateFrames[state][frame];
}
public final static Image loadClippedImage(String filename,int originX,int originY,int width,int height){
try{
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{
Image fileImage = Image.createImage(filename);
if (originX==0 && originY==0) return fileImage;
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){
Image result=Image.createImage(width,height);
if (x+width>source.getWidth() || y+height>source.getHeight()){
System.out.println("error 111");
}
result.getGraphics().drawImage(source, -x,-y, Tools.GRAPHCS_TOP_LEFT);
return result;
}
public final static Image[] extractFrames(Image sourceImage,int sourceX,int sourceY,int framesWide,int framesHigh,int frameWidth,int frameHeight){
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -