⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 word.java

📁 J2ME中游戏读文档的类word.java
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


/*
 * @(#)Word.java
 *
 * Copyright (c) GreatelSoft, Inc. All rights reserved. 
 * PROPRIETARY/CONFIDENTIAL
 * Use is subject to license terms
 */

/*
 * Word.java
 *
 * Created on 05/06/29
 * @author Cheng Zhou
 * @version 1.20
 */
/*
 * 注意事项:
 * 1.几处最大值:1)SYMBOL字符最大值为MAX_SYMBOLS.
 * 						 	 2)最大读取字符数为MAX_TEXT.
 * 						 	 3)预处理时最大行数为MAX_LINES.
 * 						 	 4)预处理是最大页数为MAX_PAGES.
 * 2.paintText()之前要执行processText(),若字符串为空则报错.
 * 3.关于\p:如果之前的文字正好填满一页,之后将会有一空白页(\n同样).
 * 
 */
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

//import org.kwis.msp.lcdui.Font;
//import org.kwis.msp.lcdui.Graphics;

public class Word {
	public static final int CHINESE = 0;
	public static final int ENGLISH = 1;
	public static final int TYPE_HELP = 0;
	public static final int TYPE_ABOUT = 1;
	public static final int TYPE_STAFF = 2;
	public static final int TYPE_PRELUDE = 3;
	public static final int TYPE_ENDING = 4;
	public static final int TYPE_DIALOG = 5;
	public static final int TYPE_INTRODUCTION = 6;
	public static final int TYPE_GUIDE = 7;
	public static final int TYPE_CONTROL = 8;
	public static final int TYPE_SERVER = 9;
	public static final int TYPE_NEWLINE = 0;
	public static final int TYPE_NEWPAGE = 1;
	
	public static final int MODE_NORMAL = 1;
	public static final int MODE_DIALOG = 2;
	public static final int MODE_MIDDLE = 3;
	public static final int MODE_ROLL = 4;
	
	public static final int MAX_LINES = 500;
	public static final int MAX_PAGES = 100;
	public static final int MAX_SYMBOLS = 10;
	public static final int MAX_TEXT = 2048;
	public static final int LINE_OFFSET = 3;
	
	public static final char[][] SYMBOL = {{'H'},		
											{'A'},		
											{'S'},		
											{'P'},		
											{'E'},		
											{'D'},		
											{'I'},		
											{'G'},		
											{'C'},
										    {'U'}};	
																					
	public static final char[] FORMAT = {'n','p'};			
	public static final char BEGINOF_PART1 = 60;		
	public static final char BEGINOF_PART2 = 47;		
	public static final char ENDOF = 62;						
	public static final char BEGINOF_FORMAT = 92;		
	public static final char SPACE = 32;						
	private static final String WORD_PUNCTUATION = ",./?;':!,。/?;:!”";
	public char[] value;
	private int count;
	public int totalPages;
	public int totalRows;
	public int totalHeight;
	
	private int width;
	public int height;
	private int[] row_begin;
	private int[] row_end;
	public int[] page_begin;
	private int[] roll_begin;
	private int yOffset;
	private int currMode;
	public int yRoll;
	private int lastPage;
	private int language;

	
	
	/**
	 * 判断字符是否是标点符号
	 * @param ch
	 * @return
	 */
	
	private static boolean isPunctuation(int ch) {
	          

		return WORD_PUNCTUATION.indexOf(ch) >= 0;
	}
	
	public Word(int charHeight) {
	
		this(charHeight, CHINESE);
		//ENGLISH
	}
	// 构造函数重载
	
	public Word(int charHeight, int language) {
		yOffset = charHeight + LINE_OFFSET;
		this.language = language;
	}
	//设置语言种类
	public void setLanguage(int value) {
		if(value >= 3 || value < 0)
			throw new IllegalArgumentException();
		language = value;
	}
	
	public void destroyText() {
		count = 0;
		value = null;
		row_begin = null;
		page_begin = null;
		yRoll = -1;
		lastPage = 0;
		
		System.gc();
	}
	
	public int getPages() {
		return totalPages;
	}
	
	public char[] getText() {
		return value;
	}
		
	public void setText(String str) {
    count = str.length();
    value = null;
    value = new char[count];
    str.getChars(0, count, value, 0);
	}
	
	public void setText(char[] c) {
    count = c.length;
    value = null;
    value = new char[count];
    System.arraycopy(c, 0, value, 0, count);
	}
	
	public boolean rollEnd() {
		return yRoll > totalHeight + height - 10 ? true : false;
	}
	
	public void setRollEnd() {
		yRoll = totalHeight + height;
	}
	
	public boolean changeRole(int page) {
		int n = roll_begin.length;
		int[] rb = roll_begin;
		while(n-- != 0) {
			if(page == rb[n]) {
				if(page != lastPage && page != 1) {
					lastPage = page;
					return true;
				} else {
					return false;
				}
			}
		}
		return false;
	}
	
	public boolean dialogEnd(int page) {
		return page >= totalPages ? true : false;
	}
	 
	  
	public void paintText(Graphics g, int page, int x_start, int y_start, int mode) {
		currMode = mode;
		paintText(g, page, x_start, y_start);
	}
	public static boolean ISover;
	public static int  IScout;
	public void paintText(Graphics g, int page, int x_start, int y_start) {
		int x = x_start, y = y_start;
		int fromIndex = 0, toIndex = 0, row = 0, lastRow = 0, len = 0;
		char[] v = value;
		
		
		//System.out.println("hbbbbbbbbbh="+page_begin.length);
		//System.out.println("hbbbh="+page_begin[0]);
		row = page_begin[page - 1];
		
		//System.out.println("row="+row);
		   
			   
		   
			   
		lastRow = page < totalPages ? page_begin[page] - 1 : totalRows;
		
		
		Font f = g.getFont();
		
		switch(currMode) {
		    
			case MODE_ROLL:
				// System.out.println("aaa="+currMode);
				row = page_begin[0];
				lastRow = totalRows;
				int baseline = y_start + height;
				yRoll = (yRoll < 0 || yRoll > totalHeight + height) ? yOffset : ++yRoll;
				y = baseline - yRoll;
				g.setClip(x_start, y_start, width, height);
				do {
					fromIndex = row_begin[row - 1];
					toIndex = row_end[row - 1];
					len = toIndex - fromIndex;
					if(len != 0 && y > y_start - yOffset && y < baseline) {
						x = x_start + (width - f.charsWidth(v, fromIndex, len)) / 2;
						g.drawChars(v, fromIndex, len, x, y, 20);
					}
					y += yOffset;
				} while(row++ < lastRow);
				 IScout=totalHeight + height-yRoll;
				if(yRoll < 0 || yRoll > totalHeight + height){
					 
					ISover=true;
				
				}
			 	break;
			case MODE_MIDDLE:
				// System.out.println("aaaaaaaaaa="+currMode);
				do {
					fromIndex = row_begin[row - 1];
					toIndex = row_end[row - 1];
					len = toIndex - fromIndex;
					if(len != 0) {
						x = x_start + (width - f.charsWidth(v, fromIndex, len)) / 2;
						g.drawChars(v, fromIndex, len, x, y, 20);
					}
					y += yOffset;
				} while(row++ < lastRow);
				break;
			default:				
				do {
					fromIndex = row_begin[row - 1];
					toIndex = row_end[row - 1];
					len = toIndex - fromIndex;
					if(len != 0) {
						g.drawChars(v, fromIndex, len, x, y, 20);
					}
					y += yOffset;
				} while(row++ < lastRow);
				break;
		  }
	}
	
	public void processText(Font f, int mode, int x, int y, int screenWidth, int screenHeight) {
		processText(f, mode, screenWidth - x * 2, screenHeight - y * 2);
	}
	

	public void processText(Font f, int mode, int width, int height) {
		if(value == null)
			throw new NullPointerException();
		this.width = width;
		this.height = height;
		char[] v = value;
		currMode = mode;
		ISover=false;
		IScout=0;
		int[] rbi = new int[MAX_LINES];		
		int[] rei = new int[MAX_LINES];		
		int[] pbr = new int[MAX_PAGES];		
		int[] rc = new int[MAX_LINES];		
		int rows = 0, pages = 0, index = 0, rolls = 0, 
				x = 0, y = height, len = count;
		char c;
		boolean p = false, r = true;
		boolean blankPage = true;	// 首页为空白页
		switch(mode) {
		default:
			if(language == CHINESE) {										// 中文等字符文字
				while(index < len) {
					rbi[rows++] = index;										// 置新行开始位置
//					System.out.println("row begin " + (rows - 1) + ", " + index);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -