📄 textcomponent.java
字号:
/* * Fire (Flexible Interface Rendering Engine) is a set of graphics widgets for creating GUIs for j2me applications. * Copyright (C) 2006-2008 Bluevibe (www.bluevibe.net) * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * *//** * */package gr.fire.ui;import gr.fire.core.Component;import gr.fire.core.FireScreen;import gr.fire.core.Theme;import gr.fire.util.StringUtil;import java.util.Vector;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Graphics;/** * @author padeler * */public class TextComponent extends Component{ public static final int LINE_DISTANCE=0; private String text; private Vector formatedText; private int textWidth,startX; private int lastLineExtraHeight=0; public TextComponent(String txt,int textWidth,int startX) { if(txt==null) throw new NullPointerException("Cannot have null text in the Text component."); this.text = txt; this.textWidth = textWidth; this.startX = startX; } public void validate() { if(!valid) { formatedText = StringUtil.format(text,font,textWidth-startX,textWidth); valid=true; } } public void paint(Graphics g) { int valign = getValign(); int halign = getHalign(); Theme theme = FireScreen.getTheme(); int width = getWidth(); int height = getHeight(); g.setFont(font); Vector lines = formatedText; int rowHeight = font.getHeight(); int lineCount = lines.size(); int txtHeight = (rowHeight + LINE_DISTANCE)*lines.size() + lastLineExtraHeight; for (int i = 0; i < lineCount; ++i) { String str = (String) lines.elementAt(i); int x=0,y=0; switch(valign) { case FireScreen.TOP: y = (rowHeight+LINE_DISTANCE) * i; break; case FireScreen.VCENTER: y = (height/2 - txtHeight/2) + (rowHeight+LINE_DISTANCE) * i; break; case FireScreen.BOTTOM: y = height - txtHeight +((rowHeight+LINE_DISTANCE) * i); break; } if(i==lineCount-1) y += lastLineExtraHeight; switch(halign) { case FireScreen.LEFT: if(i==0) x = startX; else x =0; break; case FireScreen.CENTER: if(i==0) x = width/2 - font.stringWidth(str)/2 + startX; else x = width/2 - font.stringWidth(str)/2; break; case FireScreen.RIGHT: x = width- font.stringWidth(str); break; } if(!isSelected()) { // paint background is needed //if((backgroundColor&0xFF000000)!=0xFF000000) { g.setColor(backgroundColor); g.fillRect(x , y , font.stringWidth(str), rowHeight); } g.setColor(foregroundColor); } else { //if((theme.selectedLinkBgColor&0xFF000000)!=0xFF000000) { g.setColor(theme.selectedLinkBgColor); g.fillRect( x , y , font.stringWidth(str), rowHeight); } g.setColor(theme.selectedLinkColor); }// System.out.println("PRINTING LINE: ["+ str+"]("+x+"+ "+(i==0?startX:0)+"),"+y+" Width: "+ font.stringWidth(str)); g.drawString(str,x, y, Graphics.TOP | Graphics.LEFT); }// g.setColor(0x00000000+(1000*formatedText.size()));// g.drawRect(1,1,width-2,height-2); } public boolean contains(int x, int y) { if(super.contains(x, y)) { if(formatedText.size()==1) { if(x>startX && x<(startX + getLastLineWidth())) return true; } else // more than one lines. { if(x<startX && y<(font.getHeight()+LINE_DISTANCE)) return false; if(x>getLastLineWidth() && y>(getContentHeight()-getLastLineHeight())) return false; return true; } } return false; } protected void pointerReleased(int x, int y) { if(command!=null && commandListener!=null) { setSelected(!isSelected()); if(isSelected()) commandListener.commandAction(command,this); } super.pointerReleased(x, y); } protected void keyReleased(int keyCode) { int key = FireScreen.getScreen().getGameAction(keyCode); if((key==Canvas.LEFT|| key==Canvas.DOWN ||key==Canvas.RIGHT || key==Canvas.UP)) { setSelected(!isSelected()); } else if(command!=null && commandListener!=null && key==Canvas.FIRE) commandListener.commandAction(command,this); super.keyReleased(keyCode); } public void setSelected(boolean v) { super.setSelected(v); repaint(); } public String getText() { return text; } public int[] getMinSize() { return new int[]{textWidth,font.getHeight()}; } public Vector getFormatedText() { return formatedText; } public int getContentHeight() { if(!valid) throw new IllegalStateException("The element is not validated."); return ((font.getHeight() + LINE_DISTANCE)*formatedText.size()) + lastLineExtraHeight; } public int getContentWidth() { if(!valid) throw new IllegalStateException("The element is not validated."); if(formatedText.size()==0) return 0; if(formatedText.size()==1) return font.stringWidth((String)formatedText.lastElement()); // more than two lines: return textWidth; } public int getLastLineWidth() { if(!valid) throw new IllegalStateException("The element is not validated."); if(formatedText.size()==0) return 0; return font.stringWidth((String)formatedText.lastElement()); } public int getLastLineHeight() { if(!valid) throw new IllegalStateException("The element is not validated."); if(formatedText.size()==0) return 0; return (font.getHeight()+LINE_DISTANCE)+lastLineExtraHeight; } public int getLastLineExtraHeight() { return lastLineExtraHeight; } public void setLastLineExtraHeight(int lastLineExtraHeight) { this.lastLineExtraHeight = lastLineExtraHeight; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -