日期:2014-05-20 浏览次数:20684 次
public class TextBox extends Part { /** 文本的行数 */ private int numOfEls; /** * 此页面最多显示得行数 */ private int maxNum; private int pillarHeight; /** 开始索引 * */ private int startIndex; /** * 文本的内容 */ private char [] textChars; /** * 文本切割后得内容 */ private String[] textNum; private String text; public void setString(String text) { this.text = text; this.textChars = text.toCharArray(); skipContent(); //numOfEls = textChars.length; } /** * 分割字符传.并处理pillar的高度,最主要的方法。 * 通过屏幕的高,宽,以及字体的体型进行文本出来。并把处理后的文本保存在一个Vector中,以后绘制出来就容易多了 * */ private void skipContent(){ this.numOfEls = FontUtil.siptRow(this.contentStyle.font, viewContent[WIDTH],text); int charWidth = viewContent[X]+ 2; int charHeight = viewContent[Y]; int h; if(numOfEls>this.maxNum){ h = (viewContent[WIDTH]-18); }else{ h = (viewContent[WIDTH]-12); } StringBuffer sb = new StringBuffer(); Vector ve = new Vector(); for(int i=0 ; i < this.textChars.length; i++){ sb.append(textChars[i]); charWidth = charWidth + contentStyle.font.charWidth(textChars[i]); if(charWidth>h){ charHeight = charHeight + contentStyle.font.getHeight() ; charWidth = viewContent[X] + 2; ve.addElement(sb.toString()); log.debug(sb); sb = null; sb = new StringBuffer(); } } ve.addElement(sb.toString()); log.debug(sb); textNum = new String[ve.size()]; ve.copyInto(this.textNum); ve.removeAllElements(); ve = null; //获取最大的数组值 maxNum = viewContent[HEIGHT]/(this.contentStyle.font.getHeight() ) ; this.numOfEls = this.textNum.length; this.pillarHeight = numOfEls > maxNum ? viewContent[HEIGHT]/(numOfEls-maxNum +1):0; log.debug("MaxNum=" + maxNum); log.debug("numOfEls=" + numOfEls); } /** * 绘制文本的内容,自动实现,状态条的显示等 * @param g */ private void paintContent(Graphics g) { this.contentStyle.setFontColor(g); int charWidth = viewContent[X]+ 2; int charHeight = viewContent[Y]; int size = maxNum > this.numOfEls? numOfEls:maxNum; log.debug(startIndex); for(int i=startIndex ; i < size + startIndex; i++){ g.drawString(this.textNum[i],charWidth,charHeight,Graphics.TOP | Graphics.LEFT); charHeight = charHeight + contentStyle.font.getHeight() ; } paintPillar(viewContent[WIDTH]-pillarWidth-4, viewContent[Y],g); } private void paintPillar(int x,int y,Graphics g){ if (maxNum > this.numOfEls) return; pillar.paint(x,y+(startIndex * pillarHeight),viewContent[WIDTH],this.pillarHeight,g); }