当前位置:编程学习 > JAVA >>

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

【问题描述】在JavaMe连载(3)-也说MVC设计模式 一文中提到了一个TextEdit类,但没有给出具体实现,TextEdit是采用GameCanvas绘制的文本编辑器。本文结合实例给出实现的方法。

 

【原理】

1 运用Graphics、GameCanvas绘制文本框和光标。

2 检测到输入事件时,跳转到 高级界面->TextBox 。通过系统调用输入法完成输入。

3 将TextBox输入的值返回给TextEdit对象。

 

【设计模式】

这个过程有点类似装饰模式,实际上,实现输入的还是TextBox,只是给TextBox装饰了一下,形成了一个漂亮的外观。

 

【代码清单】

TextEdit.java

[html]  package com.token.view.components; 
 
import javax.microedition.lcdui.Font; 
import javax.microedition.lcdui.Graphics; 
import javax.microedition.lcdui.game.GameCanvas; 
 
public class TextEdit extends GameCanvas 

    private Font ft; 
     
    public int width; 
    public int height; 
     
    public TextEdit(GameCanvas canvas)  
    { 
        super(false); 
         
    } 
     
    //绘制文本框 
    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 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); 
        } 
    } 

package com.token.view.components;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

public class TextEdit extends GameCanvas
{
 private Font ft;
 
 public int width;
 public int height;
 
 public TextEdit(GameCanvas canvas)
 {
  super(false);
  
 }
 
 //绘制文本框
 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 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);
  }
 }
}
 

PopUpTextBox.java

[html] package com.token.view; 
 
import javax.microedition.lcdui.Command; 
import javax.microedition.lcdui.CommandListener; 
import javax.microedition.lcdui.Displayable; 
import javax.microedition.lcdui.TextBox; 
 
import com.token.util.UIController; 
 
public class PopUpTextBox extends TextBox { 
 
      private UIController controller; 
      protected String canvasText = ""; 
      private Command okCommand; 
      private Command cancelCommand; 
      private String object_name = null; 
      private String editor = null; 
 &nbs

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,