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

📄 taglayout.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        g.setColor(inverted ? Display.FG_H_COLOR : Display.FG_COLOR);        int nextIndex  = 0;        int nextOffset = 0;        int next = 0;        if (from > 0) {            next       = 2*(from-1);            nextIndex  = lineBreaks[next++];            nextOffset = lineBreaks[next++];        }        // if the first line contains an image that might        // be offset from the left we should start drawing         // from the first line of that image. Otherwise        // we do not know by how much to offset from the left        if ((items[nextIndex/2] instanceof ImageItem) &&             (nextIndex != 0) && ((nextIndex % 2) != 0)) {            y -= nextOffset;            from -= nextOffset/lineHeight;            if (from == 0) {                next = nextIndex = nextOffset = 0;            } else {                next = 2*(from-1);                nextIndex  = lineBreaks[next++];                nextOffset = lineBreaks[next++];            }        }        int xOffset = 0;        // to compensate for the fact that in the loop the first thing         // that will be done - move to a new line (which includes         // addition of lineHeight         y -= lineHeight;        int j = nextIndex;        int offset = nextOffset;        while(j < 2*numItems) {            // move to the next item if the end of current item was painted            if (j < nextIndex) {                j++;                                if (j >= 2*numItems) {                    break;                }                offset = 0;            }                        // move to next line if the end of current line was reached            if (j == nextIndex && offset == nextOffset) {                                if (y > yEnd || next > 2*numLines) {                    break;                }                xOffset = 0;                y += lineHeight;                nextIndex = lineBreaks[next++];                nextOffset = lineBreaks[next++];            }                        Item item = items[j/2];            Object obj = null;            // Get the next object that has to be painted;            // even numbers correspond to item's labels            // while odd numbers are Item's content (text or image)            if ((j % 2) == 0) {                obj = item.getLabel();            } else {                                   if (item instanceof StringItem) {                    obj = ((StringItem)item).getText();                } else if (item instanceof ImageItem) {                    obj = ((ImageItem)item).getImage();                }            }            if (obj == null) continue;                        // Next object is an Image            if (obj instanceof Image) {                Image img    = (Image)obj;                int imgWidth  = img.getWidth();                int imgHeight = img.getHeight();                int layout    = ((ImageItem)item).getLayout();                int rowsInImage = (imgHeight - offset + lineHeight - 1) /                                  lineHeight;                if (((layout & ImageItem.LAYOUT_NEWLINE_BEFORE) != 0) &&                    ((layout & ImageItem.LAYOUT_NEWLINE_AFTER) != 0)) {                    layout &= ImageItem.LAYOUT_CENTER;                    if (layout == ImageItem.LAYOUT_CENTER) {                        xOffset += Math.max(0, (width - xOffset - imgWidth)/2);                    } else if (layout == ImageItem.LAYOUT_RIGHT) {                        xOffset += Math.max(0, width - xOffset - imgWidth);                    }                }                int vPadding = ((imgHeight+lineHeight-1)/                                lineHeight*lineHeight-imgHeight)/2;                g.drawImage(img, xOffset, y - offset + vPadding,                             Graphics.TOP | Graphics.LEFT);                if (rowsInImage > 1) {                    next += 2*(rowsInImage-2);                    y += (rowsInImage-1)*lineHeight;                    nextIndex = lineBreaks[next++];                    nextOffset = lineBreaks[next++];                }                xOffset += img.getWidth();                offset = 0;            } else if (obj instanceof String) {                // Next object is a String                String str = (String)obj;                                int subStrLen = 0;                    // if next line starts with a different Item                // paint till the end of that item's label or text,                // otherwise paint till the next offset inside that                // item                if (next < 2*numLines && j == nextIndex) {                    subStrLen = nextOffset - offset;                } else {                    subStrLen = str.length() - offset;                }                                // paint only if there are are characters other                 // than '\n'                if (subStrLen > 0 && str.charAt(offset) != '\n') {                    int subStrLenWithLf = subStrLen;                     // dont' paint the last character '\n' and '\r'                    // on this line                    for (int last = offset+subStrLen-1; last >= offset;                        last--) {                        if (str.charAt(last) == '\n' ||                            str.charAt(last) == '\r') {                            subStrLenWithLf--;                        } else {                            break;                        }                    }                                        if (subStrLenWithLf > 0) {                        g.drawSubstring(str, offset, subStrLenWithLf,                                        xOffset, y,                                         Graphics.TOP | Graphics.LEFT);                                                xOffset += f.substringWidth(str, offset,                                                     subStrLenWithLf);                    }                }                                offset += subStrLen;            }        }    }    /**     * Get the line height of this layout     *     * @return int The height (in pixels) of one line of this layout     */    public int getLineHeight() {        return lineHeight;    }    /**     * Get the width of this layout     *     * @return int The width of this layout     */    public int getWidth() {        return width;    }    /**     * Get the height of this layout     *     * @return int The height of this layout     */    public int getHeight() {        return height;    }    /**     * Set the width of this layout     *     * @param w The new width of this layout     * @return int The height of this layout after setting the new width     */    public int setWidth(int w) {        if (width != w && w >= 0) {            width = w;            if (w == 0) {                numLines = 0;            } else if (w > 0) {                                updateLineBreaks(w);            }                        height = numLines*lineHeight;                        if (minHeight != -1 && height < minHeight) {                height = minHeight;            }                        if (maxHeight != -1 && height > maxHeight) {                height = maxHeight;            }        }        return height;    }    /**     * Set the minimum height for this layout     *     * @param h The new minimum height for this layout     */    void setMinHeight(int h) {        if (h < 0) {            h = 0;        }        minHeight = h;        if (height < minHeight) {            height = minHeight;        }    }    /**     * Set the maximum height for this layout     *     * @param h The new maximum height for this layout     */    public void setMaxHeight(int h) {        if (h < 0) {            h = 0;        }        maxHeight = h;        if (height > maxHeight) {            height = maxHeight;        }    }    /**     * Signal this layout that an item has changed     *     * @param item The Item which has changed     * @return int The new height based on the Item change     */    int itemContentChanged(Item item) {        return updateContent(get(item));    }    /**     * Add a line break in this layout     *     * @param index The index to insert the break     * @param offset     * @param last     * @return int     */    private int addBreak(short index, short offset, int last) {        if (last+1 < lineBreaks.length) {            short newBreaks[] = new short[lineBreaks.length + GROW_SIZE];            System.arraycopy(lineBreaks, 0, newBreaks, 0, lineBreaks.length);            lineBreaks = newBreaks;        }        lineBreaks[last++] = index;        lineBreaks[last++] = offset;        return last;    }    /**     * Update the line breaks of this layout with the given width     *     * @param width The new width to use to calculate line breaks     */    private void updateLineBreaks(int width) {        if (numItems == 0 || width <= 0) {            height = maxHeight > 0 ? maxHeight : 0;            numLines = lineBreaks[0] = lineBreaks[1] = 0;            return;        }         numLines = 0;        int curWidth   = 0;        int lastIndex  = 0;        for (int i = 0; i < 2*numItems; ++i) {            Item item = items[i/2];            Object obj = null;            // Get the next object that has to be laid out;            // even numbers correspond to item's labels

⌨️ 快捷键说明

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