Canvas used to adjust the MIDlet

Source: Internet
Author: User

A canvas used to debug the MIDlet is written to output debugging information. It can be the same as the form append string. In the past, the reality of form was always not updated in a timely manner, and it was much better now.

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 ("");
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 (0 xffffff );
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 */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.