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

📄 taglayout.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * @(#)TaGLayout.java	1.85 01/08/21 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information").  You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR tITS DERIVATIVES. */package javax.microedition.lcdui;/** * This class allows to group together multiple StringItems and ImageItems * (or in other words items which isGroupable() returns true. * *  Here is current LAYOUT POLICY: *  * Interactive items with null, empty or non-null label) * are always laid out on a new line. Interactive items are: * TextField, DateField, Gauge, ChoiceGroup *       * StringItems and ImageItems with NON-NULL and NOT EMPTY labels  * are laid out on a new line as well. *  * \n is treated as a line break. *  * ImageItem with NULL or EMPTY label is attempted to be placed * next to the content of the previous StringItem or ImageItem. * ImageItem is placed at the beginning of next line if one of the * following is true: * 1) its image is too wide to fit on this line * 2) that is the first item in the form * 3) its layout has LAYOUT_NEWLINE_BEFORE set * 4) there is an ImageItem before this one and its layout  *    has LAYOUT_NEWLINE_AFTER set *     * StringItem's text is attempted to be placed next to its label * (if label is NON-NULL and NOT EMPTY) or next to the content of * the previous StringItem (text) or ImageItem (image) * (if label is NULL or EMPTY). * - Text is placed at the beginning of the next line if one of the *   following is true: *   1) that is the first item in the form *   2) there is an ImageItem before this StringItem and its layout *      has LAYOUT_NEWLINE_AFTER set *   3) it is too wide to fit on this line and it cannot be broken *      into words (there are no spaces within text). * - If text is too wide to fit on this line and it can be broken *   into words then as many words as possible are put on this line *   and then as many words as possible go on the next line, etc. *    *   NOTE: There is no word concatenation with previous labels and texts. *   Label and Text part of StringItem imply implicit word break. *  * StringItems and ImageItems that are placed next to each other  * in a Form are scrolled together as a unit.  * * Form is also the one that actually controls the grouping and  * asks TaGLayout to insert/set/delete a groupable Item in the * corresponding TaGLayout. While TaGLayout makes is responsible to * the actual "contract" between groupable Item and Form  * (item.layout[0] points to correct TaGLayout while TaGLayout has  * item in its list of items). */class TaGLayout implements Layout {    /** The size to grow the internal Item array by when necessary */    private static final int GROW_SIZE = 4;    /** The set of Items in this layout */    private Item items[];    /** The number of Items in the set */    private int  numItems; // = 0;    /**     * This array holds information about line wrapping.     * A lot of information is encoded in that array.     * Each physical line on the screen has 2 entries in the     * lineBreaks array to represent a line break:     * 1) index of an item in the items array multiplied by 2      *    if that is a label part of the item, or     *    index of an item in the items array multiplied byt 2 plus 1     *    if that is the content part of the item.     *    Content could be String for StringItem or Image for ImageItem.     * 2) offset inside that part (label or content) of the item     *     * As you can see offset could be in a label or in the content.     * Even or oddness of the first element in the pair determines      * if offset applies to the content or the label.     *      * For example,     *     *      Form f = new Form();     *      f.append(new StringItem("Label1: ",      *                              "Here is some text "));     *      f.append("And more text");     *      f.append(new StringItem("Label2: ", "The end");      *     * Suppose on the screen it would look like:     *     *     *      --------------------     *      Label1: Here is some    <--- only item 0 is on that line     *      text And more text      <--- part of item 0 and all of item 1     *      Label2: The end         <--- all of item 2     *     * When layout is done (setWidth() is called on created     * TaGLayout for 3 StringItems)     * lineBreaks array will look the following way     * (note that line breaks represent first char (or image part) on     * next line or in other word until where chars or image appear      * still on this line):     *      * for line 0:     *   lineBreaks[0] = 1      <--- odd => content; 1/2=0=>index of item = 0     *   lineBreaks[1] = 13     <--- offset inside content (12th char is last     *                               on that line)     *     * for line 1:     *   lineBreaks[2] = 4      <--- even => label; 4/2=2=>index of item = 2      *   lineBreaks[3] = 0      <--- offset inside label      *                               (next line starts at the beg. of item 2)     *     * for line 2:     *   lineBreaks[4] = 6      <--- the last line break points to the     *   lineBreaks[5] = 0           number of items and to 0 offset     *     */    private short[] lineBreaks = {0, 0};    /** The number of lines in this layout */    private int numLines;  // = 0;    /** The width of this layout */    private int width = -1; // -1 when layout was not done yet    /** The height of this layout */    private int height;     // = 0;    /** The minimum height of this layout */    private int minHeight = -1;    /** The maximum height of this layout */    private int maxHeight = -1;    /** The Font used to render this layout */    private Font f;    /** The height (in pixels) of one line of this layout */    private int lineHeight;    /**     * Creates a new, empty TaGLayout object.     *     * @param font The Font to use when rendering     */    TaGLayout(Font font) {        f = font;        lineHeight = font.getHeight();        items = new Item[GROW_SIZE];    }    /**     * Returns the number of elements contained in the layout     *     * @return int The number of Items in this layout     */    int getSize() {         return numItems;    }    /**     * Sets the slot where oldItem is to contain the given item,      * replacing whatever was previously there.       * If <code> item </code> is null,      * a null element is placed into this slot instead of an item.     *     * @param oldItem the Item to replace     * @param item the new Item.     * @return delta of the height that was changed     */    int set(Item oldItem, Item item) {                    int index = get(oldItem);        item.layouts[0] = this;        items[index] = item;                return updateContent(index);                }        /**     * Inserts an item  just prior to the indicated item.  All     * subsequent elements are pushed to higher positions,      * and the element count increases by one.     *      * @param nextItem indicates the slot at which a new item is to be      *        inserted; if nextItem is null item will be appended.     * @param item The new Item to insert     * @return delta of the height that was changed     */    int insert(Item nextItem, Item item) {        int index = nextItem == null ? numItems : get(nextItem);        if (numItems == items.length) {            Item[] newItems = new Item[items.length + GROW_SIZE];            System.arraycopy(items, 0, newItems, 0, index);            System.arraycopy(items, index, newItems, index + 1,                              numItems - index);            items = newItems;                    } else {            System.arraycopy(items, index, items, index + 1,                              numItems - index);        }        item.layouts[0] = this;        items[index] = item;        numItems++;        return updateContent(index);    }            /**     * Deletes the element at the indicated position.  Elements beyond this     * position are moved down to a lower position, and the element count     * decreases by one.     *     * @param item The Item to be deleted.     * @return delta of the height that was changed     */    int delete(Item item) {        item.layouts[0] = null;        int index = get(item);        if (numItems > 1 && index != numItems - 1) {            System.arraycopy(items, index + 1,                              items, index, numItems - index - 1);        }        numItems--;        items[numItems] = null;        return updateContent(index);    }        /**     * Returns the index of the item within the layout.      *     * @param item which index is to be retrieved.     * @return int The index of the Item     */    int get(Item item) {        for (int i = 0; i < numItems; i++) {            if (items[i] == item) {                return i;            }        }                return -1;    }        /**     * Paints layout content to the provided graphics      * at the specified location.     *     * @param g graphics where TaGLayout should be painted.     * @param eraseBgrnd If True, erase the background before painting     * @param inverted If True, render with the colors inverted     */    public void paint(Graphics g, boolean eraseBgrnd, boolean inverted) {        if (numLines <= 0) {            return;        }         int from = 0;        int y = g.getClipY();        // if the start of the clip rect is outside of this layout         // object, no painting is needed.        if (y > height) {            return;        }        int yEnd = y + g.getClipHeight();        // if the height of the layout to be painted is less than the        // end of the clip rect, then paint up till the height only        if (yEnd > height) {          yEnd = height;        }        if (y < 0) {            y = 0;        } else if (y > 0) {            from = y/lineHeight;            if (from >= numLines) {                return;            }            y = from * lineHeight;            if (y > yEnd) {                return;            }        }        if (eraseBgrnd) {            g.setColor(inverted ? Display.BG_H_COLOR : Display.ERASE_COLOR);            g.fillRect(0, y, width, yEnd);        }        g.setFont(f);

⌨️ 快捷键说明

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