📄 logcanvas.java
字号:
package opusmicro.demos.game;
import java.util.Vector;
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;
public class LogCanvas extends Canvas implements CommandListener{
private Font font= Font.getDefaultFont();
private int gintBeginIndex = 0;
private int gintLineHeight = font.getHeight();
private int w = getWidth();
private int h = getHeight();
private int scrollbarwidth = 6;
private Command back;
private MoveCanvas mc;
private MoveMIDlet md;
private Vector vector = new Vector();
Vector logs = new Vector();
private StringBuffer sb = new StringBuffer();
public static final byte DEBUG = 11;
public static final byte INFO = 12;
public static final byte ERROR = 13;
public static final byte FETAL = 14;
public LogCanvas(MoveCanvas mc, MoveMIDlet md) {
this.mc = mc;
this.md = md;
init();
}
private void init() {
back = new Command("Back",Command.BACK,1);
addCommand(back);
setCommandListener(this);
}
protected void keyPressed(int keycode) {
int code = getGameAction(keycode);
switch ( code) {
case UP:
if ( gintBeginIndex > 0) {
gintBeginIndex--;
}
else {
gintBeginIndex = 0;
break;
}
repaint();
break;
case DOWN:
// System.out.println("size = "+vector.size());
if ( vector.size() * gintLineHeight <= h
|| (vector.size() - gintBeginIndex) * gintLineHeight < h) {
break;
}
gintBeginIndex++;
repaint();
break;
}
}
private String getDebugMsg(){
sb.delete(0, sb.length());
for(int i=0;i<logs.size();i++){
sb.append(logs.elementAt(i));
}
return sb.toString();
}
public void log(int type, String msg) {
try {
String message = null;
switch(type){
case DEBUG:
message = "[DEBUG] "+Utils.getTimeZone()+" "+msg+"\n";
break;
case INFO:
message = "[INFO] "+Utils.getTimeZone()+" "+msg+"\n";
break;
case ERROR:
message = "[ERROR] "+Utils.getTimeZone()+" "+msg+"\n";
break;
case FETAL:
message = "[FETAL] "+Utils.getTimeZone()+" "+msg+"\n";
break;
}
logs.insertElementAt(message, 0);
if ( logs.size()>20)
logs.removeElementAt(logs.size()-1);
}
catch (Exception e) {
System.out.println("in debug() error!");
e.printStackTrace();
}
}
protected void paint(Graphics g) {
// System.out.println("font height "+font.getHeight());
g.setColor(-1);
g.fillRect(0, 0, w, h);
g.setColor(0);
vector.removeAllElements();
vector = Utils.getSubsection(getDebugMsg(), font, w - scrollbarwidth-5, " ,.?!");
for ( int i = gintBeginIndex ; i < vector.size() ; i++) {
g.drawString((String) vector.elementAt(i), 1, gintLineHeight * (i - gintBeginIndex), Graphics.LEFT |Graphics.TOP);
if ( (i - gintBeginIndex + 1) * gintLineHeight > h) {
break;
}
}
if ( (vector.size()) * gintLineHeight > h) {
double position = Math.floor(((vector.size() + 1) * gintLineHeight - h+ .5) / gintLineHeight );
double scrollbarheight = Math.floor((h + .8) / (position +1));
int scrollbary = (int) Math.floor(scrollbarheight * (gintBeginIndex) + .8);
g.setColor(0);
g.fillRect(w - scrollbarwidth+1, scrollbary, scrollbarwidth, (int) scrollbarheight);
}
}
public void commandAction(Command c, Displayable d) {
if(c==back){
gintBeginIndex = 0;
md.display.setCurrent(mc);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -