/* * MWT - Micro Window Toolkit * Copyright (C) 2007 Lucas Domanico - lucazd@gmail.com * * Licensed under the terms of the GNU Lesser General Public License: * http://www.opensource.org/licenses/lgpl-license.php * * For further information visit: * http://j2me-mwt.sourceforge.net/ */ import java.util.Random; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import mwt.Button; import mwt.Component; import mwt.EventListener; import mwt.Font; import mwt.Label; import mwt.Window; class BasicTextBox extends Component { final StringBuffer text = new StringBuffer(); int cursor = 0; int cursorWait; static private int CURSOR_WAIT = 10; static private int[] CURSOR_COLOR = {0xA1C632, 0xCFFF40}; int cursorColor; final Font font; public BasicTextBox(int x, int y, int w, int h, Font font) { super(x,y,w,h,false); this.font = font; } protected boolean keyEvent(long key, Window window) { if((key >> 32) != 0) return true; // if key is not released return if(cursor == text.length()) text.append(' '); // new char (blank) int keycode = (int) key; switch(Example14.canvas.getGameAction(keycode)) { // move cursor case Canvas.RIGHT: cursor++; return true; case Canvas.LEFT: if(cursor > 0) cursor--; return true; } // set char char c = text.charAt(cursor); switch(keycode) { case Canvas.KEY_POUND: case Canvas.KEY_STAR: if(text.length() > 0) { text.deleteCharAt(cursor); if(cursor > 0) cursor--; } return true; case Canvas.KEY_NUM0: if(!Character.isDigit(c) || c == '9') c = '0'; else c++; text.setCharAt(cursor, c); return true; case Canvas.KEY_NUM2: switch(c) { case 'a': c = 'b'; break; case 'b': c = 'c'; break; case 'c': c = 'A'; break; case 'A': c = 'B'; break; case 'B': c = 'C'; break; default: case 'C': c = 'a'; break; } text.setCharAt(cursor, c); return true; case Canvas.KEY_NUM3: switch(c) { case 'd': c = 'e'; break; case 'e': c = 'f'; break; case 'f': c = 'D'; break; case 'D': c = 'E'; break; case 'E': c = 'F'; break; default: case 'F': c = 'd'; break; } text.setCharAt(cursor, c); return true; case Canvas.KEY_NUM4: switch(c) { case 'g': c = 'h'; break; case 'h': c = 'i'; break; case 'i': c = 'G'; break; case 'G': c = 'H'; break; case 'H': c = 'I'; break; default: case 'I': c = 'g'; break; } text.setCharAt(cursor, c); return true; case Canvas.KEY_NUM5: switch(c) { case 'j': c = 'k'; break; case 'k': c = 'l'; break; case 'l': c = 'J'; break; case 'J': c = 'K'; break; case 'K': c = 'L'; break; default: case 'L': c = 'j'; break; } text.setCharAt(cursor, c); return true; case Canvas.KEY_NUM6: switch(c) { case 'm': c = 'n'; break; case 'n': c = 'o'; break; case 'o': c = 'M'; break; case 'M': c = 'N'; break; case 'N': c = 'O'; break; default: case 'O': c = 'm'; break; } text.setCharAt(cursor, c); return true; case Canvas.KEY_NUM7: switch(c) { case 'p': c = 'q'; break; case 'q': c = 'r'; break; case 'r': c = 's'; break; case 's': c = 'P'; break; case 'P': c = 'Q'; break; case 'Q': c = 'R'; break; case 'R': c = 'S'; break; default: case 'S': c = 'p'; break; } text.setCharAt(cursor, c); return true; case Canvas.KEY_NUM8: switch(c) { case 't': c = 'u'; break; case 'u': c = 'v'; break; case 'v': c = 'T'; break; case 'T': c = 'U'; break; case 'U': c = 'V'; break; default: case 'V': c = 't'; break; } text.setCharAt(cursor, c); return true; case Canvas.KEY_NUM9: switch(c) { case 'w': c = 'x'; break; case 'x': c = 'y'; break; case 'y': c = 'z'; break; case 'z': c = 'W'; break; case 'W': c = 'X'; break; case 'X': c = 'Y'; break; case 'Y': c = 'Z'; break; default: case 'Z': c = 'w'; break; } text.setCharAt(cursor, c); return true; } return false; } protected void paint(Graphics g, Window window) { boolean focused = window.getFocus() == this; if(focused) { cursorWait++; if(cursorWait == CURSOR_WAIT) { cursorWait = 0; cursorColor = cursorColor == 0? 1 : 0; } g.setColor(CURSOR_COLOR[cursorColor]); char cursorChar = (cursor == text.length())? ' ' : text.charAt(cursor); int x = font.getWidth(text.toString().substring(0, cursor)); if(cursor > 0) x -= 3; // charspacing g.fillRect(x+2,3,font.getWidth(""+cursorChar),14); } font.write(g, text.toString(), 2, 0, getWidth()-4, getHeight(), Component.ALIGN_BOTTOM_LEFT); g.setColor(0); g.drawRect(0,0,getWidth()-1,getHeight()-1); g.setColor(0xC6C6C6); g.drawRect(1,1,getWidth()-3,getHeight()-3); } } class Canvas14 extends Canvas implements Runnable, EventListener { Window win = new Window(4,4,120,120) { public int getFocusAction(long key) { switch(Example14.canvas.getGameAction((int)key)) { case Canvas.FIRE: return FOCUSACTION_FIRE; case Canvas.UP: case Canvas.LEFT: return FOCUSACTION_PREV; case Canvas.DOWN: case Canvas.RIGHT: return FOCUSACTION_NEXT; default: return FOCUSACTION_NONE; } } }; // the main window -reference- boolean exit; // setted to true to finish the application static final int ACTION_EXIT = 1; // notify input protected void keyPressed(int keyCode) { win.setKeyState(keyCode,Window.KEYSTATE_PRESSED,true); } protected void keyReleased(int keyCode) { win.setKeyState(keyCode,Window.KEYSTATE_RELEASED,true); } // event listener implementation public void processEvent(int eventType, Component sender, Object[] args) { switch(eventType) { case EVENT_ACTION: // when a button is pressed an event action is triggered switch(((Button)sender).getActionType()) { case ACTION_EXIT: exit = true; break; } break; default: break; } } Canvas14() { try { final char[] charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'".toCharArray(); final Image[] images = new Image[charset.length]; for(int i=0; i