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

📄 sprite.java

📁 J2me唆哈的代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * Created on 2005-12-20 by pcy
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package javax.microedition.lcdui.game;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import com.nokia.mid.ui.DirectGraphics;
import com.nokia.mid.ui.DirectUtils;

public class Sprite extends Layer {

    /**
     * No transform is applied to the Sprite. This constant has a value of
     * <code>0</code>.
     */
    public static final int TRANS_NONE = 0;

    /**
     * Causes the Sprite to appear rotated clockwise by 90 degrees. This
     * constant has a value of <code>5</code>.
     */
    public static final int TRANS_ROT90 = 5;

    /**
     * Causes the Sprite to appear rotated clockwise by 180 degrees. This
     * constant has a value of <code>3</code>.
     */
    public static final int TRANS_ROT180 = 3;

    /**
     * Causes the Sprite to appear rotated clockwise by 270 degrees. This
     * constant has a value of <code>6</code>.
     */
    public static final int TRANS_ROT270 = 6;

    /**
     * Causes the Sprite to appear reflected about its vertical center. This
     * constant has a value of <code>2</code>.
     */
    public static final int TRANS_MIRROR = 2;

    /**
     * Causes the Sprite to appear reflected about its vertical center and then
     * rotated clockwise by 90 degrees. This constant has a value of
     * <code>7</code>.
     */
    public static final int TRANS_MIRROR_ROT90 = 7;

    /**
     * Causes the Sprite to appear reflected about its vertical center and then
     * rotated clockwise by 180 degrees. This constant has a value of
     * <code>1</code>.
     */
    public static final int TRANS_MIRROR_ROT180 = 1;

    /**
     * Causes the Sprite to appear reflected about its vertical center and then
     * rotated clockwise by 270 degrees. This constant has a value of
     * <code>4</code>.
     */
    public static final int TRANS_MIRROR_ROT270 = 4;

    // --- member variables

    /**
     * If this bit is set, it denotes that the transform causes the axes to be
     * interchanged
     */
    private static final int INVERTED_AXES = 0x4;

    /**
     * If this bit is set, it denotes that the transform causes the x axis to be
     * flipped.
     */
    private static final int X_FLIP = 0x2;

    /**
     * If this bit is set, it denotes that the transform causes the y axis to be
     * flipped.
     */
    private static final int Y_FLIP = 0x1;

    /**
     * Bit mask for channel value in ARGB pixel.
     */
    private static final int ALPHA_BITMASK = 0xff000000;

    /**
     * Source image
     */
    Image sourceImage;

    /**
     * The number of frames
     */
    int numberFrames; // = 0;

    /**
     * list of X coordinates of individual frames
     */
    int[] frameCoordsX;

    /**
     * list of Y coordinates of individual frames
     */
    int[] frameCoordsY;

    /**
     * Width of each frame in the source image
     */
    int srcFrameWidth;

    /**
     * Height of each frame in the source image
     */
    int srcFrameHeight;

    /**
     * The sequence in which to display the Sprite frames
     */
    int[] frameSequence;

    /**
     * The sequence index
     */
    private int sequenceIndex; // = 0

    /**
     * Set to true if custom sequence is used.
     */
    private boolean customSequenceDefined; // = false;

    // -- reference point
    /**
     * Horizontal offset of the reference point from the top left of the sprite.
     */
    int dRefX; // =0

    /**
     * Vertical offset of the reference point from the top left of the sprite.
     */
    int dRefY; // =0

    // --- collision rectangle

    /**
     * Horizontal offset of the top left of the collision rectangle from the top
     * left of the sprite.
     */
    int collisionRectX; // =0

    /**
     * Vertical offset of the top left of the collision rectangle from the top
     * left of the sprite.
     */
    int collisionRectY; // =0

    /**
     * Width of the bounding rectangle for collision detection.
     */
    int collisionRectWidth;

    /**
     * Height of the bounding rectangle for collision detection.
     */
    int collisionRectHeight;

    // --- transformation(s)
    // --- values that may change on setting transformations
    // start with t_

    /**
     * The current transformation in effect.
     */
    int t_currentTransformation;

    /**
     * Horizontal offset of the top left of the collision rectangle from the top
     * left of the sprite.
     */
    int t_collisionRectX;

    /**
     * Vertical offset of the top left of the collision rectangle from the top
     * left of the sprite.
     */
    int t_collisionRectY;

    /**
     * Width of the bounding rectangle for collision detection, with the current
     * transformation in effect.
     */
    int t_collisionRectWidth;

    /**
     * Height of the bounding rectangle for collision detection, with the
     * current transformation in effect.
     */
    int t_collisionRectHeight;

    // ----- Constructors -----

    public Sprite(Image image) {
        super(image.getWidth(), image.getHeight());

        initializeFrames(image, image.getWidth(), image.getHeight(), false);

        // initialize collision rectangle
        initCollisionRectBounds();

        // current transformation is TRANS_NONE
        this.setTransformImpl(TRANS_NONE);

    }

    public Sprite(Image image, int frameWidth, int frameHeight) {

        super(frameWidth, frameHeight);
        // if img is null img.getWidth() will throw NullPointerException
        if ((frameWidth < 1 || frameHeight < 1)
                || ((image.getWidth() % frameWidth) != 0)
                || ((image.getHeight() % frameHeight) != 0)) {
            throw new IllegalArgumentException();
        }

        // construct the array of images that
        // we use as "frames" for the sprite.
        // use default frame , sequence index = 0
        initializeFrames(image, frameWidth, frameHeight, false);

        // initialize collision rectangle
        initCollisionRectBounds();

        // current transformation is TRANS_NONE
        this.setTransformImpl(TRANS_NONE);

    }

    public Sprite(Sprite s) {

        super(s != null ? s.getWidth() : 0, s != null ? s.getHeight() : 0);

        if (s == null) {
            throw new NullPointerException();
        }

        this.sourceImage = Image.createImage(s.sourceImage);

        this.numberFrames = s.numberFrames;

        this.frameCoordsX = new int[this.numberFrames];
        this.frameCoordsY = new int[this.numberFrames];

        System.arraycopy(s.frameCoordsX, 0, this.frameCoordsX, 0, s
                .getRawFrameCount());

        System.arraycopy(s.frameCoordsY, 0, this.frameCoordsY, 0, s
                .getRawFrameCount());

        this.x = s.getX();
        this.y = s.getY();

        // these fields are set by defining a reference point
        this.dRefX = s.dRefX;
        this.dRefY = s.dRefY;

        // these fields are set when defining a collision rectangle
        this.collisionRectX = s.collisionRectX;
        this.collisionRectY = s.collisionRectY;
        this.collisionRectWidth = s.collisionRectWidth;
        this.collisionRectHeight = s.collisionRectHeight;

        // these fields are set when creating a Sprite from an Image
        this.srcFrameWidth = s.srcFrameWidth;
        this.srcFrameHeight = s.srcFrameHeight;

        // the above fields are used in setTransform()
        // which is why we set them first, then call setTransformImpl()
        // to set up internally used data structures.
        setTransformImpl(s.t_currentTransformation);

        this.setVisible(s.isVisible());

        this.frameSequence = new int[s.getFrameSequenceLength()];
        this.setFrameSequence(s.frameSequence);
        this.setFrame(s.getFrame());

        this.setRefPixelPosition(s.getRefPixelX(), s.getRefPixelY());

    }

    // ----- public methods -----

    public void defineReferencePixel(int x, int y) {
        dRefX = x;
        dRefY = y;
    }

    public void setRefPixelPosition(int x, int y) {

        // update this.x and this.y
        this.x = x
                - getTransformedPtX(dRefX, dRefY, this.t_currentTransformation);
        this.y = y
                - getTransformedPtY(dRefX, dRefY, this.t_currentTransformation);

    }

    public int getRefPixelX() {
        return (this.x + getTransformedPtX(dRefX, dRefY,
                this.t_currentTransformation));
    }

    public int getRefPixelY() {
        return (this.y + getTransformedPtY(dRefX, dRefY,
                this.t_currentTransformation));
    }

    public void setFrame(int sequenceIndex) {
        if (sequenceIndex < 0 || sequenceIndex >= frameSequence.length) {
            throw new IndexOutOfBoundsException();
        }
        this.sequenceIndex = sequenceIndex;
    }

    public final int getFrame() {
        return sequenceIndex;
    }

    public int getRawFrameCount() {
        return numberFrames;
    }

    public int getFrameSequenceLength() {
        return frameSequence.length;
    }

    public void nextFrame() {
        sequenceIndex = (sequenceIndex + 1) % frameSequence.length;
    }

    public void prevFrame() {
        if (sequenceIndex == 0) {
            sequenceIndex = frameSequence.length - 1;
        } else {
            --sequenceIndex;
        }
    }

    public final void paint(Graphics g) {
        // managing the painting order is the responsibility of
        // the layermanager, so depth is ignored
        if (g == null) {
            throw new NullPointerException();
        }

        if (visible) {
            // width and height of the source
            // image is the width and height
            // of the original frame
            
            drawRegion(g,sourceImage,
                    frameCoordsX[frameSequence[sequenceIndex]],
                    frameCoordsY[frameSequence[sequenceIndex]], srcFrameWidth,
                    srcFrameHeight, t_currentTransformation, this.x, this.y,
                    Graphics.TOP | Graphics.LEFT);
            /*
            g.drawRegion(sourceImage,
                    frameCoordsX[frameSequence[sequenceIndex]],
                    frameCoordsY[frameSequence[sequenceIndex]], srcFrameWidth,
                    srcFrameHeight, t_currentTransformation, this.x, this.y,
                    Graphics.TOP | Graphics.LEFT);*/
        }

    }

    public void setFrameSequence(int sequence[]) {

        if (sequence == null) {
            // revert to the default sequence
            sequenceIndex = 0;
            customSequenceDefined = false;
            frameSequence = new int[numberFrames];
            // copy frames indices into frameSequence
            for (int i = 0; i < numberFrames; ++i) {
                frameSequence[i] = i;
            }
            return;
        }

        if (sequence.length < 1) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -