📄 mysprite.java
字号:
package demo;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class MySprite extends MyLayer{
public static final int TRANS_NONE = 0; //不转换
public static final int TRANS_MIRROR = 1; //镜像翻转
public static final int TRANS_TYPE_NUM = 2; //转换类型总数
protected int m_nTransform = TRANS_NONE; //转换类型
protected int m_nFrame; //当前的帧编号
protected int m_nFrameCols; //每行的帧数
protected int m_nFrameRows; //每列的帧数
protected int m_nRefX; //参考点的X坐标
protected int m_nRefY; //参考点的Y坐标
public MySprite(Image image, int frameWidth, int frameHeight){
super();
if( image == null ){
System.out.print("(MySprite)image is null!");
return;
}
if( image.getWidth() % frameWidth != 0 ){
System.out.print("(MySprite)frameWidth is wrong!");
return;
}
if( image.getHeight() % frameHeight != 0 ){
System.out.print("(MySprite)frameHeight is wrong!");
return;
}
m_Image = image;
m_nWidth = frameWidth;
m_nHeight = frameHeight;
m_bVisible = true;
m_nFrame = 0;
m_nFrameCols = image.getWidth()/m_nWidth;
m_nFrameRows = image.getHeight()/m_nHeight;
m_nRefX = frameWidth / 2;
m_nRefY = frameHeight / 2;
m_nTransform = TRANS_NONE;
}
//设置当前的帧编号
public void setFrame(int sequenceIndex){
if( sequenceIndex < 0 || sequenceIndex >= m_nFrameCols * m_nFrameRows ){
System.out.print("(setFrame)sequenceIndex is wrong!");
return;
}
m_nFrame = sequenceIndex;
}
//获取当前的帧编号
public int getFrame(){
return m_nFrame;
}
//获取帧的总数
public int getFrameSequenceLength(){
return m_nFrameCols * m_nFrameRows;
}
//获取参考点的X坐标
public int getRefPixelX(){
return m_nRefX;
}
//获取参考点的Y坐标
public int getRefPixelY(){
return m_nRefY;
}
//设置参考点
public void defineReferencePixel(int x, int y) {
m_nRefX = x;
m_nRefY = y;
}
//通过参考点,设置精灵位置
public void setRefPixelPosition(int x, int y) {
setPosition(x - m_nRefX, y - m_nRefY);
}
//设置精灵图像的转换
public void setTansform(int transform){
if( transform < 0 || transform >= TRANS_TYPE_NUM ){
System.out.print("(setTansform)transform is wrong!");
return;
}
m_nTransform = transform;
}
//显示精灵图像
public void paint(Graphics g){
if( !m_bVisible )
return;
//保存原有的切割区域
int clipx = g.getClipX();
int clipy = g.getClipY();
int clipw = g.getClipWidth();
int cliph = g.getClipHeight();
//设置图片的绘制位置
int x = m_nX - (m_nFrame % m_nFrameCols) * m_nWidth;
int y = m_nY - (m_nFrame / m_nFrameCols) * m_nHeight;
//根据转换类型,显示图像
switch( m_nTransform ){
case TRANS_NONE:
g.setClip(m_nX, m_nY, m_nWidth, m_nHeight);
g.drawImage(m_Image, x, y, 0);
break;
case TRANS_MIRROR:
for( int i = 0; i < m_nWidth; i ++ ){
//设置切割区域是1列
g.setClip(m_nX + i, m_nY, 1, m_nHeight);
//绘制图片
g.drawImage(m_Image, x - ( 2 * (m_nRefX-i) ), y, 0);
}
break;
}
//恢复切割区域
g.setClip(clipx, clipy, clipw, cliph);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -