关闭

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

665人阅读 评论(1) 收藏 举报
  •  
  • //原代码来源与网络
  •  
  •  

  • 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;
            }
        }
  • }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8474次
    • 积分:124
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    文章分类