JavaMe连载(5)-绘制文本框TextEdit (十一)

2014-11-24 07:56:21 · 作者: · 浏览: 11
or,username,passwd,passwd_re};
controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT,args);
break;
}
default:;
}

keyCode = getGameAction(keyCode);
switch(keyCode)
{
case UP:
case LEFT:
{
currentlySelectedIndex--;
if(currentlySelectedIndex<0)
{
currentlySelectedIndex=0;
}
else
{
redraw();
}
break;
}
case DOWN:
case RIGHT:
{
currentlySelectedIndex++;
if(currentlySelectedIndex>2)
{
currentlySelectedIndex=2;
}
else
{
redraw();
}

break;
}
}
}

}

【分析】

1 文本框的绘制(TextEdit.java)

需要传递GameCanvas、Graphics对象,实现绘图,策略是谁使用,谁传递该参数。此外需要床底文本框左上角坐标(x,y)以及控制光标闪烁的变量cursorBlinkOn。

[html] public void drawTextBox(GameCanvas canvas, Graphics graphics, String text, int x, int y, boolean cursorBlinkOn)
{
//System.out.println("draw");
int padding = 4;
int margin = 2;

ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
graphics.setFont(ft);

width = 3*canvas.getWidth()/5+2*padding;
height = ft.getHeight()+2*padding;

graphics.setColor(Color.frame);
graphics.fillRect(x+1,y+1,width+margin,height+margin);

graphics.setColor(Color.frameBg);
graphics.drawRect(x, y,width, height);
graphics.setColor(Color.background);
graphics.fillRect(x+1, y+1,width-1,height-1);

graphics.setColor(Color.text);
graphics.drawString(text, x+padding, y+padding, Graphics.TOP|Graphics.LEFT);

drawCursor(graphics, x+ft.stringWidth(text)+padding, y+padding, 1, ft.getHeight(), cursorBlinkOn);

canvas.flushGraphics(x,y,width,height);
}
public void drawTextBox(GameCanvas canvas, Graphics graphics, String text, int x, int y, boolean cursorBlinkOn)
{
//System.out.println("draw");
int padding = 4;
int margin = 2;

ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
graphics.setFont(ft);

width = 3*canvas.getWidth()/5+2*padding;
height = ft.getHeight()+2*padding;

graphics.setColor(Color.frame);
graphics.fillRect(x+1,y+1,width+margin,height+margin);

graphics.setColor(Color.frameBg);
graphics.drawRect(x, y,width, height);
graphics.setColor(Color.background);
graphics.fillRect(x+1, y+1,width-1,height-1);

graphics.setColor(Color.text);
graphics.drawString(text, x+padding, y+padding, Graphics.TOP|Graphics.LEFT);

drawCursor(graphics, x+ft.stringWidth(text)+padding, y+padding, 1, ft.getHeight(), cursorBlinkOn);

canvas.flushGraphics(x,y,width,height);
}

2 绘制光标(TextEdit.java)

[html] public void drawCursor(Graphics graphics, int x, int y, int width, int height, boolean cursorBlinkOn)
{
if(cursorBlinkOn)
{
ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
graphics.setFont(ft);
graphics.setColor(0x0,0x0,0x0);
graphics.drawLine(x+width,y,x+width,y+height);
}
}
public void drawCursor(Graphics graphics, int x, int y, int width, int height, boolean cursorBlinkOn)
{
if(cursorBlinkOn)
{
ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
graphics.setFont(ft);
graphics.setColor(0x0,0x0,0x0);
gra