⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagex.java

📁 j2me简单实例,j2me教程加源码,希望大家喜欢
💻 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 + -