您的位置:首页 > 编程语言

自己做的J2ME阅读器源代码!

2006-09-25 21:26 225 查看
挺长时间没有更新Blog,因为内容不多,所以总结起来一起更新。

这个东西做了十多天,别人可能也就1天左右,为什么我要那么长?可能是新手的原因吧,而且这段时间课程也比较紧。废话不说。代码上来:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
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;
import javax.microedition.midlet.MIDlet;

public class newCanvas extends Canvas implements CommandListener{

private String s; //把读出来的字节码形成UTF-8编码方式

private final static int scrHight = 208; //固定的屏幕高度。

private Font f; //当前系统默认字体信息

private char[] c; //通过s得到的char数组

private Vector view; //最后形成,存放文本的地方

private String[] strView; //显示用数组

private int start; //显示的开始行

private int end; //终止行

private int scrLines; //屏幕可以打印多少行

public static MIDlet mid; //主窗口
//成员变量初始化
public newCanvas() {
setFullScreenMode(true);
s = null;
f = Font.getDefaultFont();
c = null;
view = new Vector();
try {
s = getText(); //测试通过,已经得到文件内容。
} catch (IOException e) {

System.out.println("读文件失败!"); //这里添加错误处理
}
c = s.toCharArray();
cut(view, c);
strView = new String[view.size()];
scrLines = scrHight / f.getHeight(); //scrHight
start = 0;
end = scrLines;
//按行存放到数组中,方便以后显示。
Enumeration e = view.elements();
for (int i = 0; i < strView.length; i++) {
strView[i] = (String) e.nextElement();
}
//垃圾回收
s = null;
System.gc();
//添加退出命令
this.addCommand(new Command("退出", Command.EXIT, 1));
setCommandListener(this);
}

public void paint(Graphics g) {
clearScreen(g);
int x = 0;
int y = -f.getHeight();
for (int i = start; i <= end - 1; i++) {
y += f.getHeight();
g.drawString(strView[i], x, y, f.SIZE_SMALL);
}
}

private void clearScreen(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), scrHight); //scrHight
g.setColor(0, 0, 0);
}

public String getText() throws IOException {
InputStream is = getClass().getResourceAsStream("/1.txt");
is.skip(3); //跳过UTF-8的标记符号
if (null != is) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch = 0;
while ((ch = is.read()) != -1) {
baos.write(ch);
}
byte[] text = baos.toByteArray();
baos.close();
return new String(text, "UTF-8");

} else {
return null;
}
}

public void cut(Vector v, char[] c) {

StringBuffer sb = new StringBuffer(); //临时存放一行内容的容器
int tmp = 0; //记录截断位
int num = 0; // 行内数字计数器
while (tmp < c.length - 1) {
for (int i = tmp; i < c.length; i++) {
if (c[i] == '/n') {
num = 0;
tmp++; //遇到空格跳过去
break;
}
num += f.charWidth(c[i]); //记录开始
if (num >= getWidth()) {
num = 0;
tmp++; //重复字符跳过去。
break;
}
sb.append(c[i]); // 添加字符
tmp = i;
}
v.addElement(sb.toString());
sb.delete(0, sb.length());

}
sb = null; //清理垃圾

}

protected void keyPressed(int keycode) {
switch (getGameAction(keycode)) {
case Canvas.UP:

if (start == 0) {
break;
}
start = start - 1;
end = end - 1;
repaint();
break;
case Canvas.DOWN:

if (end == strView.length) {
break;
}
start = start + 1;
end = end + 1;
repaint();
break;
case Canvas.LEFT:

if ((start - (scrLines - 1)) < 0) {
start = 0;
end = scrLines;
repaint();
break;
}
start = start - (scrLines - 1);
end = end - (scrLines - 1);
repaint();
break;
case Canvas.RIGHT: //对右键的处理,是向下翻一页。
if ((end + (scrLines - 1)) >= strView.length) {
if (end == strView.length) {
break;
}
start = end;
end = strView.length;
repaint();
break;
}
start = start + (scrLines - 1);
end = end + (scrLines - 1);
repaint();
break;
}
}

public void commandAction(Command c, Displayable d) {
if (c.getLabel().equals("退出")) {
mid.notifyDestroyed();
}

}
}

要读的文件放到res目录里,必须是UTF-8格式的。

这个程序只是能跑起来,速度不是很快,需要把算法在优化一下,这几天就弄,然后再加点功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: