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

📄 mytextbox.java

📁 利用j2me所开发的一个角色扮演类游戏
💻 JAVA
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class MyTextBox extends Canvas implements Runnable, CommandListener {

	private final static int KEY_CLEAR = -8;
	private final static int MAX_NUM = 10;
	private final static int KEY_LEFT = -3;
	private final static int KEY_RIGHT = -4;

	private char[] m_text = new char[10];
	private int currentCursor = 0;
	private int previouseCursor = 0;
	private int currentKey = -1;
	private int keyStatus = 0;
	private int offPos = 0;
	private int currentLen = 0;
	private boolean showCursor = true;
	private int left;
	private int top;
	private Command com_Ok;
	private Command com_Return;
	private GameMain gameMain;
	Score score;
	int spriteScore;
	private boolean isPlay;

	final static protected char[][] CHAR_OPTIONS = {
			{ '0', '+' },
			{ '1', '.', '-', '\'', '+', '!', '\"', '*', '#', '$', '%', '(',
					')', '@', '&', '_', '~', '`', '^', '=', '\\', '|', ',',
					'/', '?', '<', '>', ':', ';', '[', ']' },
			{ 'A', 'B', 'C', 'a', 'b', 'c', '2' },
			{ 'D', 'E', 'F', 'd', 'e', 'f', '3' },
			{ 'G', 'H', 'I', 'g', 'h', 'i', '4' },
			{ 'J', 'K', 'L', 'j', 'k', 'l', '5' },
			{ 'M', 'N', 'O', 'm', 'n', 'o', '6' },
			{ 'P', 'Q', 'R', 'S', 'p', 'q', 'r', 's', '7' },
			{ 'T', 'U', 'V', 't', 'u', 'v', '8' },
			{ 'W', 'X', 'Y', 'Z', 'w', 'x', 'y', 'z', '9' }, { ' ' } };

	final static protected int[] KEY_OPTIONS = { KEY_NUM0, KEY_NUM1, KEY_NUM2,
			KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8,
			KEY_NUM9, KEY_POUND };

	public MyTextBox(int left, int top, GameMain game, Score score,
			int spriteScore) {
		isPlay=true;
		this.gameMain = game;
		this.score = score;
		this.spriteScore = spriteScore;
		try {
			score.loadScores();
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.left = left;
		this.top = top;
		for (int i = 0; i < 10; i++) {
			m_text[i] = '\0';
		}
		com_Ok = new Command("确定", Command.OK, 0);
		com_Return = new Command("返回", Command.EXIT, 0);
		this.addCommand(com_Ok);
		this.addCommand(com_Return);
		this.setCommandListener(this);
	}
	public void setStart(int spriteScore){
		this.spriteScore = spriteScore;
		for (int i = 0; i < 10; i++) {
			m_text[i] = '\0';
		}
		currentLen=0;
		currentCursor=0;
		repaint();
	}
	protected void paint(Graphics g) {
		g.setColor(00, 00, 00);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		g.setColor(255, 255, 255);
		g.drawImage(GameCenter.createImage("/gaofen.png"), 0, 0, 0);
//		g.fillRect(left, top, 112, 20);
//		g.drawString("请输入姓名:",0, 60, 0);
		
//		drawBox(g);
		drawChar(g);
		if (showCursor)
			drawCursor(g);
	}
	private void drawBox(Graphics g) {
		g.setColor(0, 0, 0);
		g.drawRect(left, top, 110, 18);
	}
	private void drawChar(Graphics g) {
		int lt = left + 4;
		int tp = top + 2;
		g.setColor(0, 0, 255);
		Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
				Font.SIZE_MEDIUM);
		g.setFont(font);
		int fw;
		for (int i = 0; i < 10; i++) {
			if (m_text[i] == '\0')
				break;
			fw = font.stringWidth(m_text[i] + "");
			g.drawString(m_text[i] + "", lt, tp, Graphics.LEFT | Graphics.TOP);
			lt = lt + fw + 2;
		}
	}
	private void drawCursor(Graphics g) {
		Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
				Font.SIZE_MEDIUM);
		int lt = left + 4;
		g.setColor(0, 0, 255);
		for (int i = 0; i < currentCursor; i++) {
			lt = lt + font.stringWidth(m_text[i] + "") + 2;
		}
		lt = lt - 2;
		g.drawLine(lt, top + 4, lt, top + 15);
	}
	private void rightCursor() {
		if (currentCursor >= currentLen)
			return;
		currentCursor++;
		currentKey = -1;
	}
	private void leftCursor() {
		if (currentCursor <= 0)
			return;
		currentCursor--;
		currentKey = -1;
	}
	private void deleteChar() {
		if (currentCursor <= 0)
			return;
		if (currentCursor > currentLen)
			return;
		System.arraycopy(m_text, currentCursor, m_text, currentCursor - 1,
				currentLen - currentCursor);
		m_text[currentLen - 1] = '\0';
		currentLen--;
		currentCursor--;
		if (currentCursor < 0)
			currentCursor = 0;
		if (currentLen < 0)
			currentLen = 0;
	}
	private void insertChar(char ch, boolean mode) {
			if (mode) {
			if (currentLen >= MAX_NUM)
				return;
			System.arraycopy(m_text, currentCursor, m_text, currentCursor + 1,
					currentLen - currentCursor);
			m_text[currentCursor] = ch;
			previouseCursor = currentCursor;
			currentCursor++;
			currentLen++;
		} else {
			m_text[previouseCursor] = ch;
		}
	}

	protected int getKeyOption(int keyCode) {
		int l = KEY_OPTIONS.length;
		for (int keyIndex = 0; keyIndex < l; keyIndex++) {
			if (KEY_OPTIONS[keyIndex] == keyCode)
				return keyIndex;
		}
		return -1;
	}

	private char getKeyCodeChar(int keyCode, boolean mode) {
		int keyIndex = getKeyOption(keyCode);
		if (keyIndex == -1)
			return '\0';
		if (mode) {
			offPos++;
			int len = CHAR_OPTIONS[keyIndex].length;
			if (offPos >= len)
				offPos %= len;
		} else {
			offPos = 0;
		}
		char ch = CHAR_OPTIONS[keyIndex][offPos];
		return ch;
	}
	public void run() {
		while (isPlay) {
			try {
				Thread.sleep(500);
				showCursor = !showCursor;
			} catch (Exception ex) {

			}
			synchronized (this) {
				keyStatus++;
			}
			repaint();
		}
	}
	public void keyPressed(int keyCode) {

		switch (keyCode) {
		case KEY_LEFT:
			leftCursor();
			break;
		case KEY_RIGHT:
			rightCursor();
			break;
		case KEY_CLEAR:
			deleteChar();
			break;
		case KEY_NUM0:
		case KEY_NUM1:
		case KEY_NUM2:
		case KEY_NUM3:
		case KEY_NUM4:
		case KEY_NUM5:
		case KEY_NUM6:
		case KEY_NUM7:
		case KEY_NUM8:
		case KEY_NUM9:

			boolean b;
			if (currentKey == keyCode && keyStatus <= 1) {
				b = true;
			} else {
				if (currentLen >= MAX_NUM) {
					return;
				}
				b = false;
			}
			char ch = getKeyCodeChar(keyCode, b);
			insertChar(ch, !b);

			synchronized (this) {
				keyStatus = 0;
			}
			break;
		}
		repaint();
		currentKey = keyCode;
	}

	public void commandAction(Command c, Displayable d) {
		if (c.getCommandType() == Command.OK) {
			try {
				String temps = "";
				for (int i = 0; i < 10; i++) {
					if (m_text[i] != '\0') {
						temps = temps + m_text[i];
					}
					else{
						break;
					}
				}
				score.updateScores(this.spriteScore, temps);
				isPlay=false;
				if(GameLogo.gameMenu != null){
					GameLogo.gameMenu.setMenuSelect(1);
				}				
				gameMain.display.setCurrent(GameLogo.gameMenu);
			} catch (Exception e) {				
				e.printStackTrace();
			}
		}


	}

}

⌨️ 快捷键说明

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