📄 animation.java
字号:
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;
import java.io.*;
//import javax.microedition.lcdui.game.Sprite;
//import com.nttdocomo.ui.Canvas;
//import java.util.*;
public class Animation extends MIDlet {
private Action m_Action = null;
public Animation() throws IOException {
m_Action = new Action();
Display.getDisplay(this).setCurrent(m_Action);
}
protected void startApp() {
//m_tGame.repaint ();
}
protected void pauseApp() {
// TODO Auto-generated method stub
System.out.println("train pause the app");
}
protected void destroyApp(boolean arg0) {
// TODO Auto-generated method stub
System.out.println("train destory the app");
}
}
class Action extends Canvas implements Runnable{
public final static int ID_WATER_PNG = 22;
public Image m_Image;
private Thread thread;
public short m_nCount; //the total count of animations
public short m_size1; //one animation data size(maybe not useful)
public short m_imageID; //the image ID of the current animation
public short m_mCount; //the total count of modules for current animation
public byte m_modules[]; //the module data(modX,modY,modW,modH)
public short m_fCount; //the total count of frames for current animation
public byte m_size2; //the size of every frame data (maybe not useful)
public byte m_flag; //(flag & 0x80) != 0,the current frame has the collide box;(flag & 0x40) != 0,the current frame has the attackBox
public byte m_num; //the number of sprites for current frame
public byte m_sprites[][]; //[frame][data] the all sprites data for current frame. [modID,flag(bit0:flipX,bit1:flipY,bit2:rotateFlag), X, Y] * num;
public byte m_colData[]; //4 bytes if((flag & 0x80) != 0) it will read 4 bytes for collide box,otherwise please do not read
public byte m_attData[]; //4 bytes if((flag & 0x40) != 0) it will read 4 bytes for attack box,otherwise please do not read
public short m_actionCount; //the total count of actions
//.......................loop 'actionCount' times
public byte m_size3; //the size of current action data
public byte m_number; //the number of frames for current action
public byte m_framesData[][]; //[action][data] the frame data for current data [frameID,duration]*number
public byte m_mechModelID; //the ID of mechModel (maybe not useful)
public short m_mechModel[]; // (size3 - 1 - number * 2 - 1)/2 the data of mechModel
// ./......................loop
public int m_actionPosX = 100,m_actionPosY = 100; //the pos of action
public byte m_curNumber; //the number of current frame
public short m_delay; //the time of delay
// static Image m_bufCanvas;
// static Graphics m_bufScreen;
public Action() throws IOException{
ReadFile();
// m_bufCanvas = Image.createImage(getWidth(),getHeight());
// m_bufScreen = m_bufCanvas.getGraphics();
m_delay = 0;
thread = new Thread(this);
thread.start();
}
public void run() {
// TODO Auto-generated method stub
for(;;)
{
try {
// s_Thread.sleep(100);
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
serviceRepaints();
}
}
protected void paint(Graphics g) {
g.setColor(0);
g.fillRect(0, 0, getWidth(), getHeight());
doAction(g,m_actionPosX,m_actionPosY);
}
public void doAction(Graphics g,int PosX,int PosY) {
short frameDelay;
System.out.println(m_curNumber);
if(m_curNumber > m_number-1)
m_curNumber = 0;
frameDelay = m_framesData[/*actionID*/0][m_curNumber*2+1];
short drawingFrame = m_framesData[0][m_curNumber*2];
if(m_delay < frameDelay){
// short drawingFrame = m_framesData[0][m_curNumber*2];
drawFrame(g, drawingFrame, PosX, PosY,false,false);
m_delay++;
}
else{
drawFrame(g, drawingFrame, PosX, PosY,false,false);
m_delay = 0;
m_curNumber ++;
}
}
public void drawFrame(Graphics g,short drawingFrame,int PosX,int PosY,boolean flipX,boolean flipY) {
int modID, flag, modX, modY, modW, modH; //module info
byte[] s = m_sprites[drawingFrame]; //sprite's info
int posX, posY, dgflag = 0; //sprite's map coordinate.\
boolean flipXs, flipYs; //final sprite flip info
System.out.println(22222);
for(int i = 0; i<s.length; i+=4)
{
modID = (s[i] & 0xff)<<2;
flag = s[i+1] & 0xff;
modX = (int)(m_modules[modID] & 0xff);
modY = (int)(m_modules[modID+1] & 0xff);
modW = (int)(m_modules[modID+2] & 0xff);
modH = (int)(m_modules[modID+3] & 0xff);
flipXs = flipX ^ ((flag & 0x01) != 0);
flipYs = flipY ^ ((flag & 0x02) != 0);
posX = PosX;
posY = PosY;
if ( flipX )
posX -= s[i + 2];
else posX += s[i + 2];
if ( flipY )
posY -= s[i + 3];
else posY += s[i + 3];
posX -= flipXs ? modW : 0;
posY -= flipYs ? modH : 0;
if (flipXs)
dgflag ^= Sprite.TRANS_MIRROR;
if (flipYs)
dgflag ^= Sprite.TRANS_MIRROR_ROT180;
if (posY > getHeight()|| posY + modH < 0
|| posX > getWidth() || posX + modW < 0)
continue;
if (posY + modH > getHeight())
g.setClip(posX, posY, modW, getHeight() - posY);
else if (posY < 0)
g.setClip(posX, 0, modW, modH + posY);
else g.setClip(posX, posY, modW, modH);
System.out.println(3333);
System.out.println("modX = " + modX);
System.out.println("modY = " + modY);
System.out.println("modW = " + modW);
System.out.println("modH = " + modH);
System.out.println("posX = " + posX);
System.out.println("posY = " + posY);
System.out.println(dgflag);
if (dgflag == 0)
g.drawImage(m_Image, posX - modX, posY - modY, Graphics.LEFT|Graphics.TOP);
else
g.drawRegion(m_Image, modX, modY, modW,
modH, dgflag, posX, posY,
Graphics.LEFT|Graphics.TOP);
}
}
private void ReadFile() throws IOException {
InputStream is = getClass().getResourceAsStream("/a.bin");
DataInputStream dis = new DataInputStream(is);
m_nCount = dis.readShort();
m_size1 = dis.readShort();
m_imageID = dis.readShort();
m_Image = Image.createImage("/water.png");
m_mCount = dis.readShort();
System.out.println("m_nCount = " + m_nCount);
System.out.println("m_size1 = " + m_size1);
System.out.println("m_imageID = " + m_imageID);
System.out.println("m_mCount = " + m_mCount);
m_modules = new byte[m_mCount*4];
for (int i = 0; i < m_mCount*4; i++) {
m_modules[i] = dis.readByte();
System.out.print("m_modules["+i+"]= " + m_modules[i]+" / ");
}
System.out.println();
m_fCount = dis.readShort();
System.out.println("m_fCount = " + m_fCount);
//the data from every frame
m_sprites = new byte[m_fCount][];//这个4表示第个frame里含一个sprite,要改;假如第个frame中的sprite不一样多,怎么办呢?
for (int i = 0; i < m_fCount; i++) {
m_size2 = dis.readByte();
m_flag = dis.readByte();
m_num = dis.readByte();
System.out.println("size2 = " + m_size2);
System.out.println("flag = " + m_flag);
System.out.println("num = " + m_num);
m_sprites[i] = new byte[m_num*4];
dis.read(m_sprites[i]);
System.out.println();
if((m_flag & 0x80) != 0){
m_colData = new byte[4];
dis.read(m_colData);
for (int j = 0; j < 4; j++) {
System.out.print("colData["+j+"] = " + m_colData[j]+" / ");
}
}//if
if((m_flag & 0x40) != 0){
m_attData = new byte[4];
dis.read(m_attData);
for (int j = 0; j < 4; j++) {
System.out.print("attData["+j+"] = " + m_attData[j]+" / ");
}
}//if
}//for-fame
//get the data from sprites[][]
for (int i = 0; i < m_fCount; i++) {
for (int j = 0; j < m_num*4; j++) {
System.out.print("sprites["+i+"]["+j+"] = " + m_sprites[i][j]+" / ");
}
System.out.println();
}
System.out.println();
m_actionCount = dis.readShort();
System.out.println("actionCount = " + m_actionCount);
m_framesData = new byte[m_actionCount][];//number*2];好郁闷,怎么申请啊?
for (int i = 0; i < m_actionCount; i++) {
m_size3 = dis.readByte();
m_number = dis.readByte();
System.out.println("size3 = " + m_size3);
System.out.println("number = " + m_number);
m_framesData[i] = new byte[m_number*2];
dis.read(m_framesData[i]);
m_mechModelID = dis.readByte();
System.out.println("mechModelID = " + m_mechModelID);
int temp = (m_size3 - 1 - m_number * 2 - 1)/2;
m_mechModel = new short[temp]; // the data of mechModel
for (int j = 0; j < temp; j++) {
m_mechModel[j] = dis.readShort();
System.out.print("mechModel["+j+"] = " + m_mechModel[j]+" / ");
}
System.out.println();
}
//get the data from framesData[i][j]
for (int i = 0; i < m_actionCount; i++) {
for (int j = 0; j < m_number*2; j++) {
System.out.print("framesData["+i+"]["+j+"] = " + m_framesData[i][j]+" / ");
}
System.out.println();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -