📄 sprite.java
字号:
/* * Siemens API for MicroEmulator * Copyright (C) 2003 Markus Heberling <markus@heberling.net> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.siemens.mp.color_game;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.Graphics;public class Sprite extends Layer { public static final int TRANS_NONE = 0; public static final int TRANS_ROT90 = 5; public static final int TRANS_ROT180 = 3; public static final int TRANS_ROT270 = 6; public static final int TRANS_MIRROR = 2; public static final int TRANS_MIRROR_ROT90 = 7; public static final int TRANS_MIRROR_ROT180 = 1; public static final int TRANS_MIRROR_ROT270 = 4; public Sprite(Image image) { super(image.getWidth(), image.getHeight()); initializeFrames(image, image.getWidth(), image.getHeight(), false); // initialize collision rectangle initCollisionRectBounds(); } 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(); } 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; 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 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 - dRefX; this.y = y - dRefY; } public int getRefPixelX() { return this.x + dRefX; } public int getRefPixelY() { return this.y + dRefY; } 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) { if (g == null) { throw new NullPointerException(); } if (visible) { int clipX = g.getClipX(); int clipY = g.getClipY(); int clipW = g.getClipWidth(); int clipH = g.getClipHeight(); g.setClip(this.x, this.y, this.srcFrameWidth, this.srcFrameHeight); g.drawImage(sourceImage, this.x-frameCoordsX[frameSequence[sequenceIndex]], this.y-frameCoordsY[frameSequence[sequenceIndex]],Graphics.TOP | Graphics.LEFT); g.setClip(clipX, clipY, clipW, clipH); } } 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) { throw new IllegalArgumentException(); } for (int i = 0; i < sequence.length; i++) { if (sequence[i] < 0 || sequence[i] >= numberFrames) { throw new ArrayIndexOutOfBoundsException(); } } customSequenceDefined = true; frameSequence = new int[sequence.length]; System.arraycopy(sequence, 0, frameSequence, 0, sequence.length); sequenceIndex = 0; } public void setImage(Image img, int frameWidth, int frameHeight) { // if image is null image.getWidth() will throw NullPointerException if ((frameWidth < 1 || frameHeight < 1) || ((img.getWidth() % frameWidth) != 0) || ((img.getHeight() % frameHeight) != 0)) { throw new IllegalArgumentException(); } int noOfFrames = (img.getWidth() / frameWidth)*(img.getHeight() / frameHeight); boolean maintainCurFrame = true; if (noOfFrames < numberFrames) { // use default frame , sequence index = 0 maintainCurFrame = false; customSequenceDefined = false; } if (! ((srcFrameWidth == frameWidth) && (srcFrameHeight == frameHeight))) { // computing is the location // of the reference pixel in the painter's coordinate system. // and then use this to find x and y position of the Sprite int oldX = this.x + dRefX; int oldY = this.y + dRefY; setWidthImpl(frameWidth); setHeightImpl(frameHeight); initializeFrames(img, frameWidth, frameHeight, maintainCurFrame); // initialize collision rectangle initCollisionRectBounds(); // set the new x and y position of the Sprite this.x = oldX - dRefX;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -