代码:
/*
* Created on 2004-6-20
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package cn.edu.xtu.tilepuzzle.ui;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import cn.edu.xtu.tilepuzzle.GameDB;
import cn.edu.xtu.tilepuzzle.contorller.MainUIController;
import cn.edu.xtu.tilepuzzle.model.ClassPeopleInfo;
import cn.edu.xtu.tilepuzzle.model.RecordStoreOperations;
public class ShowPeopleInfoListUI extends Canvas implements CommandListener {
static final int lowColor = 0x000000FF;
static final int highColor = 0x00FF0000;
static final int highBGColor = 0x00CCCCCC;
public int width;
public int height;
static final Font fontTitle = GameDB.MONOSPACE_BOLD_LARGE_Font;
static final Font font = GameDB.MONOSPACE_PLAIN_MEDIUM_Font;
static final int lineHeight = font.getHeight();
private MainUIController mainUIController;
private Command ok;
private Display display;
private int startLine = 0;
// private int endLine = 0;
private int allPeopleLine = 0;
private int showLines = 16;
private String titleString[] = { "排 名", "名 字 ", "时 间" };
private ClassPeopleInfo classPeopleInfo[];
Displayable displayable;
int xSpace=0;
int lenTitle[];
int titleHeight=0;
// private Image titleImage;
public ShowPeopleInfoListUI(MainUIController mainUIController,Display display,Displayable displayable) {
this.display = display;
this.displayable=displayable;
this.mainUIController = mainUIController;
// this.setFullScreenMode(true);
this.width = getWidth();
this.height = getHeight();
// System.out.println("屏幕的宽,高:"+width+","+height);
startLine = 0;
ok = new Command("返回", Command.OK, 0);
addCommand(ok);
setCommandListener(this);
}
public void init() {
//System.out.println("获取人物信息。。。。");
this.classPeopleInfo=RecordStoreOperations.ReaderRecordStores(GameDB.recordStorePeopleResult);
allPeopleLine=classPeopleInfo.length;
this.sortPeopleInfos();
startLine=0;
lenTitle = new int[3];
for (int ii = 0; ii < 3; ii++) {
lenTitle[ii] = font.stringWidth(titleString[ii]);
}
xSpace = (width - (lenTitle[0] + lenTitle[1] + lenTitle[2])) / 4;
titleHeight=fontTitle.getHeight();
showLines=(getHeight()-titleHeight)/lineHeight;
}
protected void paint(Graphics g) {
// TODO Auto-generated method stub
g.setColor(255, 255, 190);
g.fillRect(0, 0, width, height);
// System.out.println("屏幕的宽,高:"+width+","+height);
g.setFont(fontTitle);
g.setColor(50,50,50);
int len[] = new int[3];
for (int ii = 0; ii < 3; ii++) {
len[ii] = fontTitle.stringWidth(titleString[ii]);
}
g.drawString(titleString[0], xSpace / 6, lineHeight / 4, Graphics.LEFT | Graphics.TOP);
g.drawString(titleString[1], xSpace / 5 + 2 * xSpace + lenTitle[0] - xSpace / 3, lineHeight / 4, Graphics.LEFT | Graphics.TOP);
g.drawString(titleString[2], xSpace / 5 + 3 * xSpace + lenTitle[0] + lenTitle[1], lineHeight / 4, Graphics.LEFT | Graphics.TOP);
String str[];
if(startLine+showLines>allPeopleLine)
showLines=allPeopleLine-startLine;
for (int i = startLine,j=0; j < showLines; i++,j++) {
g.setFont(font);
str = getPeopleInfo(classPeopleInfo[i],i);
len = new int[3];
for (int ii = 0; ii < 3; ii++) {
len[ii] = font.stringWidth(str[ii]);
}
g.drawString(str[0], xSpace / 6 , titleHeight+j*lineHeight +lineHeight / 4, Graphics.LEFT | Graphics.TOP);
g.drawString(str[1], xSpace / 5 + 2 * xSpace + lenTitle[0] - xSpace / 3, titleHeight+j*lineHeight +lineHeight / 4, Graphics.LEFT | Graphics.TOP);
g.drawString(str[2], xSpace / 5 + 3 * xSpace + lenTitle[0] + lenTitle[1], titleHeight+j*lineHeight +lineHeight / 4, Graphics.LEFT | Graphics.TOP);
//g.drawString(mainMenu[i],(width - DataBase.MONOSPACE_PLAIN_MEDIUM_Font.stringWidth(mainMenu[i])) / 2, startHeight + (i * startHeight * 3 / 2 + startHeight / 4),Graphics.LEFT | Graphics.TOP);
}
}
public String[] getPeopleInfo(ClassPeopleInfo classPeopleInfo, int i) {
String str[] = new String[3];
str[0] = Formatstr0(i);
str[1] = classPeopleInfo.getName();
str[2] = classPeopleInfo.getStrTime();
return str;
}
public String Formatstr0(int i) {
if ((1+i) >0 && (1+i) < 10)
return "第" + " " + (1+i) + " " + "名";
else if ((1+i) < 100 && (1+i) >= 10)
return "第" + (1+i) + " " + "名";
else
return "第" + (1+i) + "名";
}
public void sortPeopleInfos() {
int len = classPeopleInfo.length, i, j,flag;
//System.out.println("共有人数:"+len);
ClassPeopleInfo temp;
for (i = 0; i < len - 1; i++) {
temp=classPeopleInfo[i];
flag=i;
for (j = i + 1; j < len; j++) {
if (temp.time > classPeopleInfo[j].time) {
//System.out.println(temp.time +","+peopleInfo[j].time);
temp = classPeopleInfo[j];
flag=j;
}
}
if(flag!=i){
classPeopleInfo[flag]=classPeopleInfo[i];
classPeopleInfo[i] = temp;
}
}
//return peopleInfo;
}
protected void keyPressed(int code) {
if (getGameAction(code) == Canvas.UP) {
if (startLine - 1 >= 0)
startLine--;
} else if (getGameAction(code) == Canvas.DOWN) {
if (startLine + showLines < allPeopleLine)
startLine++;
//else
// startLine = 0;
} else
;
//System.out.println("起始行:"+(startLine+1)+" 显示行:"+showLines+" 总人数:"+allPeopleLine);
// System.out.println("menuIdx = " + (menuIdx)+":"+mainMenu[menuIdx]);
repaint();
}
public void commandAction(Command c, Displayable db) {
// TODO Auto-generated method stub
if (c == ok) {
//System.out.println("返回=====");
display.setCurrent(mainUIController.mainMenuUI);
}
}
}
运行显示如图: