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

📄 keyeventcanvas.java

📁 Java ME中对多个按键同时处理的例子。
💻 JAVA
字号:
import java.util.Vector;

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

// Canvas类:
public class KeyEventCanvas extends Canvas implements Runnable {
	int x, y;

	int preX, preY;

	/** 保存被按下的键 */
	private Vector keys = new Vector();

	private int keyStates;

	private int ballSize = 14;

	public static final int UP_PRESSED = 1 << Canvas.UP;

	public static final int DOWN_PRESSED = 1 << Canvas.DOWN;

	public static final int LEFT_PRESSED = 1 << Canvas.LEFT;

	public static final int RIGHT_PRESSED = 1 << Canvas.RIGHT;

	public static final int FIRE_PRESSED = 1 << Canvas.FIRE;

	public KeyEventCanvas() {
		new Thread(this).start();
	}

	// 按下鍵后的处理,往数组里添加
	private void addKey(int keyCode) {

		keyCode = keyCode < 0 ? getGameAction(keyCode) : keyCode;

		keys.addElement(keyCode + "");

		keyStates += 1 << keyCode;
	}

	// 松开键的处理,从数组中去除
	private void removeKey(int keyCode) {
		keyCode = keyCode < 0 ? getGameAction(keyCode) : keyCode;

		keys.removeElement(keyCode + "");

		keyStates -= 1 << keyCode;
	}

	public void keyPressed(int keyCode) {
		// System.out.println("Key Pressed " + keyCode);
		addKey(keyCode);
	}

	public void keyReleased(int keyCode) {
		removeKey(keyCode);
	}

	public void paint(Graphics g) {
		g.setColor(0xffffff);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());

		g.setColor(0xff0000);
		g.fillArc(x, y, ballSize, ballSize, 0, 360);

		g.setColor(0);
		g.drawString("hasRepeatEvents-->" + hasRepeatEvents(), 0, 0,
				Graphics.LEFT | Graphics.TOP);
		g.drawString("hasPointerEvents-->" + hasPointerEvents(), 0, 20,
				Graphics.LEFT | Graphics.TOP);
		g.drawString("hasPointerMotionEvents-->" + hasPointerMotionEvents(), 0,
				40, Graphics.LEFT | Graphics.TOP);

	}

	public int getKeyStates() {
		return keyStates;
	}

	public void run() {

	}
}

class DemoCanvas extends KeyStack {
	int x = 100, y = 100;

	// int preX, preY;

	private int ballSize = 14;

	public DemoCanvas() {
	}

	public void paint(Graphics g) {
		// g.setColor(0xffffff);
		// g.fillRect(0, 0, this.getWidth(), this.getHeight());

		g.setColor(0xff0000);
		g.fillArc(x, y, ballSize, ballSize, 0, 360);
		
		// g.setColor(0);
		// g.drawString("hasRepeatEvents-->" + hasRepeatEvents(), 0, 0,
		// Graphics.LEFT | Graphics.TOP);
		// g.drawString("hasPointerEvents-->" + hasPointerEvents(), 0, 20,
		// Graphics.LEFT | Graphics.TOP);
		// g.drawString("hasPointerMotionEvents-->" + hasPointerMotionEvents(),
		// 0,
		// 40, Graphics.LEFT | Graphics.TOP);
	}

	public void keyPressed(int keyCode) {
		super.keyPressed(keyCode);
	}

	public void keyReleased(int keyCode) {
		super.keyReleased(keyCode);
	}

	public void input1() {

		int keyState = this.getKeyStates();

		if (((keyState >> Canvas.UP) & 0x1) == 1) {
			y--;
			if (y < 0) {
				y = 0;
			}
		}
		if (((keyState >> Canvas.DOWN) & 0x1) == 1) {
			y++;
			if (y > MainCanvas.getMainCanvas().getHeight() - ballSize) {
				y = MainCanvas.getMainCanvas().getHeight() - ballSize;
			}
		}
		if (((keyState >> Canvas.LEFT) & 0x1) == 1) {
			x--;
			if (x < 0) {
				x = 0;
			}
		}
		if (((keyState >> Canvas.RIGHT) & 0x1) == 1) {
			x++;
			if (x > MainCanvas.getMainCanvas().getWidth() - ballSize) {
				x = MainCanvas.getMainCanvas().getWidth() - ballSize;
			}
		}
	}

	public void run() {
		while (true) {
			input1();
			MainCanvas.getMainCanvas().repaint();
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}// end while(true)
	}// end function

}

⌨️ 快捷键说明

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