自己做的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格式的。
这个程序只是能跑起来,速度不是很快,需要把算法在优化一下,这几天就弄,然后再加点功能。
这个东西做了十多天,别人可能也就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格式的。
这个程序只是能跑起来,速度不是很快,需要把算法在优化一下,这几天就弄,然后再加点功能。
相关文章推荐
- 关于自己的游戏《小鱼吃苹果》 J2ME和android代码提供给大家学习
- 程序员的进步从阅读自己的老代码开始
- 程序员的成长从阅读自己的老代码开始
- 通过阅读别人的代码轻松提高自己的编程能力
- 代码阅读方法(自己总结地 厚厚)
- 程序员的进步从阅读自己的老代码开始
- 关于代码阅读的编程实现技巧及自己工作学习的一些经验(推荐看后面的后记)
- 程序员的进步从阅读自己的老代码开始
- 程序员的进步从阅读自己的老代码开始
- 自己学驱动6——uboot代码阅读一(start.S)
- 自己写的J2ME手机文件浏览器,个人感觉代码比书上的更能让人明白
- 自己学驱动8——uboot代码阅读三(start.S)
- 如何通过阅读别人的代码提高自己的编程能力
- 自己学驱动4——SourceInsight代码阅读工具
- 关于自己J2ME和android游戏《小鱼吃苹果》的游戏主代码
- 程序员的进步从阅读自己的老代码开始
- 十大注释技巧教你如何书写容易阅读的代码
- 人脸检测之MTCNN训练自己的数据(部分代码公开!请关注置顶的MTCNN算法优化!)
- 代码阅读
- 代码阅读总结之ASP.NET StartKit TimeTracker(QueryString之改进笔记)