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

📄 gamecanvas.java

📁 j2me入门游戏-雷电 游戏实现了最基本的功能比如飞机移动、产生飞机、发射子弹、击中敌方、爆炸等功能。
💻 JAVA
字号:
/********************************************************************
 * 项目名称				:<b>足球项目j2me客户端</b>			<br/>
 * 
 * Copyright 2005-2006 Teesoo. All rights reserved
 ********************************************************************/
package org.wuhua.game;

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

import org.wuhua.game.util.Log;

/**
 * <b>类名:GameCanvas.java</b> </br> 编写日期: 2006-11-29 <br/> 程序功能描述:
 * 实现双缓冲的Game画布。实现原理是创建一个BufferImage。然后绘制,最后显示出来。就这么简单。<br/> Demo: <br/> Bug:
 * <br/>
 * 
 * 程序变更日期 :<br/> 变更作者 :<br/> 变更说明 :<br/>
 * 
 * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
 */
public abstract class GameCanvas extends Canvas {
	private static Log log = Log.getLog("GameCanvas");
	/**
	 * 绘制缓冲的图片。用户绘制资源的时候都是操作这个图片来进行的
	 */
	protected Image bufferImage;

	protected int HEIGHT;

	protected int WIDTH;

	private int clipX, clipY, clipWidth, clipHeight;

	private boolean setClip;
	
	private int keyStates;

	public final int getKeyStates() {
		return keyStates;
	}

	protected GameCanvas() {

		super();
	}

 
	protected void paint(Graphics g) {
		// 如果要求绘制指定区域的话就需要这样了
		if (this.setClip) {
			g.clipRect(this.clipX, this.clipY, this.clipWidth, this.clipHeight);
			this.setClip = false;
		}
		if(bufferImage == null){
			return;
		}
		g.drawImage(this.bufferImage, 0, 0, Graphics.TOP | Graphics.LEFT);

	}

	public void flushGraphics(int x, int y, int width, int height) {
		this.setClip = true;
		this.clipX = x;
		this.clipY = y;
		this.clipWidth = width;
		this.clipHeight = height;

		repaint();
		serviceRepaints();
	}

	public void flushGraphics() {
		repaint();
		serviceRepaints();
	}

	/**
	 * 设计者主要是通过调用这个方法获取图片。然后就可以绘制了
	 * 
	 * @return
	 */
	protected Graphics getGraphics() {
		if(bufferImage == null)
			return null;
		
		return this.bufferImage.getGraphics();
	}

	/**
	 * 这个方法主要是处理Nokia平台,用户调用setFullScreenMode(boolean enable) 时重新按照新的w & h创建缓冲图片
	 */
	protected   void sizeChanged(int w, int h) {
		if (h > HEIGHT) {
			this.bufferImage = Image.createImage(w, h);
			this.WIDTH = w;
			this.HEIGHT = h;
		}
	}

	 
	/**
	 * 按照颜色填充整个屏幕
	 */
	public final void fillFullScreen(Graphics g, int rgb) {
		if (g == null) {
			throw new NullPointerException("Graphics is not exists");
		}
		g.setColor(rgb);
		g.fillRect(0, 0, WIDTH, HEIGHT);
	}

	

	public final void setKeyStates(int keyStates) {
		this.keyStates = keyStates;
	}

	protected void keyPressed(int keyCode) {
		this.setKeyStates(keyCode);
		 
	}
	
	/**
	 * Called when a key is repeated (held down).
	 * 
	 * <P>
	 * The <code>getGameAction()</code> method can be called to determine what
	 * game action, if any, is mapped to the key. Class <code>Canvas</code>
	 * has an empty implementation of this method, and the subclass has to
	 * redefine it if it wants to listen this method.
	 * </P>
	 * 
	 * @param keyCode
	 *            the key code of the key that was repeated
	 * @see #hasRepeatEvents()
	 */
	protected void keyRepeated(int keyCode) {
		this.setKeyStates(keyCode);
	}

	/**
	 * Called when a key is released.
	 * <P>
	 * The <code>getGameAction()</code> method can be called to determine what
	 * game action, if any, is mapped to the key. Class <code>Canvas</code>
	 * has an empty implementation of this method, and the subclass has to
	 * redefine it if it wants to listen this method.
	 * </P>
	 * 
	 * @param keyCode
	 *            the key code of the key that was released
	 */
	protected void keyReleased(int keyCode) {
		this.setKeyStates(keyCode);
	}

	public final int getHEIGHT() {
		return HEIGHT;
	}

	public final int getWIDTH() {
		return WIDTH;
	}

}

⌨️ 快捷键说明

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