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

📄 stage.java

📁 j2me开发框架
💻 JAVA
字号:
package com.podome.ui;

import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;

import com.podome.log.Logger;

public class Stage extends Canvas {

	private Vector keysQueue;
	//private Vector cursorQueue;
	private Vector repaintQueue;

	private Thread eventDispatcher;

	private static UIContainer current;
	private static Stage stage;
	private static boolean hasDisplay = false;

	public static MIDlet midlet;

	static int canvasHeight = 0;
	static int canvasWidth = 0;
	
	//如果该标志为空,系统只处理用户特定的输入事件,为了实现用户阻塞事件设置。
	static boolean userEventBlockFlag = false; 
	
	static EventDispatch blockListener;
	
	static Class eventDispatchCls;
	
	static{
		try {
			eventDispatchCls = Class.forName("com.podome.ui.EventDispatch");
		} 
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 
	 */
	private Stage() {
		super();
		this.setFullScreenMode(true);

		// 初始化重画队列,并启动重画监控线程
		repaintQueue = new Vector();
		keysQueue = new Vector();

		// 初始化按键事件队列,并启动按键监控线程
		eventDispatcher = new UserEventDispatcher();

		//设置最大优先级
		eventDispatcher.setPriority(Thread.MAX_PRIORITY);
		
		//启动线程
		eventDispatcher.start();
	}

	/**
	 * 设置屏幕的高度和宽度
	 */
	private void resetSize() {
		Logger.info("Resize Canvas!");
		if(canvasHeight == 0 || canvasHeight < getHeight() ){
			canvasHeight = getHeight();
			if(canvasHeight >0 ) hasDisplay = true;
		}
		if(canvasWidth == 0 || canvasWidth < getWidth() ){
			canvasWidth = getWidth();
			if(canvasWidth >0 )hasDisplay = true;
		}
	}

	/**
	 * 尺寸改变时重新设置屏幕的高度和宽度
	 */
	protected void sizeChanged(int w, int h) {	
		Logger.info("SIZE CHANGE W=" + w + ",H=" + h);
		resetSize();
	}

	public static Stage getInstance() {
		if (stage == null) {
			stage = new Stage();
		}
		return stage;
	}

	/**
	 * @return Page
	 */
	public final static UIContainer currentPage() {
		return current;
	}

	/**
	 * @param page
	 */
	public final static void setCurrentPage(UIContainer page) {
		if (page != null && current != page) {
			if(current != null)
				current.offsetImage = null;	
			current = page;
			if (current != null) {
				stage.notifyRepaint(current);
			}
		}
	}

	/**
	 * 获取画布的高度
	 */
	public static int getCH() {
		return canvasHeight;
	}

	/**
	 * 获取画布的宽度度
	 */
	public static int getCW() {
		return canvasWidth;
	}
	
	/**
	 * 判断主屏幕是否已经显示,如果该属性为true,则可以取得
	 * 屏幕的高度和宽度值
	 * @return
	 */
	public static boolean hasDisplay(){
		return hasDisplay;
	}

	public final void paint(Graphics g) {
		// 从重画队列中检索重画对象
		//Logger.info("Stage RepaintQueue:size=" + repaintQueue.size());		
		while (repaintQueue.size() > 0) {			 
			Paint o = (Paint) repaintQueue.elementAt(0);			
			if (o != null) o.paint(g); //reflush屏幕			
			repaintQueue.removeElementAt(0);
		}
	}

	void notifyRepaint(Paint o) {
		if (o != null) {
			repaintQueue.addElement(o);
			repaint();
		}
	}
	
	void notifyRepaintCurrent(){
		if ( current != null) {
			repaintQueue.addElement( current );				
			repaint();
		}
	}

	/**
	 * @param keyCode
	 */
	public final void keyPressed(int keyCode) {
		if(!userEventBlockFlag )
		    keysQueue.addElement(new Integer(keyCode));
		else if( blockListener != null)
			blockListener.keyPressed(keyCode);		
	}
	
	public final void keyRepeated(int keyCode){
		keyPressed(keyCode);
	}
	
	/**
	 * 设置阻塞用户行为的标志
	 * @param isBlock
	 */
	static void setUserEventBlock(boolean isBlock){
		if(userEventBlockFlag != isBlock)
			userEventBlockFlag = isBlock;
	}
	/**
	 * 设置阻塞监听器对象
	 * @param dis
	 */
	static void setBlockListener(EventDispatch dis){
		blockListener = dis;
	}

	/**
	 * @param keyCode
	 */
	public final void keyReleased(int keyCode) {

	}

	/**
	 * @param x
	 * @param y
	 */
	public final void pointerPressed(int x, int y) {
		Logger.info("pointerPressed x:" + x + ",y:" + y);
	}

	/**
	 * @param x
	 * @param y
	 */
	public final void pointerDragged(int x, int y) {
		Logger.info("pointerDragged x:" + x + ",y:" + y);
	}

	/**
	 * @param x
	 * @param y
	 */
	public final void pointerReleased(int x, int y) {
		Logger.info("pointerReleased x:" + x + ",y:" + y);
	}

	public void hideNotify() {
		Logger.info("Hide Notify!");
	}

	/**
	 * 
	 */
	public void showNotify() {
		Logger.info("Show Notify!");
		if ( current != null) {
			repaintQueue.addElement( current );
		}
		this.repaint();
	}

	/**
	 * 按键事件处理线程
	 * 
	 * @author leadj
	 * 
	 */
	class UserEventDispatcher extends Thread {
		public void run() {
		    while(true){
		    	if(keysQueue.size() > 0 && !userEventBlockFlag ){	
				    int keyCode = ((Integer) keysQueue.elementAt(0)).intValue();
				    keysQueue.removeElementAt(0);
					try {
						currentPage().keyPressed(keyCode);
					} 
					catch (Exception e) {
						e.printStackTrace();
					}
				    continue;
		    	}		  	
				try {
					sleep(25);
				} 
				catch (InterruptedException e) {
					//Logger.info("InterruptedException");
				}
		    }					
	    }
	}
}

⌨️ 快捷键说明

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