⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 component.java

📁 j2me下的1套UI框架.包含j2me开发中会应用的各种低级组件
💻 JAVA
字号:
package com.jmobilecore.ui.core;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Font;

/**
 * A <em>component</em> is an object having a graphical representation
 * that can be displayed on the screen and that can interact with the
 * user. Examples of components are the buttons, checkboxes, and scrollbars
 * of a typical graphical user interface. <p>
 * The <code>Component</code> class is the abstract superclass of
 * the User Interface components. Class <code>Component</code> can also
 * be extended directly to create a custom component.
 *
 * @author Greg Gridin
 */
abstract public class Component {

    /**
     * Indicates that the component should be left justified.
     */
    public static final int LEFT = 0;

    /**
     * Indicates that the component should be centered.
     */
    public static final int CENTER = 1;

    /**
     * Indicates that the component should be right justified.
     */
    public static final int RIGHT = 2;

    /**
     * The label's alignment.  The default alignment is set
     * to be left justified.
     */
    public int alignment = LEFT;

    /**
     * The height of the component.
     */
    protected int height = 0;

    public boolean focusable = false;

    /**
     * The width of the component.
     */
    public int width = ScreenCanvas.WIDTH;

    /**
     * The foreground color for this component.
     */
    public int foreground = Style.TEXT_COLOR;

    /**
     * The background color for this component.
     */
    public int background = Style.BACKGROUND_COLOR;

    /**
     * The foreground color for this component when it focused.
     */
    public int focusedForeground = Style.FOCUSED_TEXT_COLOR;

    /**
     * The background color for this component when it focused.
     */
    public int focusedBackground = Style.FOCUSED_BACKGROUND_COLOR;

    /**
     * The font used by this component.
     * The <code>font</code> can be <code>null</code>.
     *
     * @see #setFont
     */
    protected Font font = Style.TEXT_FONT;

    /**
     * Indicated if component should be repainted
     *
     * @see #invalidate
     */
    protected boolean valid = false;

    /**
     * Vertical offset in pixels of current component from top of the window
     */
    protected int screenY = 0;

    /**
     * Indicated if component is completely or partially visible on the screen
     */
    protected boolean isVisible = false;

    /**
     * Indicated if component is completely visible on the screen
     */
    protected boolean isCompletelyVisible = false;

    /**
     * Indicated if component is focusable
     *
     * @see #isFocusOwner
     * @see #requestFocus
     * @see #releaseFocus
     */
    protected boolean isFocused = false;

    /**
     * The parent screen of the object.
     */
    protected ScreenCanvas parentScreen = null;

    /**
     * Returns the current height of this component.
     *
     * @return the current height of this component
     */
    public int getHeight() {
        return height;
    }

    /**
     * Returns the current width of this component.
     *
     * @return the current width of this component
     */
    public int getWidth() {
        return width;
    }

    /**
     * Returns <code>true</code> if this <code>Component</code> is the
     * focus owner.
     *
     * @return <code>true</code> if this <code>Component</code> is the
     *         focus owner; <code>false</code> otherwise
     */
    public boolean isFocusOwner() {
        return isFocused;
    }

    /**
     * Processes a press of a key.
     */
    protected boolean keyPressed(int keyCode) {
        return false;
    }

    /**
     * Processes a release of a key.
     */
    protected boolean keyReleased(int keyCode) {
        return false;
    }

    /**
     * Processes when a key is repeated (held down).
     */
    protected boolean keyRepeated(int keyCode) {
        return false;
    }

    /**
     * Processes when a pointer is dragged (held down).
     */
    protected boolean pointerDragged(int x, int y) {
        return false;
    }

    /**
     * Processes a press of a pointer.
     */
    protected boolean pointerPressed(int x, int y) {
        return false;
    }

    /**
     * Processes a release of a pointer.
     */
    protected boolean pointerReleased(int x, int y) {
        return false;
    }

    /**
     * Paints this component.
     *
     * @param g the graphics context to use for painting
     */
    public void paint(Graphics g) {

        valid = true;
    }

    /**
     * Sets the focusable state of this Component to the specified value.
     * If component is not focusable then focusable state won't be changed
     *
     * @see #isFocusOwner
     * @see #releaseFocus
     */
    public void requestFocus() {
        if (focusable) isFocused = true;
    }

    public void releaseFocus() {
        if (focusable) isFocused = false;
    }

    /**
     * Sets the font of this component.
     *
     * @param font the font to become this component's font;
     *             if this parameter is <code>null</code> then this
     *             component font won't be changed
     */
    public void setFont(Font font) {
        this.font = font;
        if (font != null) {
            setHeight();
        } else {
            height = 0;
        }
    }

    /**
     * Get the component's font
     *
     * @return the current font
     */
    public Font getFont() {
        return font;
    }

    /**
     * Get foreground
     */
    public int getForeground() {
        return isFocusOwner() ? focusedForeground : foreground;
    }

    /**
     * Get background
     */
    public int getBackground() {
        return isFocusOwner() ? focusedBackground : background;
    }

    /**
     * Paints background for the component
     *
     * @param g the Graphics context
     */
    protected void paintBackground(Graphics g) {
        if (getBackground() != Color.TRANSPARENT) {
            g.setColor(getBackground());
            g.fillRect(0, screenY, getWidth(), getHeight());
        }
    }

    /**
     * Prepares foreground for painting
     *
     * @param g the Graphics context
     */
    protected void prepareForeground(Graphics g) {
        if (getForeground() != Color.TRANSPARENT) g.setColor(getForeground());
        if (getFont() != null) g.setFont(font);

    }

    /**
     * Calculates the component height
     */
    protected void setHeight() {
        height = font.getHeight() + 2 * Style.V_GAP;
    }

    /**
     * Invalidates this component.
     */
    public void invalidate() {
        valid = false;
    }

    /**
     * Default destructor. Helps VM to perform garbage collection
     */
    public void destructor() {
    };

}  // class Component

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -