修改了一下StringLayout,增加了翻页功能,并且还有了滚动条,希望多多提意见.

原创 2006年05月26日 17:35:00
  •  
  • //原代码来源与网络
  •  
  •  

  • import java.util.Vector;
  • import javax.microedition.lcdui.Font;
    import javax.microedition.lcdui.Graphics;
  • /**
     * <p>
     * Title:
     * </p>
     * <p>
     * Description:
     * </p>
     * <p>
     * Copyright: Copyright (c) 2004
     * </p>
     * <p>
     * Company: Bluecell
     * </p>
     * 
     * @author not attributable
     * @modify liberydn 05-26 增加了滚动条,增加了翻页的功能
     * @version 1.0
     */
  • public class StringLayout {
        public int layoutWidth, layoutHeight, layoutX, layoutY;
  •     private int lineGap;
  •     private String text;
  •     private int fontHeight;
  •     private int totalLineCount;
  •     private Vector line;
  •     private Font font = null;
  •     private int currLine = 0;
  •     public boolean isChange = false;
  •     private int lineHeight = 0;
  •     private int moveLineCount;
  •     private int screenLineCount = 0;//屏幕可以显示的行数
  •     /**
         * 构造函数 <br>
         * Str:布局中的字符串 <br>
         * X:布局的左上角位置 <br>
         * Y:布局的右上角位置 <br>
         * LayoutX:布局顶点X <br>
         * LayoutY:布局顶点Y <br>
         * LayoutWidth:布局宽度 <br>
         * LayoutHeight:布局高度 <br>
         * Gap:各行之间的空隙
         */
        public StringLayout(String Str, int LayoutX, int LayoutY, int LayoutWidth,
                int LayoutHeight, int Gap, Font font) {
            text = Str;
            layoutX = LayoutX;
            layoutY = LayoutY;
            layoutWidth = LayoutWidth;
            layoutHeight = LayoutHeight;
            lineGap = Gap;
            this.font = font;
  •         int begin = 0;
  •         fontHeight = font.getHeight();
            totalLineCount = 0;
            line = new Vector(5, 2);
            for (int i = 0; i < text.length(); i++) {
                char ch = text.charAt(i);
  •             if (font.stringWidth(text.substring(begin, i + 1)) >= layoutWidth
                        || i == text.length() - 1 || ch == '/n') { //layoutWidth-3中的3为偏移值
  •             if(i==text.length()) i++;
                    line.addElement(text.substring(begin, i));
                    if (ch == '/n')
                        begin = i + 1;
                    else
                        begin = i;
                    totalLineCount++;
                }
            }
  •         if (LayoutHeight % (fontHeight + Gap) == 0) {
                screenLineCount = (LayoutHeight / (fontHeight + Gap));
            } else {
                screenLineCount = (LayoutHeight / (fontHeight + Gap)) - 1;
            }
            moveLineCount = totalLineCount - screenLineCount;
  •         lineHeight = layoutHeight / (moveLineCount);
        }
  •     /**
         * 画出字符串
         */
        public void draw(Graphics g, int x, int y) {
            int i1 = g.getClipX();
            int j1 = g.getClipY();
            int k1 = g.getClipWidth();
            int l1 = g.getClipHeight();
  •         g.setClip(layoutX, layoutY, layoutWidth, layoutHeight);
            g.setFont(font);
            for (int i = currLine; i < line.size(); i++) {
                String s = (String) line.elementAt(i);
                g.drawString(s, x, y + (i - currLine) * (fontHeight + lineGap),
                        Graphics.TOP | Graphics.LEFT);
            }
  •         if (moveLineCount >= 1) {
                //开始画出滚动条
                g.setColor(21, 111, 72);
                g.drawLine(layoutX + layoutWidth - 2, layoutY, layoutX
                        + layoutWidth - 2, layoutY + layoutHeight);
  •             int curY = layoutY + (currLine * layoutHeight) / (moveLineCount);
  •             if (curY + lineHeight >= (layoutY + layoutHeight)) {
                    curY = layoutY + layoutHeight - lineHeight;
                }
  •             g.fillRect(layoutX + layoutWidth - 4, curY, 4, lineHeight);
  •         }
            g.setClip(i1, j1, k1, l1);
            isChange = false;
        }
  •     public boolean isNext() {
  •         if (currLine < moveLineCount - 1)
                return true;
            else
                return false;
        }
  •     public void next() {
            if (currLine < totalLineCount - 1) {
                currLine++;
                isChange = true;
            }
        }
  •     public boolean isPrev() {
            if (currLine > 0)
                return true;
            else
                return false;
        }
  •     public void prev() {
            if (currLine > 0) {
                currLine--;
                isChange = true;
            }
        }
  •     public boolean isNextPage() {
  •         if (currLine < totalLineCount - screenLineCount)
                return true;
            else
                return false;
        }
  •     public void nextPage() {
            if (currLine < totalLineCount - 1) {
                currLine = Math.min(totalLineCount, currLine + screenLineCount);
                isChange = true;
            }
        }
  •     public boolean isPrevPage() {
            if (currLine > 0)
                return true;
            else
                return false;
        }
  •     public void prevPage() {
            if (currLine > 0) {
                currLine = Math.max(0, currLine - screenLineCount);
                isChange = true;
            }
        }
  • }

相关文章推荐

本人是一名专科学生,现自学java,记录学习。希望能够大家多多提提意见

今天在网上传智播客下载了一些视频,初步了解了一下计算机的概述,了解了一下计算机的硬件,软件,DOS命令,图形界面命令,以及将来这方面要做什么。             简单的说开发软件,就是做软件...

JavaScript模拟的HashMap,请大家多多提意见!

/** * V1.0 * HashMap javascript版 * 2013-09-04 * var map = new $.HashMap(); map.put("ag...

九宫格程序代码 共享并希望大家多提意见

九宫格程序代码 共享并希望大家多提意见以下是该程序的题目要求,如下:编程题:Android手机9宫格有多少种可能的手势?如果是16宫格呢? 规则:4个点或以上的手势才算;一个手势中,每个点最多只能被算...

ECharts--基于力导向布局图功能更完善的人物关系图插件扩展-增加横纵滚动条

ECharts--基于力导向布局图功能更完善的人物关系图插件扩展-增加横纵滚动条 作者: IT小兵 | 分类: 前端开发 | 2014年4月25日| 浏览:18173℃ | 评论:0 |参与:0...

ListView中的滚动事件及自动翻页功能

ListView的滚动事件              为ListView添加滚动事件            setOnScrollListener(OnScrollListener listene...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:修改了一下StringLayout,增加了翻页功能,并且还有了滚动条,希望多多提意见.
举报原因:
原因补充:

(最多只允许输入30个字)