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

  •  
  • //原代码来源与网络
  •  
  •  

  • 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;
            }
        }
  • }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值