📄 component.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 * *//* * Created on Feb 22, 2008 */package gr.fire.core;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;/** * @author padeler * */public class Component{ protected Component parent; private String id; /** Constrains for this object, used by the layoutManager */ Object constrains=null; /** * Location of the component inside its parent container. * These coordinates are refer to the top left corner of this component, relative to the * top left corder (0,0) of the parent container. */ int x,y; /** * The actual dimensions of this component */ protected int width,height; /** * The font used by this component when displaying text. */ protected Font font; /** * The layout of this Component's contents i.e (FireScreen.TOP|FireScreen.LEFT) */ protected int layout; protected int foregroundColor; protected int backgroundColor; /** * Padding is the distance of this component's border from its content * The Component is responsible for rendering correctly the padding. protected int paddingLeft; protected int paddingTop; protected int paddingRight; protected int paddingBottom; /** * The internal padding between the component's elements protected int paddingVertical; protected int paddingHorizontal; /** * The margin is the distance of the component from its neighbors. * The parent container is responsible for rendering the margins correctly. protected int marginLeft; protected int marginTop; protected int marginRight; protected int marginBottom; */ /** If the Component is not valid, its characteristics (with,height,etc) need validation */ protected boolean valid; /** If it can be focused (traversed) by the user */ private boolean focusable; /** * If this component is in animation mode then the animation is held in this field. */ Animation animation=null; /** If the component is currently selected, i.e. the curson is on it */ boolean selected; /** * Dimensions of the component that control its layout inside a Container. */ private int prefWidth=-1,prefHeight=-1; protected CommandListener commandListener; protected KeyListener keyListener; protected PointerListener pointerListener; protected Command command; protected Command leftSoftKeyCommand; protected Command rightSoftKeyCommand; public Component() { Theme t = FireScreen.getTheme(); backgroundColor = t.componentBackgroundColor; foregroundColor = t.componentForegroundColor; } /** * Paint is called by the container of the component to allow it to draw itself on Graphics g * The drawable area on g is (0,0,width,height). * * @param g the area on witch the component will draw it self. */ public void paint(Graphics g) { } /** * Sets this component on/off selected mode. * @param v */ public void setSelected(boolean v){selected = v;} public boolean isSelected(){return selected;} /** * A validate event requests from the component to recalculate its internal properties such as width/height etc. */ public void validate(){valid=true;} public int getHeight() { return height; } public void setHeight(int height) { if(height<0) throw new IllegalArgumentException("Height cannot be negative."); this.height = height; } public int getWidth() { return width; } public void setWidth(int width) { if(width<0) throw new IllegalArgumentException("Width cannot be negative."); this.width = width; } /** * set a command to this component. * @param c */ public void setCommand(Command c) { command=c; focusable= (focusable||commandListener!=null||keyListener!=null||pointerListener!=null||command!=null); } public void setCommandListener(CommandListener listener) { this.commandListener=listener; focusable= (focusable||commandListener!=null||keyListener!=null||pointerListener!=null||command!=null); } public void setKeyListener(KeyListener listener) { this.keyListener=listener; focusable= (focusable||commandListener!=null||keyListener!=null||pointerListener!=null||command!=null); } public void setPointerListener(PointerListener listener) { this.pointerListener=listener; focusable= (focusable||commandListener!=null||keyListener!=null||pointerListener!=null||command!=null); } public boolean isFocusable() { return focusable; } /** * Checks if the point (x,y) is inside this Component. The point must be on the coordinate system of this Component. * That means that the top left corner of the component is (0,0). * @param x * @param y * @return */ public boolean contains(int x,int y) { return (x>=0 && y>=0 && x<width && y<height); } /** * Determines whether or not this <code>Component</code> and the specified * rectangular area intersect. Two rectangles intersect if their * intersection is nonempty. * * @return <code>true</code> if the specified area and this * <code>Component</code> intersect; <code>false</code> * otherwise. */ public boolean intersects(int rx, int ry, int rw, int rh) { int tw = width; int th = height; if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) { return false; } int tx = x; int ty = y; rw += rx; rh += ry; tw += tx; th += ty; // overflow || intersect return ((rw < rx || rw > tx) && (rh < ry || rh > ty) && (tw < tx || tw > rx) && (th < ty || th > ry)); } public void repaint() { repaint(0,0, width, height); } /** * Get the X position of this component inside its parent. * @return */ public int getX() { return x; } /** * Set the X position of this component inside its parent. * @param x */ public void setX(int x) { this.x = x; } /** * Get the Y position of this component inside its parent. * @return */ public int getY() { return y; } /** * Set the Y position of this component inside its parent. * @param y */ public void setY(int y) { this.y = y; } void repaint(int cx,int cy,int cwidth,int cheight) { if(parent!=null) {// System.out.println("Repaint for "+cx+","+cy); parent.repaint(x+cx, y+cy, cwidth, cheight); } else { // top level component FireScreen screen = FireScreen.getScreen();// System.out.println("TOP LEVEL Repaint for "+cx+","+cy+". "+cwidth+","+cheight); if(screen!=null) screen.repaint(x+cx,y+cy,cwidth,cheight); } } public int[] getPrefSize() { if(prefWidth==-1 || prefHeight==-1) return null; return new int[]{prefWidth,prefHeight}; } public void setPrefSize(int width,int height,boolean isPercent) { if (width < 0 || height < 0) throw new IllegalArgumentException("Dimensions can not be negative: "+width+"/"+height); if(isPercent && (width>100 || height>100)) throw new IllegalArgumentException("Dimensions in percentage can only be between 0 and 100."); if(isPercent) { FireScreen screen = FireScreen.getScreen(); if(screen==null) throw new IllegalStateException("FireScreen is not yet innitialized."); int w = screen.getWidth(); int h = screen.getHeight(); prefWidth = (width*w)/100; prefHeight = (height*h)/100; } else { prefWidth = width; prefHeight = height; } } protected void pointerDragged(int x, int y) { if(pointerListener!=null) pointerListener.pointerDragged(x, y,this); } protected void pointerPressed(int x, int y) { if(pointerListener!=null) pointerListener.pointerPressed(x, y,this); } protected void pointerReleased(int x, int y) { if(pointerListener!=null) pointerListener.pointerReleased(x, y,this); } protected void keyPressed(int keyCode) { if(keyListener!=null) keyListener.keyPressed(keyCode,this); } protected void keyReleased(int keyCode) { if(keyCode==FireScreen.leftSoftKey && leftSoftKeyCommand!=null && commandListener!=null) { commandListener.commandAction(leftSoftKeyCommand, this); return; } if(keyCode==FireScreen.rightSoftKey && rightSoftKeyCommand!=null && commandListener!=null) { commandListener.commandAction(rightSoftKeyCommand, this); return; } if(keyListener!=null) keyListener.keyReleased(keyCode,this); } protected void keyRepeated(int keyCode) { if(keyListener!=null) keyListener.keyRepeated(keyCode,this); } public Command getLeftSoftKeyCommand() { return leftSoftKeyCommand; } public void setLeftSoftKeyCommand(Command leftSoftKeyCommand) { this.leftSoftKeyCommand = leftSoftKeyCommand; } public Command getRightSoftKeyCommand() { return rightSoftKeyCommand; } public void setRightSoftKeyCommand(Command rightSoftKeyCommand) { this.rightSoftKeyCommand = rightSoftKeyCommand; } public Component getParent() { return parent; } public int getForegroundColor() { return foregroundColor; } public void setForegroundColor(int foregroundColor) { this.foregroundColor = foregroundColor; } public int getBackgroundColor() { return backgroundColor; } public void setBackgroundColor(int backgroundColor) { this.backgroundColor = backgroundColor; } /** * The minSize is used to calculate the layout of the components inside their container by the LayoutManager. * The minSize is only used when the prefSize is not set. * If the prefSize is set it will be used even if the minSize is bigger than prefSize. * * @return the minSize dimensions of this Component. */ public int[] getMinSize() { return new int[]{0,0}; } public Font getFont() { return font; } public void setFont(Font font) { this.font = font; } public int getLayout() { return layout; } public void setLayout(int layout) { this.layout = layout; } public int getContentWidth(){ return 0; } public int getContentHeight() { return 0; } public void setFocusable(boolean focusable) { this.focusable = focusable; } public void setId(String id) { this.id = id; } public String getId() { return id; } public String toString() { return super.toString() + ((id!=null)?" ["+id+"]":""); } public int getValign() { int valign = FireScreen.TOP; if ((layout&FireScreen.VCENTER)==FireScreen.VCENTER) { valign = FireScreen.VCENTER; } else if ((layout&FireScreen.TOP) == FireScreen.TOP) { valign = FireScreen.TOP; } else if ((layout&FireScreen.BOTTOM) == FireScreen.BOTTOM) { valign = FireScreen.BOTTOM; } return valign; } public int getHalign() { int halign = FireScreen.LEFT; if ((layout&FireScreen.CENTER)==FireScreen.CENTER) // hcenter { halign = FireScreen.CENTER; } else if ((layout&FireScreen.LEFT)==FireScreen.LEFT) { halign = FireScreen.LEFT; } else if ((layout&FireScreen.RIGHT)==FireScreen.RIGHT) { halign = FireScreen.RIGHT; } return halign; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -