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

📄 keyeventmidlet.java

📁 example2 众多JAVA实例源码...学习java基础的好帮手
💻 JAVA
字号:
package opusmicro.demos.event;

/* Copyright (c) 2006 System Exclusive & Multi Midi  (SEMM)
 All rights reserved.
 Author : Paul Hamaker.
 Part of javalessons.com
 This code is for educational purposes only.*/
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class KeyEventMIDlet extends MIDlet implements CommandListener {
	private OurCanvas canv;
	private boolean pause = false;
	private boolean keepgoing = true;
	private Display dsp;
	private Command cmdq;

	public KeyEventMIDlet() {
		cmdq = new Command("Quit", Command.EXIT, 0);
		canv = new OurCanvas();
	}

	class OurCanvas extends Canvas implements Runnable {
		private int w, h;

		OurCanvas() {
			w = getWidth();
			h = getHeight();
			x = w / 2 - 5;
			y = h / 2 - 5;
			new Thread(this).start();
		}

		private int gs = 180;

		public void paint(Graphics g) {
			g.setGrayScale(255);
			g.fillRect(0, 0, w, h);
			g.setGrayScale(gs);
			g.fillRect(x, y, 40, 20);
			gs = 180;
		}

		int gx = -999;

		public void keyPressed(int key) {
			gx = getGameAction(key);
			pause = false;
		}

		public void keyReleased(int key) {
			pause = true;
			gx = -999;
		}

		public void run() {
			while ( keepgoing) {
				if ( !pause) {
					switch ( gx) {
						case UP:
							y -= dy;
							break;
						case DOWN:
							y += dy;
							break;
						case LEFT:
							x -= dx;
							break;
						case RIGHT:
							x += dx;
							break;
						case FIRE:
							gs = 104;
					}
					repaint();
				}// end if
				try {
					Thread.sleep(40);
				}
				catch (InterruptedException e) {
				}
			}// end while
		}

		private int x;
		private int y;
		private int dx = 10;
		private int dy = 10;
	}// end canvas class

	public void commandAction(Command c, Displayable s) {
		if ( c == cmdq) notifyDestroyed();
	}

	public void startApp() {
//		if ( canv == null) canv = new OurCanvas();
//		canv.addCommand(cmdq);
//		canv.setCommandListener(this);
		dsp = Display.getDisplay(this);
//		dsp.setCurrent(canv);
//		pause = false;
//		AlertType.INFO.playSound(dsp);
		dsp.setCurrent(new KeyEventCanvas());
	}

	public void pauseApp() {
		pause = true;
	}

	public void destroyApp(boolean b) {
		keepgoing = false;
	}
}

⌨️ 快捷键说明

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