📄 netdataform.java
字号:
//package bushfighting;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Canvas;
import com.nokia.mid.ui.FullCanvas;
//NetDataForm用于管理排行榜的上传和下载的工作
public class NetDataForm extends FullCanvas
{
public static final int STATE_MSGDISPLAY = 5; //其他情形,用于显示基本信息,作用等同MsgForm
private int mCurrentState; //当前窗口的状态
protected Score mScore = null;
private String mNickName, mPhone;
protected String mTitle = ""; //消息框的标题
protected String mMessage = ""; //消息框的内容
protected String mRightBar, mLeftBar; //状态栏显示的信息
protected int mCanvasWidth, mCanvasHeight; //屏幕的大小
protected DisplayManager mDisplayManager = null;
private Image mBufferImage = null; //双缓冲的图片
private Graphics mBGraphics = null;
private int mScrollIndex = 0; //为了支持滚动而设置的,标识当前的开始行
private boolean mIsNeedScrolled = false; //当前的显示是否需要滚动
public NetDataForm(DisplayManager manager, Score score, Image bufferImg)
{
mDisplayManager = manager;
mCanvasWidth = getWidth();
mCanvasHeight = getHeight();
mScore = score;
mBufferImage = bufferImg;
mBGraphics = mBufferImage.getGraphics();
mCurrentState = STATE_MSGDISPLAY;
}
//设置当前的窗口状态
public void setState(int state)
{
if (state < 1 || state > 5)
return; //防止一些不正确的状态值
mCurrentState = state;
mIsNeedScrolled = false;
mScrollIndex = 0; //重置当前的滚动行为0
}
//处理按键事件
public void keyPressed(int keyCode)
{
if (keyCode == -6) { //右上角的按键
back(); //返回上一级显示
return;
}
else {
int action = getGameAction(keyCode);
switch (action) {
case DOWN:
if (mIsNeedScrolled == true) {
mScrollIndex = mScrollIndex + 1;
repaint(0, 0, mCanvasWidth, mCanvasHeight);
}
break;
case UP:
if (mScrollIndex > 0) {
mScrollIndex = mScrollIndex - 1;
repaint(0, 0, mCanvasWidth, mCanvasHeight);
}
break;
}
}
}
public void showLocalRolls()
{
mScrollIndex = 0; //重置当前的滚动行为0
mCurrentState = STATE_MSGDISPLAY;
String[] nickNames = mScore.mNickNames;
int[] scores = mScore.mRecordScores;
StringBuffer strBuf = new StringBuffer(60);
for (int i = 0; i < scores.length; i++) {
strBuf.append("第");
strBuf.append(i + 1);
strBuf.append("名 ");
strBuf.append(nickNames[i]);
strBuf.append(" ");
strBuf.append(scores[i]);
if (i != scores.length - 1)
strBuf.append("&");
}
set("本地排行榜", strBuf.toString());
}
//根据不同状态绘制界面
protected void paint(Graphics g)
{
clearScreen(g, HF.COLOR_WHITE);
if (mCurrentState == STATE_MSGDISPLAY) {
drawHeader(g);
drawMessage(g);
setStatusBar("返回", "");
drawStatusBar(g);
}
}
//设置显示的标题和显示的信息
public void set(String title, String msg)
{
mTitle = title;
mMessage = msg;
}
public void setStatusBar(String l_Bar, String r_Bar)
{
mLeftBar = l_Bar;
mRightBar = r_Bar;
}
//绘制标题栏
protected void drawHeader(Graphics g)
{
int color = HF.COLOR_WHITE;
for (int j = 10; j >= 0; j--) {
g.setColor(color);
color -= 0x191900;
g.fillRect(j * (mCanvasWidth / 10), HF.LARGE_FONT.getHeight(), (j + 1) * (mCanvasWidth / 10), 7);
}
g.setColor(HF.COLOR_RED);
g.setFont(HF.LARGE_FONT);
g.drawString(mTitle, mCanvasWidth / 2, 0, Graphics.HCENTER | Graphics.TOP);
}
//绘制信息区,如果需要分行的话,就用&作为分隔符
protected void drawMessage(Graphics g)
{
if (mMessage == null)
return;
g.setFont(HF.MEDIUM_FONT);
g.setColor(HF.COLOR_BLACK);
int offset = HF.MEDIUM_FONT.getHeight();
int x = 2, y = 30;
int pos1 = 0, pos2 = 0;
int count = 0;
do {
pos1 = mMessage.indexOf("&", pos2);
count++;
if (count > mScrollIndex) { //当前位置是需要显示的
if (y >= 0 && y < mCanvasHeight)
g.drawSubstring(mMessage, pos2, pos1 != -1 ? pos1 - pos2 : mMessage.length() - pos2, x, y,
Graphics.LEFT | Graphics.TOP);
y += offset;
}
pos2 = pos1 + 1;
}
while (pos1 != -1);
if (y > mCanvasHeight - 14) {
mIsNeedScrolled = true;
}
else{
mIsNeedScrolled = false;
}
}
//绘制状态栏
public static void drawStatusBar(Graphics g, int barColor, String leftBar, String rightBar, int screenWidth, int screenHeight)
{
int fontHeight = g.getFont().getHeight();
int length = g.getFont().stringWidth(rightBar);
g.setColor(barColor);
g.fillRect(0, screenHeight - fontHeight - 2, screenWidth, fontHeight + 2);
g.setColor(HF.COLOR_BLACK);
g.drawString(leftBar, 1, screenHeight - fontHeight + 1, Graphics.LEFT | Graphics.TOP);
g.drawString(rightBar, screenWidth - length - 4, screenHeight - fontHeight + 1, Graphics.LEFT | Graphics.TOP);
}
protected void drawStatusBar(Graphics g)
{
g.setFont(HF.MEDIUM_FONT);
drawStatusBar(g, HF.COLOR_LIGHTBLUE, mLeftBar, mRightBar, mCanvasWidth, mCanvasHeight);
}
//使用指定的颜色清屏
public void clearScreen(Graphics g, int bgColor)
{
g.setColor(bgColor);
g.fillRect(0, 0, mCanvasWidth, mCanvasHeight);
}
//绘制方框
public static void drawFrame(Graphics g, int color, int x, int y, int width, int height)
{
g.setColor(color);
g.fillRect(x, y, width, height);
}
//后退到上一级的显示
protected void back()
{
mDisplayManager.back();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -