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

📄 canvas.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) 2004 Jordi Martin Perez
*/
package org.piratis.j2me.core.game;

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

import org.piratis.j2me.core.BBox2D;

/**
 * CoreCanvas Description
 * 
 * @author		Jordi Mart韓
 * @copyright	Copyright (c) 2004
 * @created 	24-jun-2003
 * @version		$Id: Canvas.java,v 1.3 2004/07/22 16:49:42 piratis Exp $
 */
public class Canvas 
	extends javax.microedition.lcdui.Canvas
	implements Runnable
{
	/**
	 * Canvas display
	 */
	protected Display display;
	
	/**
	 * Double buffering image
	 */
	protected Image offscreen = null;

	/**
	 * Canvas size
	 */
	private int width;
	private int height;
	
	/**
	 * Bg painting issues
	 */
	private boolean paintBg = true;
	private int bgColor = 0xFFFFFF;
	
	/**
	 * The Canvas' engine
	 */
	private Engine engine;
	
	/**
	 * Creates a new instance of a CoreCanvas, with an empty (white pixels)
	 * buffer for offscreen processing.
	 * @param display Canva's display
	 */
	public Canvas(Display display)
	{
		super();
		
		this.initialize(display);
	}
	
	/**
	 * Returns the Graphics object for rendering a Game Canvas: it will be
	 * the Graphics object of the off-screen buffer.<br>
	 * <br>
	 * Painting operations will not be visible on the display
	 * as long as flushGraphics() is not called
	 * @return this off-screen's Graphic object
	 */
	protected Graphics getGraphics()
	{
	    return this.offscreen.getGraphics();
	}

	/**
	 * @return the canvas' display
	 */
	public Display getDisplay()
	{
		return this.display;
	}

    /** 
     * 
     * To be called by a {@link Display#callSerially(java.lang.Runnable)}
     * when requesting a new repaint. It is the same as calling
     * {@link Canvas#flushGraphics()}.
     * @see java.lang.Runnable#run()
     * @see #flushGraphics()
     * @see Display#callSerially(java.lang.Runnable)
     */
    public void run()
    {
        this.flushGraphics();
    }

    /**
	 * @see javax.microedition.lcdui.Displayable#paint(Graphics)
	 */
	protected void paint(Graphics g)
	{
		g.drawImage(this.offscreen, 0, 0, Graphics.LEFT | Graphics.TOP);
	}

	/**
	 * Paints the current background (if any). Default implementation clears
	 * the background with bgColor
	 * @param g graphics context
	 * @param area dirty region
	 */
	protected void paintBackground(Graphics g, BBox2D area)
	{
		int prevColor = g.getColor();
		g.setColor(this.bgColor);
		g.fillRect(area.x, area.y, area.width, area.height);
		g.setColor(prevColor);
	}
	
	/**
	 * Sets its associated engine. High coupling between Canvas and Engine!
	 * @param newEngine its engine
	 * @see Engine
	 */
	protected void setEngine(Engine newEngine)
	{
	    this.engine = newEngine;
	}
	
	/**
	 * Flushes the off-screen buffer to the display.
	 */
	public void flushGraphics()
	{
	    // force repainting
	    this.repaint();
	    this.serviceRepaints();
	}

	/**
	 * Inner initialization
	 * @param display where the canvas will be displayed
	 */
	private void initialize(Display display)
	{
		// init params
		this.display = display;

		this.width = getWidth();
		this.height = getHeight();

		// double buffering screen
		this.offscreen = Image.createImage(width, height);
		
		// engine currently not known
		this.engine = null;
	}
	
    /* (non-Javadoc)
     * @see javax.microedition.lcdui.Displayable#keyPressed(int)
     */
    protected void keyPressed(int keyCode)
    {
        if (this.engine != null)
            this.engine.keyPressed(keyCode);
    }
    /* (non-Javadoc)
     * @see javax.microedition.lcdui.Displayable#keyReleased(int)
     */
    protected void keyReleased(int keyCode)
    {
        if (this.engine != null)
            this.engine.keyReleased(keyCode);
    }
}

⌨️ 快捷键说明

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