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

📄 layermanager.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
*/
package org.piratis.j2me.core.game;

import javax.microedition.lcdui.Graphics;

import org.piratis.j2me.core.BBox2D;
import org.piratis.j2me.core.QuickList;


/**
 * As in MIDP2.0, the LayerManager manages Layers. The LayerManager 
 * simplifies the process of rendering the Layers that have been added 
 * to it by automatically rendering the correct regions of each Layer 
 * in the appropriate order.<br>
 * <br>
 * The LayerManager mantains an ordered list to which Layers can be
 * appended, inserted and removed. Its index in the list correlates to
 * its z-order: index 0 is closest to the user, while the higher index, the
 * further from the user.
 * TODO: is it necessary to keep an own bbox?
 * 
 * @author		Jordi Mart韓
 * @copyright	Copyright (c) 2003-2004
 * @created 	24-jun-2004
 * @version		$Id: LayerManager.java,v 1.3 2004/06/26 18:48:31 piratis Exp $
 */

public class LayerManager
{
    /**
     * Where the layers will be stored
     */
    private QuickList layers = null;
    
    /**
     * View Window
     */
    private BBox2D window = null;
    
    /**
     * Creates a new LayerManager
     */
    public LayerManager()
    {
        this.layers = new QuickList();
        // view window -> max value
        this.window = new BBox2D();
        this.window.width = this.window.height = Integer.MAX_VALUE;
    }
    
    /**
     * Appends a Layer to this LayerManager. It will be given the highest
     * position available, thus the furthest away from the user. It is also
     * first removed if was previously added
     * @param l
     */
    public void add(Layer l)
    {
        this.layers.removeElement(l);
        this.layers.add(l);
    }
    
    /**
     * Inserts a Layer at a given position. The Layer is first removed
     * if previously added, and Layers with index equals or greater than
     * the specified are shift to the right.
     * @param l Layer
     * @param idx where to insert the layer
     */
    public void insertAt(Layer l, int idx)
    {
        this.layers.removeElement(l);
        this.layers.insertElementAt(l, idx);
    }
    
    /**
     * Gets the layer at the specified index
     * @param idx the index
     * @return the Layer, if index ok
     * @throws IndexOutOfBoundsException if element not found at the
     * 	specified index
     */
    public Layer getLayerAt(int idx)
    {
        return (Layer) this.layers.elementAt(idx);
    }
    
    /**
     * Gets the number of layers
     * @return the number of layers
     */
    public int getSize()
    {
        return this.layers.length();
    }
    
    /**
     * Deletes the Layer from this LayerManager.
     * @param l the Layer to be removed
     */
    public void remove(Layer l)
    {
        this.layers.removeElement(l);
    }
    
    /**
     * Renders the LayerManager's Layers.<br>
     * <br>
     * The Layers within this LayerManager are rendered following the z-order
     * given by its position, i.e. descending order.<br>
     * <br>
     * x,y params specify where is the LayerManager to be rendered relative
     * to the Graphic's origin.
     * @param g to draw the LayerManager
     * @param x horizontal location at which to render the view window
     * @param y vertical location at which to render the view window
     * @see #setWindow(int, int, int, int)
     */
    public void paint(Graphics g, int x, int y)
    {
        // translate issues
        int transX, transY;
        transX = g.getTranslateX();
        transY = g.getTranslateY();
        g.translate(x + (x - this.window.x)- transX, 
                y + (y - this.window.y) - transY);

        // store prev clipping
        int clipX, clipY, clipW, clipH;
        clipX = g.getClipX(); clipY = g.getClipY();
        clipW = g.getClipWidth(); clipH = g.getClipHeight();
        // set clipping
        g.clipRect(this.window.x, this.window.y, 
                this.window.width, this.window.height);

        // draw sprites
        Layer l;
        for (int i = this.layers.length()-1; i >= 0; i--)
        {
            l = (Layer) this.layers.elementAt(i);
            // check visibility && inside the window!
            if (l.isVisible() && l.getBbox().collide(this.window))
                l.paint(g);
        }
        
        // restore clipping
        g.setClip(clipX, clipY, clipW, clipH);

        // restore translate issues
        g.translate(transX - g.getTranslateX(), transY - g.getTranslateY());

    }
    
    /**
     * Sets the view window on the LayerManager.
     * @param x left relative to the LayerManager's origin 
     * @param y top idem.
     * @param width
     * @param height
     */
    public void setWindow(int x, int y, int width, int height)
    {
        this.window.x = x;
        this.window.y = y;
        this.window.width = width;
        this.window.height = height;
    }
    
    /**
     * Sets the view window on the LayerManager
     * @param other the new view window
     * @see #setWindow(int, int, int, int)
     */
    public void setWindow(BBox2D other)
    {
        this.setWindow(other.x, other.y, other.width, other.height);
    }
    
    /**
     * Retrieve the current world window 
     * @return world's window
     */
    public BBox2D getWindow()
    {
        return this.window;
    }
}

⌨️ 快捷键说明

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