border.java.svn-base

来自「j2me设计的界面包」· SVN-BASE 代码 · 共 726 行 · 第 1/2 页

SVN-BASE
726
字号
        if(pressedBorder != null) {            return pressedBorder;        }        switch(type) {            case TYPE_LINE:                return createLineBorder(thickness + 1, colorA);            case TYPE_ETCHED_LOWERED: {                Border b = createEtchedRaised(colorA, colorB);                b.themeColors = themeColors;                return b;            }            case TYPE_ETCHED_RAISED: {                Border b = createEtchedLowered(colorA, colorB);                b.themeColors = themeColors;                return b;            }            case TYPE_BEVEL_RAISED: {                Border b = createBevelLowered(colorA, colorB, colorC, colorD);                b.themeColors = themeColors;                return b;            }            case TYPE_BEVEL_LOWERED: {                Border b = createBevelRaised(colorA, colorB, colorC, colorD);                b.themeColors = themeColors;                return b;            }            case TYPE_ROUNDED: {                Border b = createRoundBorder(arcWidth, arcHeight, colorA);                b.themeColors = themeColors;                b.type = TYPE_ROUNDED_PRESSED;                return b;            }            case TYPE_ROUNDED_PRESSED: {                Border b = createRoundBorder(arcWidth, arcHeight, colorA);                b.themeColors = themeColors;                return b;            }        }        return this;    }        /**     * Has effect when the border demands responsiblity for background painting     * normally the painter will perform this work but in this case the border might     * do it instead.     *      * @param g graphics context to draw onto     * @param c component whose border should be drawn     */    public void paintBorderBackground(Graphics g, Component c) {        int originalColor = g.getColor();        int x = c.getX();        int y = c.getY();        int width = c.getWidth();        int height = c.getHeight();        switch(type) {            case TYPE_ROUNDED_PRESSED:                x++;                y++;                width -= 2;                height -= 2;            case TYPE_ROUNDED:                width--;                height--;                // rounded is also responsible for drawing the background                Style s = c.getStyle();                if(s.getBgImage() != null) {                    // we need to draw a background image!                    Image i = Image.createImage(width, height);                    Graphics imageG = i.getGraphics();                    imageG.setColor(0);                    imageG.fillRoundRect(0, 0, width, height, arcWidth, arcHeight);                    int[] rgb = i.getRGB();                    int transColor = rgb[0];                    int[] imageRGB = s.getBgImage().scaled(width, height).getRGB();                    for(int iter = 0 ; iter < rgb.length ; iter++) {                        if(rgb[iter] == transColor) {                            imageRGB[iter] = 0;                        }                    }                    g.drawImage(new RGBImage(imageRGB, width, height), x, y);                } else {                    int foreground = g.getColor();                    if(c.hasFocus()) {                        g.setColor(s.getBgSelectionColor());                    } else {                        g.setColor(s.getBgColor());                    }                    // Its opaque much easier job!                    if(s.getBgTransparency() == ((byte)0xff)) {                        g.fillRoundRect(x, y , width, height, arcWidth, arcHeight);                    } else {                        // if its transparent we don't need to do anything, if its                        // translucent... well....                        if(s.getBgTransparency() != 0) {                            Image i = Image.createImage(width, height);                            int[] imageRgb;                            if(g.getColor() != 0xffffff) {                                Graphics imageG = i.getGraphics();                                imageG.setColor(g.getColor());                                imageG.fillRoundRect(0, 0 , width, height, arcWidth, arcHeight);                                imageRgb = i.getRGB();                            } else {                                // background color is white we need to remove a different color                                // black is the only other "reliable" color on the device                                Graphics imageG = i.getGraphics();                                imageG.setColor(0);                                imageG.fillRect(0, 0, width, height);                                imageG.setColor(g.getColor());                                imageG.fillRoundRect(0, 0 , width, height, arcWidth, arcHeight);                                imageRgb = i.getRGB();                            }                            int removeColor = imageRgb[0];                            int size = width * height;                            int alphaInt = ((s.getBgTransparency() & 0xff) << 24) & 0xff000000;                            for(int iter = 0 ; iter < size ; iter++) {                                if(removeColor == imageRgb[iter]) {                                        imageRgb[iter] = 0;                                        continue;                                }                                if((imageRgb[iter] & 0xff000000) != 0) {                                    imageRgb[iter] = (imageRgb[iter] & 0xffffff) | alphaInt;                                }                               }                            g.drawImage(new RGBImage(imageRgb, width, height), x, y);                        }                     }                                        g.setColor(foreground);                }                break;            case TYPE_IMAGE:                int clipX = g.getClipX();                int clipY = g.getClipY();                int clipWidth = g.getClipWidth();                int clipHeight = g.getClipHeight();                Image topLeft = images[4];                 Image topRight = images[5];                Image bottomLeft = images[6];                Image center = images[8];                x += topLeft.getWidth();                y += topLeft.getHeight();                height -= (topLeft.getHeight() + bottomLeft.getHeight());                width -= (topLeft.getWidth() + topRight.getWidth());                g.clipRect(x, y, width, height);                int centerWidth = center.getWidth();                int centerHeight = center.getHeight();                for(int xCount = x ; xCount < x + width ; xCount += centerWidth) {                    for(int yCount = y ; yCount < y + height ; yCount += centerHeight) {                        g.drawImage(center, xCount, yCount);                    }                }                Image top = images[0];  Image bottom = images[1];                Image left = images[2]; Image right = images[3];                Image bottomRight = images[7];                                g.setClip(clipX, clipY, clipWidth, clipHeight);                                x = c.getX();                y = c.getY();                width = c.getWidth();                height = c.getHeight();                                g.drawImage(topLeft, x, y);                g.drawImage(bottomLeft, x, y + height - bottomLeft.getHeight());                g.drawImage(topRight, x + width - topRight.getWidth(), y);                g.drawImage(bottomRight, x + width - bottomRight.getWidth(), y + height - bottomRight.getHeight());                                g.setClip(clipX, clipY, clipWidth, clipHeight);                drawImageBorderLine(g, topLeft, topRight, top, x, y, width);                g.setClip(clipX, clipY, clipWidth, clipHeight);                drawImageBorderLine(g, bottomLeft, bottomRight, bottom, x, y + height - bottom.getHeight(), width);                g.setClip(clipX, clipY, clipWidth, clipHeight);                drawImageBorderColumn(g, topLeft, bottomLeft, left, x, y, height);                g.setClip(clipX, clipY, clipWidth, clipHeight);                drawImageBorderColumn(g, topRight, bottomRight, right, x + width - left.getWidth(), y, height);                                                g.setClip(clipX, clipY, clipWidth, clipHeight);                break;        }        g.setColor(originalColor);    }        /**     * Draws the border for the given component, this method is called before a call to     * background painting is made.     *      * @param g graphics context to draw onto     * @param c component whose border should be drawn     */    public void paint(Graphics g, Component c) {        int originalColor = g.getColor();        int x = c.getX();        int y = c.getY();        int width = c.getWidth();        int height = c.getHeight();        if(!themeColors) {            g.setColor(colorA);        }         switch(type) {            case TYPE_LINE:                width--;                height--;                for(int iter = 0 ; iter < thickness ; iter++) {                    g.drawRect(x + iter, y + iter, width, height);                    width -= 2; height -= 2;                }                break;            case TYPE_ROUNDED_PRESSED:                x++;                y++;                width -= 2;                height -= 2;            case TYPE_ROUNDED:                width--;                height--;                                                g.drawRoundRect(x, y , width, height, arcWidth, arcHeight);                break;            case TYPE_ETCHED_LOWERED:            case TYPE_ETCHED_RAISED:                g.drawRect(x, y, width - 2, height - 2);                if(themeColors) {                    if(type == TYPE_ETCHED_LOWERED) {                        g.lighterColor(40);                    } else {                        g.darkerColor(40);                    }                } else {                    g.setColor(colorB);                }                g.drawLine(x + 1, y + height - 3, x + 1, y +1);                g.drawLine(x + 1, y + 1, x + width - 3, y + 1);                g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);                g.drawLine(x + width - 1, y + height - 1, x + width - 1, y);                break;            case TYPE_BEVEL_RAISED:                if(themeColors) {                    g.setColor(getBackgroundColor(c));                    g.lighterColor(50);                } else {                    g.setColor(colorA);                }                g.drawLine(x, y, x, y + height - 2);                g.drawLine(x + 1, y, x + width - 2, y);                if(themeColors) {                    g.darkerColor(25);                } else {                    g.setColor(colorB);                }                g.drawLine(x + 1, y + 1, x + 1, y + height - 3);                g.drawLine(x + 2, y + 1, x + width - 3, y + 1);                if(themeColors) {                    g.darkerColor(50);                } else {                    g.setColor(colorC);                }                g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);                g.drawLine(x + width - 1, y, x + width - 1, y + height - 2);                if(themeColors) {                    g.darkerColor(25);                } else {                    g.setColor(colorD);                }                g.drawLine(x + 1, y + height - 2, x + width - 2, y + height - 2);                g.drawLine(x + width - 2, y + 1, x + width - 2, y + height - 3);                break;            case TYPE_BEVEL_LOWERED:                if(themeColors) {                    g.setColor(getBackgroundColor(c));                    g.darkerColor(50);                } else {                    g.setColor(colorD);                }                g.drawLine(x, y, x, y + height - 1);                g.drawLine(x + 1, y, x + width - 1, y);                if(themeColors) {                    g.lighterColor(25);                } else {                    g.setColor(colorC);                }                g.drawLine(x + 1, y + 1, x + 1, y + height - 2);                g.drawLine(x + 2, y + 1, x + width - 2, y + 1);                if(themeColors) {                    g.lighterColor(50);                } else {                    g.setColor(colorC);                }                g.drawLine(x + 1, y + height - 1, x + width - 1, y + height - 1);                g.drawLine(x + width - 1, y + 1, x + width - 1, y + height - 2);                if(themeColors) {                    g.lighterColor(25);                } else {                    g.setColor(colorA);                }                g.drawLine(x + 2, y + height - 2, x + width - 2, y + height - 2);                g.drawLine(x + width - 2, y + 2, x + width - 2, y + height - 3);                break;            case TYPE_IMAGE:                break;        }        g.setColor(originalColor);    }    private int getBackgroundColor(Component c) {        if(c.hasFocus()) {            return c.getStyle().getBgSelectionColor();        }        return c.getStyle().getBgColor();    }        private void drawImageBorderLine(Graphics g, Image left, Image right, Image center, int x, int y, int width) {        int currentWidth = width - right.getWidth();        if(currentWidth > 0) {            x += left.getWidth();            int destX = x + currentWidth;            g.clipRect(x, y, currentWidth - left.getWidth(), center.getHeight());            int centerWidth = center.getWidth();            for(; x < destX ; x += centerWidth) {                g.drawImage(center, x, y);            }        }    }    private void drawImageBorderColumn(Graphics g, Image top, Image bottom, Image center, int x, int y, int height) {        int currentHeight = height - bottom.getHeight();        if(currentHeight > 0) {            y += top.getHeight();            int destY = y + currentHeight;            g.clipRect(x, y, center.getWidth(), currentHeight - bottom.getHeight());            int centerHeight = center.getHeight();            for(; y < destY ; y += centerHeight) {                g.drawImage(center, x, y);            }        }    }        /**     * Sets the default border to the given value     *      * @param border new border value     */    public static void setDefaultBorder(Border border) {        defaultBorder = border;    }    /**     * Gets the default border to the given value     */    public static Border getDefaultBorder() {        return defaultBorder;    }}

⌨️ 快捷键说明

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