?? imagex.java
字號:
package com.j2medev.chapter5;
import javax.microedition.lcdui.*;
/**
* Imagex是對標準Image的一個擴展,可以操作包含多幀的單張圖片,可以指定幀序列
*/
public class Imagex {
// 下列常量的含義跟Graphics的相應常量相同,重新定義是為了良好的applet移植性
public static final int HCENTER = 1;
public static final int VCENTER = 2;
public static final int LEFT = 4;
public static final int RIGHT = 8;
public static final int TOP = 16;
public static final int BOTTOM = 32;
private static final int ANCHOR = LEFT | TOP;
/** 源圖片的引用 */
public Image src;
/** 每一幀的寬、高 */
public int w, h;
/** 當前幀序列映射表 */
public int[] seqMap;
/** 當前幀序列長度 */
public int seqLen;
/** 當前幀 */
public int curFrame;
protected int[] rawLocx;
protected int[] rawLocy;
protected int anchor = ANCHOR;
protected int offsetx = 0;
protected int offsety = 0;
protected Imagex() {}
/**
* 創建一個新的Imagex
* 按照給定的行數和列數切分源圖片
* @param src
* @param columns
* @param rows
*/
public Imagex(Image src, int columns, int rows) {
setImage(src, columns, rows);
}
public Imagex(Image src, int rx, int ry, int rw, int rh, int framew,
int frameh) {
setImage(src, rx, ry, rw, rh, framew, frameh);
}
public void setImage(Image src, int columns, int rows) {
int imw = src.getWidth();
int imh = src.getHeight();
setImage(src, 0, 0, imw, imh, imw / columns, imh / rows);
}
public void setImage(Image src, int rx, int ry, int rw, int rh, int framew,
int frameh) {
int imw = src.getWidth();
int imh = src.getHeight();
if (rx < 0 || ry < 0 || rw <= 0 || rh <= 0 || rx + rw > imw ||
ry + rh > imh ||
framew <= 0 || frameh <= 0 || rw % framew != 0 || rh % frameh != 0) {
throw new java.lang.IllegalArgumentException();
}
this.src = src;
w = framew;
h = frameh;
int horiGrids = rw / framew;
int vertGrids = rh / frameh;
int size = horiGrids * vertGrids;
int oldRawLen = rawLocx == null ? Integer.MAX_VALUE : rawLocx.length;
rawLocx = new int[size];
rawLocy = new int[size];
for (int i = vertGrids; --i >= 0; ) {
for (int j = horiGrids; --j >= 0; ) {
int idx = i * horiGrids + j;
rawLocx[idx] = rx + framew * j;
rawLocy[idx] = ry + frameh * i;
}
}
if (seqMap == null || rawLocx.length < oldRawLen) {
seqMap = null;
seqLen = size;
curFrame = 0;
}
setAnchor(anchor);
}
public int getFrame() {
return curFrame;
}
public int getFrameSequenceLength() {
return seqLen;
}
public int getRawFrameCount() {
return rawLocx.length;
}
public void nextFrame() {
curFrame++;
if (curFrame >= seqLen) {
curFrame = 0;
}
}
public void prevFrame() {
curFrame--;
if (curFrame < 0) {
curFrame = seqLen - 1;
}
}
public int getFrameWidth() {
return w;
}
public int getFrameHeight() {
return h;
}
/**
* 設定當前幀
* @param sequenceIndex
*/
public void setFrame(int sequenceIndex) {
curFrame = sequenceIndex;
}
/**
* 設定當前幀序列
* @param sequence
*/
public void setFrameSequence(int[] sequence) {
seqLen = sequence.length;
seqMap = sequence;
}
public void setAnchor(int anchor) {
this.anchor = anchor;
offsetx = 0;
offsety = 0;
if ( (anchor & HCENTER) != 0) {
offsetx = - (w >> 1);
}
else if ( (anchor & RIGHT) != 0) {
offsetx = -w;
}
if ( (anchor & VCENTER) != 0) {
offsety = - (h >> 1);
}
else if ( (anchor & BOTTOM) != 0) {
offsety = -h;
}
}
public void draw(Graphics g, int x, int y) {
drawFrame(g, curFrame, x, y);
}
public void drawFrame(Graphics g, int frame, int x, int y) {
int clipx = g.getClipX();
int clipy = g.getClipY();
int clipw = g.getClipWidth();
int cliph = g.getClipHeight();
g.clipRect(x + offsetx, y + offsety, w, h);
int idx = seqMap == null ? frame : seqMap[frame];
g.drawImage(src, x + offsetx - rawLocx[idx], y + offsety - rawLocy[idx],
ANCHOR);
g.setClip(clipx, clipy, clipw, cliph);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -