📄 panel.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.core;import gr.fire.ui.ScrollAnimation;import java.util.Vector;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;/** * A Panel is a special type of Container. It can contain at most one container. * If the inner Container is bigger that the Panel's dimensions the Panel can be configured * to display scrollbars. * * The Panel will allow navigation on the Container's components thought its viewport. * The Panel will scroll to bring the focused component inside the viewport.<br/> * * The panel can display decorations around its viewport, a navigation bar (navbar) at the bottom and a * title bar (titlebar) at the top of the screen. Also a logo image can be displaed on a Theme specific location. * If the {@link #setShowBackground(boolean)} flag is set, the panel will also paint a theme specific background behing the container. * Note that the container must be transparent in order for the background to be visible.<br/> * * * On the navbar the softkey commands (if any) will appear. * On the titlebar the label of the Panel will be displayed {@link #setLabel(String)}.<br/> * * On phones with touchscreens a Panel with scrollbars will intercept tap events on the scrollbars and scroll the container.<br/> * * @see Container * @author padeler * */public class Panel extends Container{ /** * To make the navigation inside the panel easier the Panel will scroll faster when several conditions are met. * For example it will scroll fast down if there are no hotizontal scrollbars AND the user pressed right to move to the next * selectable component.<br/> * * This static value defines the percentage of the phones screen on the dimension of the scroll that will be used to * calculate the fast scroll. * */ public static final int FAST_SCROLL_PERCENT=60; /** * This static value defines the percentage of the phones screen on the dimension of the scroll that will be used to * calculate the normal scroll. * */ public static final int NORMAL_SCROLL_PERCENT=35; /** * If set, and the Container inside this Panel has container.height>this.height then vertival scrollbars will be visible. */ public static final int VERTICAL_SCROLLBAR = 0x00000001; /** * If set, and the Container inside this Panel has container.width>this.width then horizontal scrollbars will be visible. */ public static final int HORIZONTAL_SCROLLBAR = 0x00000100; /** * Default scrollbar policy. */ public static final int NO_SCROLLBAR = 0x00000000; private int scrollBarPolicy = NO_SCROLLBAR;// no scrollbars private boolean showDecorations = false; private int scrollX,scrollY; protected Container container = null; private boolean showBackground=false; int viewPortWidth, viewPortHeight; private boolean closeOnOutofBoundsPointerEvents=true; // controls if this panel should close when the user taps outside its bounding box. private Theme theme; private String label; private int labelX=0,labelY=0; private Image titlebarImage, navbarImage,backgroundTexture; private int decorLeft=0,titlebarSize=0,decorRight=0,navbarSize=0; private int dragX=-1,dragY=-1; private boolean dragScroll=false; int normalVScrollLength,fastVScrollLength,normalHScrollLength,fastHScrollLength; /** * Constructs a Panel. * @param cnt The container placed inside this panel * @param scrollbarPolicy show vertical/horizontal scrollbars or not. * @param showDecorations display decorations. Decorations are theme specific. */ public Panel(Container cnt, int scrollbarPolicy, boolean showDecorations) { setFocusable(true); this.scrollBarPolicy = scrollbarPolicy; this.showDecorations = showDecorations; if(cnt!=null) set(cnt); theme = FireScreen.getTheme(); } /** * Constructs a Panel without a container with the given scrollbar policy and with no decorations. * @param scrollBarPolicy */ public Panel(int scrollBarPolicy) { this(null, scrollBarPolicy, false); } /** * Constructs a Panel with no scrollbars no inner container and no decorations */ public Panel() { this(null, NO_SCROLLBAR, false); } public void paint(Graphics g) { int originalTrX = g.getTranslateX(); int originalTrY = g.getTranslateY(); int originalClipX = g.getClipX(); int originalClipY = g.getClipY(); int originalClipWidth = g.getClipWidth(); int originalClipHeight = g.getClipHeight(); Component cmp = container; if (cmp != null) // draw only visible components { if (cmp.valid == false) { // validate my component. cmp.validate(); } if(showBackground) { if(backgroundTexture==null) backgroundTexture = theme.getBackgroundTexture(viewPortWidth,viewPortHeight); if(backgroundTexture!=null) g.drawImage(backgroundTexture,decorLeft,titlebarSize,Graphics.TOP|Graphics.LEFT); } if (cmp.visible) { if (cmp.animation == null) { if(cmp.intersects(originalClipX, originalClipY, originalClipWidth, originalClipHeight)) { g.clipRect(cmp.x, cmp.y, cmp.width, cmp.height); g.translate(cmp.x, cmp.y); cmp.paint(g); // return to the coordinates of this component. g.translate(originalTrX - g.getTranslateX(), originalTrY - g.getTranslateY()); g.setClip(originalClipX, originalClipY, originalClipWidth, originalClipHeight); } } else { if(cmp.animation.intersects(originalClipX, originalClipY, originalClipWidth, originalClipHeight)) { g.clipRect(cmp.animation.x, cmp.animation.y, cmp.animation.width, cmp.animation.height); g.translate(cmp.animation.x, cmp.animation.y); cmp.animation.paint(g); // return to the coordinates of this component. g.translate(originalTrX - g.getTranslateX(), originalTrY - g.getTranslateY()); g.setClip(originalClipX, originalClipY, originalClipWidth, originalClipHeight); } } } } if (showDecorations) // this panel has decorations { if(originalClipY < titlebarSize) drawTitlebar(g); if((originalClipY + originalClipHeight) > (height - navbarSize)) drawNavbar(g); if(originalClipX < decorLeft) drawDecorLeft(g); if((originalClipX + originalClipWidth) > (width - decorRight)) drawDecorRight(g); Image logo = theme.getLogo(); if(logo!=null) { int lx=0,ly=0; switch (theme.getIntProperty("logo.icon.valign")) { case FireScreen.TOP: ly=0; break; case FireScreen.BOTTOM: ly = getHeight()-logo.getHeight(); break; case FireScreen.VCENTER: ly = (getHeight()-logo.getHeight())/2; break; } switch (theme.getIntProperty("logo.icon.align")) { case FireScreen.LEFT: lx=0; break; case FireScreen.RIGHT: lx= getWidth()-logo.getWidth(); break; case FireScreen.CENTER: lx= (getWidth()-logo.getWidth())/2; break; } g.drawImage(logo,lx,ly,Graphics.TOP|Graphics.LEFT); } if(label!=null) { Font labelFont = theme.getFontProperty("label.font"); g.setFont(labelFont); g.setColor(theme.getIntProperty("titlebar.fg.color")); int lw = labelFont.stringWidth(label); g.drawString(label, labelX, labelY, Graphics.TOP | Graphics.LEFT); } } if(border) { g.setColor(theme.getIntProperty("border.color")); g.drawRect(0,0,width-1,height-1); } drawScrollbars(g); } /** * Returns the label set to this Panel. The label is displayed on the top side of the Panel, on the area of the titlebar. * The alignment, the font and color of the label are theme specific. * * @return */ public String getLabel() { return label; } /** * Sets a label for this panel. * @see #getLabel() * @param label */ public void setLabel(String label) { this.label=label; if(label!=null) {// find the labelX and labelY. int align = theme.getIntProperty("label.align"); int valign = theme.getIntProperty("label.valign"); Font f = theme.getFontProperty("label.font"); int strLen = f.stringWidth(label); int strHeight = f.getHeight(); switch(align) { case FireScreen.CENTER: labelX= FireScreen.getScreen().getWidth()/2-strLen/2; break; case FireScreen.RIGHT: labelX= FireScreen.getScreen().getWidth()-strLen; break; default: labelX=0; } switch(valign) { case FireScreen.VCENTER: labelY= FireScreen.getScreen().getHeight()/2-strHeight/2; break; case FireScreen.BOTTOM: labelY= FireScreen.getScreen().getHeight()-strHeight; break; default: labelY=0; } } } private void drawScrollbars(Graphics g) { if(container!=null) { if ((scrollBarPolicy & VERTICAL_SCROLLBAR) == VERTICAL_SCROLLBAR && container.height > viewPortHeight) { // draw vertical scrollbar int rightHeight = height - navbarSize - titlebarSize; g.setColor(theme.getIntProperty("scrollbar.color")); int vpPosY = getViewPortPositionY(); scrollY = (rightHeight * (100 * (vpPosY + viewPortHeight / 2)) / container.height) / 100; int tl = theme.scrollLenght / 2; if (scrollY < tl || vpPosY == 0) scrollY = tl; else if (scrollY > rightHeight - tl || vpPosY == container.height - viewPortHeight) scrollY = rightHeight - tl; g.fillRect(width - theme.scrollSize + 1, titlebarSize + scrollY - tl, theme.scrollSize - 1, theme.scrollLenght); } if ((scrollBarPolicy & HORIZONTAL_SCROLLBAR) == HORIZONTAL_SCROLLBAR && container.width > viewPortWidth) { // draw vertical scrollbar int bottomWidth = width - decorLeft - decorRight; // draw scroll bar area. g.setColor(theme.getIntProperty("scrollbar.color")); int vpPosX = getViewPortPositionX(); scrollX = (bottomWidth * (100 * (vpPosX + viewPortWidth / 2)) / container.width) / 100; int tl = theme.scrollLenght / 2; if (scrollX < tl || vpPosX == 0) scrollX = tl; else if (scrollX > bottomWidth - tl || vpPosX == container.width - viewPortWidth) scrollX = bottomWidth - tl; g.fillRect(decorLeft + scrollX - tl, height - navbarSize + 1, theme.scrollLenght, theme.scrollSize - 1); } } } private void drawTitlebar(Graphics g) { if (titlebarSize == 0) return; if (titlebarImage == null) { titlebarImage = theme.getTitlebarTexture(width, titlebarSize); } if(titlebarImage!=null) g.drawImage(titlebarImage, 0, 0, Graphics.TOP | Graphics.LEFT); } private void drawNavbar(Graphics g) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -