使用canvas画的一个登录的Form

<!–code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }– 使用canvas也就是通常说的低级组件(low-level user interface api)画了一个登录的form,这个用high-level的form很简单,但是自己写确实很麻烦。因为所有的控制都要自己写:键盘输入(输入框),组件绘制,动画线程(光标闪烁), 输入框长度控制, 组件焦点, 密码mask. 刚刚开始接触wtk,练习一下.

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;
import javax.microedition.lcdui.Image;
public class LoginForm extends Canvas implements CommandListener {

private Image img;
private Font font;
public int currentPosX, currentPosY;
public int width, height;
public int inputBoxWidth = 150;

public int offset = 5;
public int curX = 0, curY = 0;

static final int USER_INPUT = 1, PASS_INPUT = 2, REMEMBER_CHECK = 3;

public String username = “”, password = “”;
public String usernameShow = “”,passwordShow=“”;//the display part of these two fields
public int userInputLimitation = 0,passInputLimitation = 0; //characters limitation of input box

public int userInputX, userInputY;
public int passInputX, passInputY;
public int remeCheckX, remeCheckY;
public int curSel = USER_INPUT;

public int cursorX,cursorY;
public int cursorColor = 0x00000000;
public CursorBlink cursorBlink;

private StringBuffer input = new StringBuffer(“default input”);
boolean initialized = false;

public LoginForm() {
setCommandListener(this);
width = getWidth();
height = getHeight();
// The current cursor position for the text
currentPosX = width / 2;
currentPosY = height / 2;
// The text font
font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,
Font.SIZE_LARGE);

// setFullScreenMode (true);
// Cursor blink thread
cursorBlink = new CursorBlink(this);
Thread blinkThrd = new Thread(cursorBlink);
blinkThrd.start();

setCommandListener(this);

try {
img = Image.createImage(“/imgs/” + “gtalk.jpg”);
} catch (Exception e) {
e.printStackTrace();
img = null;
}
}

protected void paint(Graphics g) {
if (!initialized) {
// Fill a white background
g.setColor(0xffffff);

g.fillRect(0, 0, width, height);
// Draw logo image
g.drawImage(img, curX, curY, Graphics.TOP | Graphics.LEFT);

// Draw form
// set font and color-black
g.setFont(font);
g.setColor(0, 0, 0);
// Draw name label and box
curY += img.getHeight() + offset;
g.drawString(“Username “, curX, curY, Graphics.TOP | Graphics.LEFT);
curY += offset + font.getHeight();
userInputX = curX;
userInputY = curY;
g.drawRect(curX, curY, inputBoxWidth, font.getHeight());
curY += offset / 2 + font.getHeight();
g
.drawString(“@gmail.com”, curX, curY, Graphics.TOP
| Graphics.LEFT);
// Draw password label and box
curY += offset + font.getHeight();
g.drawString(“Password “, curX, curY, Graphics.TOP | Graphics.LEFT);
curY += offset + font.getHeight();
passInputX = curX;
passInputY = curY;
g.drawRect(curX, curY, inputBoxWidth, font.getHeight());

cursorX = userInputX;
cursorY = userInputY;

initialized =true;
}else{
update(g);
}

}

public void update(Graphics g) {
highLightSelection(g);
//remove previous input
g.setColor(255, 255, 255);
//1px offset for the 1px border
g.fillRect(userInputX+1, userInputY+1, inputBoxWidth-1, font.getHeight()1);
g.fillRect(passInputX+1, passInputY+1, inputBoxWidth-1, font.getHeight()1);
//draw the input again
drawInput(g);
showCursor(g);
}

private void drawInput(Graphics g) {
g.setColor(0,0,0);
g.setFont(font);
usernameShow = username;
//add a blank space to avoid the char cross line, as the space available is not enough for a char like “k”, but enough for “l”
if(font.stringWidth(usernameShow+” “) >= inputBoxWidth){
if(userInputLimitation == 0) userInputLimitation = username.length();
usernameShow = username.substring(username.length()-userInputLimitation);
}
passwordShow = mask(password);
if(font.stringWidth(passwordShow+” “) >= inputBoxWidth){
if(passInputLimitation == 0) passInputLimitation = password.length();
passwordShow = password.substring(password.length()-passInputLimitation);
}

g.drawString(usernameShow, userInputX, userInputY, Graphics.TOP
| Graphics.LEFT);
g.drawString(mask(passwordShow), passInputX, passInputY, Graphics.TOP
| Graphics.LEFT);
}

private void showCursor(Graphics g) {
g.setColor(cursorColor);
if(curSel == USER_INPUT){
g.drawLine(userInputX+font.stringWidth(usernameShow),userInputY+1,userInputX+font.stringWidth(usernameShow),userInputY+font.getHeight()1);
}else if(curSel == PASS_INPUT){
g.drawLine(passInputX+font.stringWidth(mask(passwordShow)),passInputY+1,passInputX+font.stringWidth(mask(passwordShow)),passInputY+font.getHeight()1);
}

}

private String mask(String pass){
StringBuffer sb = new StringBuffer();
int le = pass.length();
for(int i=0;i<le;i++){
sb.append(‘*’);
}
return sb.toString();
}

private void highLightSelection(Graphics g) {
// set blue background
g.setColor(0, 0, 255);
if (curSel == USER_INPUT) {
g.drawRect(userInputX, userInputY, inputBoxWidth, font.getHeight());
g.setColor(0, 0, 0);
g.drawRect(passInputX, passInputY, inputBoxWidth, font.getHeight());

} else if (curSel == PASS_INPUT) {
g.drawRect(passInputX, passInputY, inputBoxWidth, font.getHeight());
g.setColor(0, 0, 0);
g.drawRect(userInputX, userInputY, inputBoxWidth, font.getHeight());

}
}

public void keyPressed(int keyCode) {
int gameCode = getGameAction(keyCode);
if (gameCode == UP) {
curSel = USER_INPUT;
} else if (gameCode == DOWN) {
curSel = PASS_INPUT;
} else if (gameCode == LEFT) {
//TODO move cursor
} else if (gameCode == RIGHT) {
//TODO move cursor
} else if (keyCode == –8 || keyCode == 8) { // -8 clear key, 8 backspace
if (curSel == USER_INPUT) {
if(“”.equals(username))return;
username = username.substring(0, username.length()1);
} else if (curSel == PASS_INPUT) {
if(“”.equals(password))return;
password = password.substring(0,password.length()1);
}
} else {
char c = (char) keyCode;
if (curSel == USER_INPUT) {
username += c;
} else if (curSel == PASS_INPUT) {
password += c;
}
}
repaint();
}

public void commandAction(Command c, Displayable d) {
System.out.println(c);
}

class CursorBlink implements Runnable {
LoginForm form;

public CursorBlink (LoginForm form) {
this.form = form;
}
public void run () {
int seed=0;
while (true) {
seed++;
if( seed%2 == 0){
cursorColor = 0x00ffffff; //white
}else{
cursorColor = 0x00000000; //black
}
form.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}