📄 debug.java
字号:
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.rms.RecordStore;
import javax.microedition.media.*;
public class Debug
{
private static final boolean NEED_PRINT_INFO = true;
private static final boolean NEED_PRINT_EXCEPTION = true;
private static final boolean NEED_ASSERT = true;
private static final boolean NEED_ANALYSE_MEM = true;
/**
* Print a string and then terminate the line.
* @param str String the string to be printed
*/
public static void Println(String str)
{
if (NEED_PRINT_INFO)
System.out.println(str);
}
/**
* Print an integer and then terminate the line.
* @param value int the integer to be printed
*/
public static void Println(int value)
{
if (NEED_PRINT_INFO)
System.out.println(value);
}
/**
* Print stack trace of an exception.
* @param e Exception the exception to be printed
*/
public static void PrintException(Exception e)
{
if (NEED_PRINT_EXCEPTION)
e.printStackTrace();
}
/**
* Assert that an expression is true. If not, it will report an error by printing error information and location.<br>
* It is used always under debug flag, to make sure logical rules and provide a perfect safe.
* @param expr boolean the expression to be verified
* @param errorInfo String printed error information
*/
public static void Assert(boolean expr, String errorInfo)
{
if (NEED_ASSERT)
{
if (!expr)
{
try
{
throw new Exception(errorInfo);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
/**
* The occupied memory last recorded.
*/
private static long s_recordedMem = 0;
/**
* Print current memory information, including current occupied memory size,
* and consumed memory contrast since last call of this function.
* @param comment String the note of this statistic
*/
public static void Mem(String comment)
{
if (NEED_ANALYSE_MEM)
{
long mem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
System.out.println(comment + " current: " + mem + " consumed since last: " + (mem - s_recordedMem));
s_recordedMem = mem;
}
}
/**
* Draw rectangles to mark the screen and viewport area.<br>
* It can be used to avoid extra drawing outside area.
*/
public static void MarkScreenAndViewport(Graphics g)
{
Donghua.SetClip(g, 0, 0, PINGMUDDD.PINGMUKUAN, PINGMUDDD.PINGMUGAO);
g.setColor(0xFF0000);
g.drawRect(0, 0, PINGMUDDD.PINGMUKUAN-1, PINGMUDDD.PINGMUGAO-1);
g.setColor(0x00FFFF);
g.drawRect(PINGMUDDD.sssCHUANNN_LEFT, PINGMUDDD.sssCHUANNN_TOP, PINGMUDDD.sssCHUANNN_WIDTH-1, PINGMUDDD.sssCHUANNN_HEIGHT-1);
}
/**
* Create an immutable image from decoded image data obtained from the named resource.
* @param name String the name of the resource containing the image data in one of the supported image formats
* @return Image the created image
*/
public static Image CreateImage(String name)
{
try
{
return Image.createImage(name);
}
catch (Exception e)
{
PrintException(e);
return null;
}
}
/**
* Get parameter value by the specified parameter name from file.<br>
* The file is organized by lines of format "[Name]=[Value]", such as <br>
* <pre>
* hp=200
* mp=150
* speed=6
* </pre>
* @param fileName String the name of the file who contains parameters
* @param paramName String the specified parameter name
* @return String the required parameter value
*/
public static String GetParamValue(String fileName, String paramName)
{
try
{
InputStreamReader isr = new InputStreamReader(fileName.getClass().getResourceAsStream(fileName));
StringBuffer strbuf = new StringBuffer();
String result = null;
byte b;
while ((b = (byte)isr.read()) != -1)
{
if (b == 13)
{
String str = strbuf.toString();
if (str.startsWith(paramName + "="))
{
result = str.substring(str.indexOf("=") + 1);
break;
}
strbuf.setLength(0);
}
else if (b != 10)
strbuf.append((char)b);
}
strbuf = null;
isr.close();
isr = null;
if (result != null)
return result;
else
throw new Exception("param not found with the specified name " + paramName);
}
catch (Exception e)
{
PrintException(e);
return null;
}
}
/**
* Show the FPS and part costed time.<br>
*/
public static long s_time1;
public static long s_time2;
public static long s_time3;
public static long s_time4;
public static long s_time5;
public static long s_time6;
public static long s_timeNow;
public static long s_timeJiaoses;
public static boolean ifShowBG = true;
public static boolean ifShowJiaoses = true;
public static boolean ifShowRate = false;
private static long rateTick;
public static final boolean SHOW_TIME_COST = true;
public static final boolean SWITCH_ACTOR_AND_MAP_DRAWING = false;
public static void ShowRate()
{
// if (Xiyou.IsKeyPressed(Xiyou.GK_SOFT_LEFT))
// ifShowRate = !ifShowRate;
if (ifShowRate)
{
Xiyou.s_g.setClip(0, 0, PINGMUDDD.PINGMUKUAN, PINGMUDDD.PINGMUGAO);
rateTick = System.currentTimeMillis() - rateTick;
long fps = 1000/rateTick;
Xiyou.s_g.setColor(0, 0, 255);
Xiyou.s_g.drawString("F:" + fps, PINGMUDDD.PINGMUKUAN - 40, PINGMUDDD.PINGMUGAO - 40, 0);
if (SHOW_TIME_COST)
{
Xiyou.s_g.drawString("1:" + s_time1, PINGMUDDD.PINGMUKUAN - 80, PINGMUDDD.PINGMUGAO - 160, 0);
Xiyou.s_g.drawString("2:" + s_time2, PINGMUDDD.PINGMUKUAN - 80, PINGMUDDD.PINGMUGAO - 140, 0);
Xiyou.s_g.drawString("3:" + s_time3, PINGMUDDD.PINGMUKUAN - 80, PINGMUDDD.PINGMUGAO - 120, 0);
Xiyou.s_g.drawString("4:" + s_time4, PINGMUDDD.PINGMUKUAN - 80, PINGMUDDD.PINGMUGAO - 100, 0);
Xiyou.s_g.drawString("5:" + s_time5, PINGMUDDD.PINGMUKUAN - 80, PINGMUDDD.PINGMUGAO - 80, 0);
Xiyou.s_g.drawString("6:" + s_time6, PINGMUDDD.PINGMUKUAN - 80, PINGMUDDD.PINGMUGAO - 60, 0);
}
rateTick = System.currentTimeMillis();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -