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

📄 sprite.java

📁 该源码实现了j2me中用midp1.0实现的midp2.0的game类
💻 JAVA
字号:
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Copyright(c) 2003-2004 Jordi Martin Perez
 *  $Id: Sprite.java,v 1.4 2004/07/01 22:17:51 piratis Exp $
*/
package org.piratis.j2me.core.game;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * Provide some basic sprite functionality: getting and setting of coordinates 
 * and dimensions, as well as simple collision detection.
 * 
 * @author		Jordi Mart韓
 * @copyright	Copyright (c) 2004
 * @created 	24-jun-2004
 * @version		$Id: Sprite.java,v 1.4 2004/07/01 22:17:51 piratis Exp $
 */
public class Sprite
	extends Layer
{
    /**
     * for inner image management!
     */
    private Image image;
    
    /**
     * Animated sprite / frame related vars
     */
    private int frame;
    private int nFrames;
    private int nFramesRow;
    private int nFramesCol;
    private int frameX;
    private int frameY;
    
    /**
     * frame sequence to follow
     */
    private int[] frameSequence;
    
    /**
     * Current frame will return this if there is no animated sprite
     */
    public static int NO_FRAME = -1;
    
    /**
     * New 'static' Sprite with the given Image. 
     * @param image the Image to use
     */
    public Sprite(Image img)
    {
        this.image = img;
        this.bbox.width = img.getWidth();
        this.bbox.height = img.getHeight();
        // no dynamic!
        this.frame = Sprite.NO_FRAME;
        this.nFrames = 1;
    }
    
    /**
     * New 'dynamic' (animtaed) Sprite with the given frames contained in the Image.
     * Each frame has the same size (frameWidth x frameHeight). The Image provided
     * might conform whatever layout, but its overall width and height must be a an
     * integer multiple of the frame width and height.<br>
     * <br>
     * Aside from that, the Sprite will behave as a static Sprite regarding its
     * width and height (by calling {@link Layer#getBbox()}).
     * @param img Image containing the frames
     * @param frameWidth the width of each frame
     * @param frameHeight the height of each frame
     */
    public Sprite(Image img, int frameWidth, int frameHeight)
    {
        this.image = img;
        this.bbox.width = frameWidth;
        this.bbox.height = frameHeight;
        // precalc nFrames X row and X col
        this.nFramesRow = img.getWidth() / frameWidth;
        this.nFramesCol = img.getHeight() / frameHeight;
        this.nFrames = this.nFramesRow*this.nFramesCol;
        this.frame = this.frameX = this.frameY = 0;
        // default frame sequence
        this.setDefaultFrameSequence();
    }

    /**
     * Paints the Sprite into the Graphics object provided.
     * @see Layer#paint(javax.microedition.lcdui.Graphics)
     */
    public final void paint(Graphics g)
    {
        if (!isVisible()) return;

        if (this.nFrames > 1)
        {
            // clip to paint only the current frame
            int clipX, clipY, clipW, clipH;
            clipX = g.getClipX(); clipY = g.getClipY();
            clipW = g.getClipWidth(); clipH = g.getClipHeight();
            g.clipRect(this.bbox.x, this.bbox.y, 
                    this.bbox.width, this.bbox.height);
            // move the image origin
            g.translate(-this.frameX, -this.frameY);
            // draw image
            g.drawImage(this.image, this.bbox.x, this.bbox.y,
                    Layer.TOP_LEFT);
            // undo the image origin translation
            g.translate(this.frameX, this.frameY);
            // restore clipping values
            g.setClip(clipX, clipY, clipW, clipH);
        }
        else
        {
            // simple image drawing
	        g.drawImage(this.image, this.bbox.x, this.bbox.y, 
	                Graphics.TOP|Graphics.LEFT);
        }
    }

    /**
     * Gets the current frame index
     * @return the index in the frame sequence
     */
    public int getFrame()
    {
        return this.frame;
    }

    /**
     * Selectes the current frame in the current sequence.<br>
     * <br>
     * The index refers to the position in the frame sequence, not to the
     * frame index (within the Image).
     * 
     * @param newFrame the index of the desired frame to be displayed on the next
     *	paint call of the current sequence
     * @throws IndexOutOfBoundsException if the index is out of the bound of the
     * 	current frame sequence
     */
    public void setFrame(int newFrame)
    	throws IndexOutOfBoundsException
    {
        if (this.nFrames > 1 && (newFrame < 0 || newFrame > this.frameSequence.length))
            throw new IndexOutOfBoundsException("Sprite#setFrame(" + newFrame + "), given index is out of bounds!");
        // set frame
        this.frame = newFrame;
        int theFrame = this.frameSequence[this.frame];
        // recalc X & Y
        if (this.nFrames > 1)
        {
            this.frameX = (theFrame % this.nFramesRow) * this.bbox.width;  // newFrame % framesRow = col
            this.frameY = (theFrame / this.nFramesRow) * this.bbox.height; // newFrame / framesRow = row
        }
    }
    
    /**
     * Moves forward to the next frame in sequence<br> 
     * <br>
     * Frame sequences are circular: when reached the end of the sequence, it
     * will continue at the beginning.
     * @see #prevFrame()
     */
    public void nextFrame()
    {
        this.setFrame( (this.frame+1) % this.frameSequence.length );
    }

    /**
     * Moves backward to the previous frame in sequence<br> 
     * <br>
     * Frame sequences are circular: when reached the beginnig of the sequence, it
     * will continue at the last frame.
     * @see #nextFrame()
     */
    public void prevFrame()
    {
        this.setFrame( (this.frame-1)<0?this.frameSequence.length-1:this.frame-1 );
    }
    
    /**
     * Establish the frame sequence of the Sprite.
     * @param sequence
     */
    public void setFrameSequence(int[] sequence)
    {
        // initial frame
        this.frame = 0;
        if (sequence != null)
        {
            // copy array
            this.frameSequence = new int[sequence.length];
            System.arraycopy(sequence, 0, this.frameSequence, 0, sequence.length);
        }
        else
            this.setDefaultFrameSequence();
    }
    
    /**
     * Sets the default frame sequence -> 0..nFrames-1  
     */
    private void setDefaultFrameSequence()
    {
        this.frameSequence = null;
        if (this.nFrames > 1)
        {
            this.frameSequence = new int[this.nFrames];
            // backward looping is quicker!
            for (int i=this.nFrames-1; i >= 0; i--)
                this.frameSequence[i] = i;
        }
    }
}

⌨️ 快捷键说明

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