写了一个用于调试midlet的canvas,用来输出调试信息,可以和Form一样append字符串。 以前用form现实总是更新不及时,即时重新setCurrent,现在好多了。
debug_canvas.java
import java.io.*;
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class debug_canvas extends Canvas
{
/*-----------------------
key code.
---------------------------*/
public static final int KEY_UP = -1;
public static final int KEY_DOWN = -2;
public static final int KEY_LEFT = -3;
public static final int KEY_RIGHT = -4;
public static final int KEY_SELECT = 8;
public static final int KEY_SOFT1 = -20;
public static final int KEY_SOFT2 = -21;
public static final int KEY_SEND = -14;
public static final int KEY_CLEAR = -53;
public static final int KEY_SIDE_UP = -51;
public static final int KEY_SIDE_DOWN = -52;
public static final int KEY_VOICE_COMMAND = -30;
private int ScrW, ScrH;
private Font font;
/*-----------------------
text window.
---------------------------*/
/**
* console size.
*/
private int csWidth, csHeight;
/**
* Strings to display.
*/
public StringBuffer debug_strs = null;
/**
* Strings in line format.
*/
private Vector strs = null;
/**
* top line on the screen.
*/
private int topLine;
/**
* max line count on the screen
*/
private int maxLine;
/**
* max characters in line
*/
private int maxChar;
/*----------------------------------------
Scroll bar.
Scroll bar resides right side of screen.
width = 2, height = scrH.
------------------------------------------*/
public debug_canvas()
{
ScrW = getWidth();
ScrH = getHeight();
font = Font.getFont( Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM );
csWidth = ScrW - 2;
csHeight = ScrH;
topLine = 0;
maxLine = csHeight / font.getHeight();
maxChar = csWidth / font.stringWidth("A");
System.out.println(">>maxChar:" + maxChar);
debug_strs = new StringBuffer(100);
strs = new Vector();
}
public void append( String s )
{
if ( s == null || s.length() == 0 )
return;
debug_strs.append(s);
/********************
append to strs.
*********************/
StringBuffer aline = new StringBuffer( maxChar+1 );
if ( strs.size() > 0 )
{
String last = (String)strs.lastElement();
if ( last.charAt( last.length() - 1 ) != '/n' )
{
aline.append( last );
strs.removeElementAt( strs.size() - 1 );
}
}
int si = 0;
while ( si < s.length() )
{
// if ( aline.length() == maxChar )
// {
// // soft wrap
// if ( s.charAt(si) == '/n' )
// {
// aline.append('/n');
// ++si;
// }
// strs.addElement( aline.toString() );
// aline = new StringBuffer( maxChar+1 );
// }
if ( font.stringWidth(aline.toString()) > csWidth )
{
// soft wrap
char ec = aline.charAt(aline.length()-1);
String line = aline.toString();
if ( ec == '/n' )
{
strs.addElement( line );
aline = new StringBuffer( maxChar+1 );
}
else
{
strs.addElement( line.substring(0, line.length()-1) );
aline = new StringBuffer( maxChar+1 );
aline.append( ec );
}
}
else if ( s.charAt(si) == '/n' )
{
// hard wrap
aline.append('/n');
++si;
strs.addElement( aline.toString() );
aline = new StringBuffer( maxChar+1 );
}
else
{
// no wrap
aline.append( s.charAt(si++) );
}
}//while ( si < s.length() )
if ( aline.length() > 0 )
{
strs.addElement( aline.toString() );
}
repaint();
}
public void paint( Graphics g )
{
g.setFont( font );
g.setColor( 0x000000 );
g.fillRect( 0, 0, ScrW, ScrH );
g.setColor( 0xffffff );
if ( debug_strs.length() == 0 )
{
g.drawString( "<< Nothing >>", 0, 0, 0 );
}
else
{
int y = 0;
int cur = topLine;
while( cur - topLine < maxLine && cur < strs.size() )
{
String line = (String)strs.elementAt(cur++);
g.drawString( line, 0, y, Graphics.TOP|Graphics.LEFT );
y += font.getHeight();
}
/* SCROLL BAR */
if ( strs.size() <= maxLine )
{
// no scroll bar
g.drawLine( ScrW-1, 0, ScrW-1, ScrH-1 );
}
else
{
int barLen = maxLine * ScrH / strs.size();
y = topLine * (ScrH - barLen) / ( strs.size() - maxLine );
if ( y < 0 ) y = 0;
if ( y > ScrH - barLen ) y = ScrH - barLen;
g.drawLine( ScrW-1, y, ScrW-1, y+barLen );
}
}
}
public void keyPressed( int keyCode )
{
if ( strs.size() > maxLine )
{
if ( keyCode == KEY_UP )
{
if ( topLine > 0 ) {
--topLine;
repaint();
}
}
else if ( keyCode == KEY_NUM2 )
{
if ( topLine > 0 ) {
topLine -= maxLine/2;
if ( topLine < 0 ) topLine = 0;
repaint();
}
}
else if ( keyCode == KEY_DOWN )
{
if ( topLine < strs.size()-maxLine ){
++topLine;
repaint();
}
}
else if( keyCode == KEY_NUM8 )
{
if ( topLine < strs.size()-maxLine ) {
topLine += maxLine/2;
if ( topLine > strs.size()-maxLine )
topLine = strs.size()-maxLine;
repaint();
}
}
}
}
}/*END_OF_CLASS*/