import javax.microedition.lcdui.Graphics; import mmo3.main.MainCanvas; import mmo3.ui.component.Component; import mmo3.util.Common; /** * 打字机效果的layer * @author tiger * @date 2010-12-1 */ public class TypeTextLayer extends Container implements ContainerListener { // private String text ; private String[] arrays; private int charNum ; //一行的最大字数 private int lineNum ; //每页的最大行数 private int currentLine = 0; // 当前行 private int currentColumn = 0; //打到了当前行的哪个字 private int beginLineIndex = 0; //当前开始行的索引 用于翻页 private byte offsetX = 5; private byte offsetY = 5; public TypeTextLayer(String text) { super(1); // this.text = text; this.x = 20; this.y = 30; this.setWidth(MainCanvas.SCREEN_WIDTH - 40); this.setHeight(180); charNum = (this.getWidth() - 2 * offsetX) / Common.CHAR_WIDTH; lineNum = (this.getHeight() - 2 * offsetY) / Common.FONT_HEIGHT; int size = (text.length() + charNum - 1) / charNum; arrays = new String[size]; for(int i = 0; i < arrays.length; i++) { if(i == arrays.length - 1) { arrays[i] = text.substring(i * charNum); }else{ arrays[i] = text.substring(i * charNum , i * charNum + charNum); } } this.setListener(this); } public void draw(Graphics g , int x, int y) { g.setColor(0x423456); g.fillRoundRect(x, y, getWidth(), getHeight(), 20, 20); g.setColor(0x65f331); int tx = x + offsetX; int ty = y + offsetY; for(int i = beginLineIndex; i < currentLine; i++) { g.drawString(arrays[i], tx, ty, 0); ty += 20; } g.drawString(arrays[currentLine].substring(0, currentColumn), tx, ty, 0); if(this.counter % 4 != 0) { return; } //画完的处理 if(currentLine == arrays.length - 1 && currentColumn == arrays[arrays.length - 1].length() - 1) { beginLineIndex = 0; currentLine = 0; currentColumn = 0; return; } currentColumn++; if(currentColumn == charNum + 1) //换行 { currentLine ++; currentColumn = 0; if(currentLine - beginLineIndex == lineNum) //换页 { beginLineIndex = currentLine; } } } public void processLayerAction(Container container, Component actionComponent, int actionType) { this.close(); } public void processLayerClose(Container container) { this.close(); } // String str = "庆历四年春,滕子京谪守巴陵郡。越明年,政通人和,百废具兴,乃重修岳阳楼,增其旧制,刻唐贤今人诗赋于其上," + // "属予作文以记之。 予观夫巴陵胜状,在洞庭一湖。衔远山,吞长江,浩浩汤汤,横无际涯;朝晖夕阴,气象万千;此则岳阳楼之大观也," + // "前人之述备矣。然则北通巫峡,南极潇湘,迁客骚人,多会于此,览物之情,得无异乎? 若夫霪雨霏霏,连月不开;阴风怒号,浊浪排空;" + // "日星隐耀,山岳潜形;商旅不行,樯倾楫摧;薄暮冥冥,虎啸猿啼;登斯楼也,则有去国怀乡,忧谗畏讥,满目萧然,感极而悲者矣! " + // "至若春和景明,波澜不惊,上下天光,一碧万顷;沙鸥翔集,锦鳞游泳,岸芷汀兰,郁郁青青。" + // "而或长烟一空,皓月千里,浮光跃金,静影沈璧,渔歌互答,此乐何极!登斯楼也,则有心旷神怡,宠辱偕忘、把酒临风,其喜洋洋者矣!" + // " 嗟夫!予尝求古仁人之心,或异二者之为,何哉?不以物喜,不以己悲,居庙堂之高,则忧其民;处江湖之远,则忧其君。" + // "是进亦忧,退亦忧;然则何时而乐耶?其必曰:“先天下之忧而忧,后天下之乐而乐矣!”噫!微斯人,吾谁与归!时六年九月十五日。 "; }