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

📄 xmain.java

📁 初学者的佳音 初学者的佳音 初学者的佳音
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
package x;

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

#if((${ant.handset_music}!="none")||(${ant.handset_sound}!="none"))
import javax.microedition.media.*;
#end

#if(${ant.handset_nokia})
import com.nokia.mid.ui.*;

#end

#if(${ant.handset_nokia})
public class XMain extends FullCanvas implements Runnable {
#elseif(!${ant.handset_command})
public class XMain extends Canvas implements Runnable {
#else
public class XMain extends Canvas implements Runnable, CommandListener {
	#end

	// ************************************
	// thread
	// ************************************

	protected int interval = 100; // sleep interval, affect game speed

	private MIDlet midlet;

	private boolean isRunning;

	private boolean isPausing;

	private boolean isPainting;

	public XMain() {
		isRunning = false;
	}

	public final void start(MIDlet midlet) {
		try {
			if (!isRunning) {
				this.midlet = midlet;

				initialize();

				Display.getDisplay(midlet).setCurrent(this);

				isRunning = true;
				isPausing = false;
				isPainting = false;

				Thread thread = new Thread(this);
				thread.start();
			} else {
				resume();
			}
		} catch (RuntimeException e) {
			showError(e);
		}
	}

	public void pause() {
		//isPausing = true;
		gamePause();
	}
	protected void gamePause(){
		
	}	
	protected void gameResume(){
		
	}

	public final void resume() {
		repaint = true;
		repaintAll = true;

		inputHolding = INPUT_NONE;
		inputPressed = INPUT_NONE;
		inputReleased = INPUT_NONE;
		inputIsHolding = false;

		isPausing = false;
		isPainting = false;
	}

	public final void destroy() {
		isRunning = false;
	}

	protected final void hideNotify() {
		pause();
	}

	protected final void showNotify() {
		resume();
	}

	protected void initialize() {
		//showInfo("initialize freeMemory:"+Runtime.getRuntime().freeMemory());

		// graphics

		#if(${ant.handset_midp}=="midp20")
		setFullScreenMode(true);
		#end

		#if(${ant.handset_width})
		width = ${ant.handset_width};
		#else
		width = getWidth();
		#end

		#if(${ant.handset_height})
		height = ${ant.handset_height};
		#else
		height = getHeight();
		#end

		repaint = true;
		repaintAll = true;

		#if(${ant.handset_smallfont})
		int size = Font.SIZE_SMALL;
		int face = Font.FACE_SYSTEM;
		int style = Font.STYLE_PLAIN;
		font = Font.getFont(face, style, size);
		#else
		font = Font.getDefaultFont();
		#end

		fontHeight = font.getHeight();

		objectArrow = -1;
		objectButton = -1;

		// input
		inputHolding = INPUT_NONE;
		inputPressed = INPUT_NONE;
		inputReleased = INPUT_NONE;
		inputIsHolding = false;

		softkeyOK = SOFTKEY_NONE;
		softkeyCancel = SOFTKEY_NONE;

		// command
		#if(${ant.handset_command})
		commandLeft = new Command(" ", SOFTKEY_VALUE_LEFT, 1);
		addCommand(commandLeft);

		commandRight = new Command(" ", SOFTKEY_VALUE_RIGHT, 1);
		addCommand(commandRight);

		setCommandListener(this);
		#end

		setState(STATE_START);
	}

	public final void run() {
		int inPainting = 0;

		while (isRunning) {
			try {
				if (isPausing) {
					Thread.sleep(interval);
				} else {
					if (isPainting && (inPainting <= 3)) {
						inPainting++;

						Thread.sleep(interval);
					} else {
						inPainting = 0;

						counter++;

						if (inputHolding == INPUT_NONE) {
							inputIsHolding = false;
						}

						if ((inputIsHolding && (inputHolding != INPUT_NONE))
								|| (inputPressed != INPUT_NONE)
								|| (inputReleased != INPUT_NONE)) {
							gameInput();

							inputPressed = INPUT_NONE;
							inputReleased = INPUT_NONE;
						} else if (inputHolding != INPUT_NONE) {
							inputIsHolding = true;
						}

						gameLogic();

						if (repaint) {
							repaint = false;

							repaint();
						}

						Thread.sleep(interval);
					}
				}
			} catch (InterruptedException e) {
				showError(e);
			} catch (RuntimeException e) {
				showError(e);
			}
		}

		midlet.notifyDestroyed();
	}

	// ************************************
	// paint
	// ************************************

	protected int width; // canvas width

	protected int height; // canvas height

	protected boolean repaint;

	protected boolean repaintAll;

	protected Font font;

	protected int fontHeight;

	protected int objectArrow;

	protected int objectButton;

	protected final void paint(Graphics g) {
		try {
			isPainting = true;

			#if(${ant.handset_midp}=="midp20")
			width = getWidth();
			height = getHeight();
			#end

			g.setClip(0, 0, width, height);
			g.setFont(font);

			gamePaint(g);

			if (objectButton != -1) {
				int buttonLeft = softkeyOK;
				int buttonRight = softkeyCancel;

				if (softkeyOK == SOFTKEY_OK) {
					#if(${ant.handset_reverseok})
					buttonLeft = softkeyCancel;
					buttonRight = softkeyOK;
					#end
				}

				if (softkeyOK == SOFTKEY_MENU) {
					#if(${ant.handset_reversemenu})
					buttonLeft = softkeyCancel;
					buttonRight = softkeyOK;
					#end
				}

				g.setClip(0, 0, width, height);

				if (buttonLeft != SOFTKEY_NONE) {
					int motion = buttonLeft * 2;
					drawObject(g, objectButton, motion, 0, 0, height);
				}

				if (buttonRight != SOFTKEY_NONE) {
					int motion = buttonRight * 2 + 1;
					drawObject(g, objectButton, motion, 0, width, height);
				}
			}
		} catch (RuntimeException e) {
			showError(e);
		} finally {
			isPainting = false;
		}
	}

	// ************************************
	// input
	// ************************************

	public static final int INPUT_NONE = -1;

	public static final int INPUT_UNKNOWN = 0;

	public static final int INPUT_OK = 1;

	public static final int INPUT_CANCEL = 2;

	public static final int INPUT_UP = 3;

	public static final int INPUT_DOWN = 4;

	public static final int INPUT_LEFT = 5;

	public static final int INPUT_RIGHT = 6;

	public static final int INPUT_FIRE = 7;

	public static final int INPUT_STAR = 8;

	public static final int INPUT_POUND = 9;

	public static final int INPUT_NUM0 = 10;

	public static final int INPUT_NUM1 = 11;

	public static final int INPUT_NUM2 = 12;

	public static final int INPUT_NUM3 = 13;

	public static final int INPUT_NUM4 = 14;

	public static final int INPUT_NUM5 = 15;

	public static final int INPUT_NUM6 = 16;

	public static final int INPUT_NUM7 = 17;

	public static final int INPUT_NUM8 = 18;

	public static final int INPUT_NUM9 = 19;

	protected int inputHolding;

	protected int inputPressed;

	protected int inputReleased;

	private boolean inputIsHolding;

	// ************************************
	// keypad
	// ************************************
	protected int inputKeyCode;

	protected final void keyPressed(int keyCode) {
		try {
			

			int input = getInput(keyCode);
			if ((inputPressed == INPUT_NONE) || (input != INPUT_UNKNOWN)) {
				inputHolding = input;
				inputPressed = input;
				inputKeyCode=keyCode;
			}
		} catch (RuntimeException e) {
			showError(e);
		}
	}

	protected final void keyReleased(int keyCode) {
		try {
			inputKeyCode=INPUT_NONE;
			int input = getInput(keyCode);
			if ((inputReleased == INPUT_NONE) || (input != INPUT_UNKNOWN)) {
				if (inputHolding == input) {
					inputHolding = INPUT_NONE;
				}
				inputReleased = input;
			}
		} catch (RuntimeException e) {
			showError(e);
		}
	}

	private int getInput(int keyCode) {
		int valueOK = SOFTKEY_VALUE_LEFT;
		int valueCancel = SOFTKEY_VALUE_RIGHT;

		if (softkeyOK == SOFTKEY_OK) {
			#if(${ant.handset_reverseok})
			valueOK = SOFTKEY_VALUE_RIGHT;
			valueCancel = SOFTKEY_VALUE_LEFT;
			#end
		}

		if (softkeyOK == SOFTKEY_MENU) {
			#if(${ant.handset_reversemenu})
			valueOK = SOFTKEY_VALUE_RIGHT;
			valueCancel = SOFTKEY_VALUE_LEFT;
			#end
		}

		if (keyCode == valueOK) {
			return INPUT_OK;
		}

		if (keyCode == valueCancel) {
			return INPUT_CANCEL;
		}

		int action = getGameAction(keyCode);

		switch (action) {
		case UP:
			return INPUT_UP;

		case DOWN:
			return INPUT_DOWN;

		case LEFT:
			return INPUT_LEFT;

		case RIGHT:
			return INPUT_RIGHT;

		case FIRE:
			return INPUT_FIRE;
		}

		switch (keyCode) {
		case Canvas.KEY_STAR:
			return INPUT_STAR;

		case Canvas.KEY_POUND:
			return INPUT_POUND;
		}

		if ((keyCode >= Canvas.KEY_NUM0) && (keyCode <= Canvas.KEY_NUM9)) {
			return INPUT_NUM0 + (keyCode - Canvas.KEY_NUM0);
		}

		return INPUT_UNKNOWN;
	}

	// ************************************
	// command
	// ************************************

	#if(${ant.handset_command})
	private Command commandLeft;

	private Command commandRight;

	public final void commandAction(Command c, Displayable d) {
		if (c.equals(commandLeft)) {
			if (softkeyOK == SOFTKEY_OK) {
				#if(${ant.handset_reverseok})
				inputPressed = INPUT_CANCEL;
				return;
				#end
			}

			if (softkeyOK == SOFTKEY_MENU) {
				#if(${ant.handset_reversemenu})
				inputPressed = INPUT_CANCEL;
				return;
				#end
			}

			inputPressed = INPUT_OK;
		}

		if (c.equals(commandRight)) {
			if (softkeyOK == SOFTKEY_OK) {
				#if(${ant.handset_reverseok})
				inputPressed = INPUT_OK;
				return;
				#end
			}

			if (softkeyOK == SOFTKEY_MENU) {
				#if(${ant.handset_reversemenu})
				inputPressed = INPUT_OK;
				return;
				#end
			}

			inputPressed = INPUT_CANCEL;
		}
	}

	#end

	// ************************************
	// softkey
	// ************************************

	public static final int SOFTKEY_NONE = -1;

	public static final int SOFTKEY_OK = 0;

	public static final int SOFTKEY_BACK = 1;

	public static final int SOFTKEY_MENU = 2;
	public static final int SOFTKEY_PAUSE = 3;

	#if(${ant.handset_nokia})
	private static final int SOFTKEY_VALUE_LEFT = FullCanvas.KEY_SOFTKEY1;

	private static final int SOFTKEY_VALUE_RIGHT = FullCanvas.KEY_SOFTKEY2;

	#else
	private static final int SOFTKEY_VALUE_LEFT = ${ant.handset_softleft};
	private static final int SOFTKEY_VALUE_RIGHT = ${ant.handset_softright};
	#end

	private int softkeyOK;

	private int softkeyCancel;

	protected final void setSoftkey(int ok, int cancel) {
		softkeyOK = ok;
		softkeyCancel = cancel;
	}

	// ************************************
	// state
	// ************************************

	private static final int STATE_NONE = -1;

	private static final int STATE_START = 0;

	public static final int STATE_LOGO_ONE = 1;

	public static final int STATE_LOGO_TWO = 2;

	public static final int STATE_TITLE = 10;

	public static final int STATE_MENU_MAIN = 11;

	public static final int STATE_MENU_HELP = 12;

	public static final int STATE_OPTIONS = 19;

	public static final int STATE_MESSAGE = 20;

	public static final int STATE_GAME_START = 60;

	public static final int STATE_GAME_END = 99;

	protected int state; // current state

	private int statePaint;

	protected final void setState(int state) {
		statePaint = STATE_NONE;

		stateLeave();

		System.gc();

		this.state = state;

		stateEnter();

		statePaint = state;

		repaint = true;
		repaintAll = true;
	}

	protected void stateEnter() {
		switch (state) {
		case STATE_START:
			enterStart();
			break;

⌨️ 快捷键说明

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