fontbranchmidlet.java
来自「example2 众多JAVA实例源码...学习java基础的好帮手」· Java 代码 · 共 137 行
JAVA
137 行
package opusmicro.demos.scroll2;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class FontBranchMIDlet extends MIDlet{
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(new FontBranchCanvas());
}
}
class FontBranchCanvas extends Canvas {
int gintBeginIndex = 0;// 全局变量,在keyPressed里改变它的值,便可以实现上下翻页
int gintLineHeight = 15;// 全局变量,行高
Vector vector = null;
public FontBranchCanvas() {
// this.setFullScreenMode(true);
}
public Vector getSubsection(String strSource, Font font, int width, String strSplit) {
vector = new Vector();
String temp = strSource;
int i, j;
int LastLength = 1;
int step = 0;
try {
while ( !temp.equals("")) {
i = temp.indexOf("\n");
if ( i > 0) {
if ( font.stringWidth(temp.substring(0, i - 1)) >= width) {
i = -1;
}
}
if ( i == -1) {
if ( LastLength > temp.length()) {
i = temp.length();
}
else {
i = LastLength;
step = font.stringWidth(temp.substring(0, i)) > width ? -1 : 1;
// 得到临界点
if ( i < temp.length()) {
while ( !(font.stringWidth(temp.substring(0, i)) <= width && font.stringWidth(temp
.substring(0, i + 1)) > width)) {
i = i + step;
if ( i == temp.length()) break;
}
}
}
// 断词,如果需要的话
if ( !strSplit.equals("")) {
j = i; // 把初始值记录下来,是因为有可能出现一种情况,这种情况就是这一行只有这么一个单词,会一直搜索到头
if ( i < temp.length()) {
while ( strSplit.indexOf(temp.substring(i - 1, i)) == -1) {
i--;
if ( i == 0) {
i = j; // 恢复
break;
}
}
}
}
}
LastLength = i;
vector.addElement(temp.substring(0, i));
if ( i == temp.length()) {
temp = "";
}
else {
temp = temp.substring(i);
if ( temp.substring(0, 1).equals("\n")) {
temp = temp.substring(1);
}
}
}
}
catch (Exception e) {
System.out.println("getSubsection:" + e);
}
return vector;
}
protected void keyPressed(int keycode) {
int code = getGameAction(keycode);
switch ( code) {
case UP:
if ( gintBeginIndex > 0) {
gintBeginIndex--;
}
else {
gintBeginIndex = 0;
break;
}
repaint();
break;
case DOWN:
if ( vector.size() * gintLineHeight <= getHeight()
|| (vector.size() - gintBeginIndex) * gintLineHeight < getHeight()) {
break;
}
gintBeginIndex++;
repaint();
break;
}
}
protected void paint(Graphics g) {
g.setColor(-1);
g.fillRect(0, 0, getWidth(), getHeight());
System.out.println("click....");
g.setColor(0);
String str = "this is a example for scroll text content 方便起见,可以定义一个行高作为全局变量,这样比较好,写个简单的例子:"
+ "2方便起见,可以定义一个行高作为全局变量,这样比较好,写个简单的例子:this is a example for scroll text content "
+ "3方便起见,可以定义一个行高作为全局变量,这样比较好,写个简单的例子:方便起见,可以定义一个行高作为全局变量,这样比较好,写个简单的例子:"
+ "4方便起见,可以定义一个行高作为全局变量,这样比较好,写个简单的例子:方便起见,可以定义一个行高作为全局变量,这样比较好,写个简单的例子:"
+ "this is a example for scroll text content AND just for number and others "
// + "写个简单的例子:7方便起见,可以定义一个行高作为全局变量,这样比较好,写个简单的例子:"
+ "this is a example for scroll text content 9方便起见,可以定义一个行高作为全局变量,这样比较好,写个简单的例子:"
+ "10方便起见,可以定义一个行高作为全局变量,这样比较好,写个简单的例子:24方便起见,可以定义一个行高作为全局变量,这样比较好,写个简单的例子:";
Vector vector = getSubsection(str, Font.getDefaultFont(), getWidth(), " ,.?!");
for ( int i = gintBeginIndex ; i < vector.size() ; i++) {
g.drawString((String) vector.elementAt(i), 0, gintLineHeight * (i - gintBeginIndex), 0);
if ( (i - gintBeginIndex + 1) * gintLineHeight > getHeight()) break;
}
vector = null;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?