import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.*;
public class MyCanvas extends Canvas {
InputStream is;
String str;
int w, h;// 屏幕的宽和高
int num1;// 每排的字符数
int num2;// 每页的行数
int row;// 文章的行数
int page;// 文章的页数
int index;// 现在的页数
int dir;// 文章显示位置(行数)
Font f;
public MyCanvas() {
is = this.getClass().getResourceAsStream("/1.txt");
try {
byte data[] = new byte[is.available()];
is.read(data);
is.close();
is = null;
str = new String(data);
} catch (IOException e) {
e.printStackTrace();
}
w = this.getWidth();
h = this.getHeight();
f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
num1 = 16;
num2 = 12;
row = str.length() / 16;
page = row / 13;
index = 0;
dir = 0;
}
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, w, h);
g.setColor(0, 0, 255);
g.setFont(f);
int slength = g.getFont().getHeight();// 得到字符串的高度
for (int i = index * num2; i <= ((index + 1) * num2 > row ? row
: (index + 1) * num2); i++) {
g.drawString(str.substring(i * num1,
(i + 1) * num1 > str.length() ? str.length() : (i + 1)
* num1), 10, 10 + slength * dir, 0);
dir++;
}
}
protected void keyPressed(int key) {
switch (key) {
case -1:
if (index > 0) {
index--;
dir = 0;
repaint();
}
break;
case -2:
if (index < page) {
index++;
dir = 0;
repaint();
}
break;
}
}
}
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MyMidlet extends MIDlet {
MyCanvas mc;
Display dis;
public MyMidlet() {
mc = new MyCanvas();
dis = Display.getDisplay(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
dis.setCurrent(mc);
}
}
07-19
07-19
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交