Search in sources :

Example 6 with Component

use of com.codename1.ui.Component in project CodenameOne by codenameone.

the class Border method paintBorderBackground.

private void paintBorderBackground(Graphics g, final int xParameter, final int yParameter, final int widthParameter, final int heightParameter, Component c) {
    int originalColor = g.getColor();
    int x = xParameter;
    int y = yParameter;
    int width = widthParameter;
    int height = heightParameter;
    switch(type) {
        case TYPE_ROUNDED_PRESSED:
            x++;
            y++;
            width -= 2;
            height -= 2;
        case TYPE_ROUNDED:
            // Removing this due to issue 301, not sure regarding this...
            // width--;
            // height--;
            // rounded is also responsible for drawing the background
            Style s = c.getStyle();
            if ((s.getBgImage() != null && s.getBackgroundType() == Style.BACKGROUND_IMAGE_SCALED) || s.getBackgroundType() > 1) {
                Object w = s.roundRectCache;
                Image i = null;
                if (w != null) {
                    i = (Image) Display.getInstance().extractHardRef(w);
                }
                if (i != null && i.getWidth() == width && i.getHeight() == height) {
                    g.drawImage(i, x, y);
                } else {
                    // we need to draw a background image!
                    i = Image.createImage(width, height);
                    Graphics imageG = i.getGraphics();
                    imageG.setColor(0);
                    imageG.fillRoundRect(0, 0, width, height, arcWidth, arcHeight);
                    int[] rgb = i.getRGBCached();
                    int transColor = rgb[0];
                    int[] imageRGB;
                    if (s.getBackgroundType() == Style.BACKGROUND_IMAGE_SCALED) {
                        imageRGB = s.getBgImage().scaled(width, height).getRGBCached();
                    } else {
                        Image bgPaint = Image.createImage(width, height);
                        Painter p = s.getBgPainter();
                        // might occur during temporary conditions in the theme switching
                        if (p == null) {
                            return;
                        }
                        p.paint(bgPaint.getGraphics(), new Rectangle(0, 0, width, height));
                        imageRGB = bgPaint.getRGB();
                    }
                    int rlen = rgb.length;
                    for (int iter = 0; iter < rlen; iter++) {
                        if (rgb[iter] == transColor) {
                            imageRGB[iter] = 0;
                        }
                    }
                    i = Image.createImage(imageRGB, width, height);
                    s.roundRectCache = Display.getInstance().createSoftWeakRef(i);
                    g.drawImage(i, x, y);
                }
            } else {
                int foreground = g.getColor();
                g.setColor(s.getBgColor());
                // Its opaque much easier job!
                if (s.getBgTransparency() == ((byte) 0xff)) {
                    g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
                } else {
                    if (g.isAlphaSupported()) {
                        int alpha = g.getAlpha();
                        g.setAlpha(s.getBgTransparency() & 0xff);
                        g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
                        g.setAlpha(alpha);
                    } else {
                        // 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.getRGBCached();
                            } 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.getRGBCached();
                            }
                            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:
            {
                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());
                if (center != null) {
                    g.tileImage(center, x, y, width, height);
                }
                Image top = images[0];
                Image bottom = images[1];
                Image left = images[2];
                Image right = images[3];
                Image bottomRight = images[7];
                x = xParameter;
                y = yParameter;
                width = widthParameter;
                height = heightParameter;
                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());
                Image arrowDownImage = null;
                Image arrowUpImage = null;
                Image arrowLeftImage = null;
                Image arrowRightImage = null;
                int arrowPosition = 0;
                // we need to draw an arrow on one of the sides
                if (trackComponent != null) {
                    int cabsY = c.getAbsoluteY();
                    int trackY = trackComponent.getY();
                    int trackX = trackComponent.getX();
                    int cabsX = c.getAbsoluteX();
                    if (cabsY >= trackY + trackComponent.getHeight()) {
                        // we are below the component
                        arrowUpImage = specialTile[0];
                        arrowPosition = (trackX + trackComponent.getWidth() / 2) - cabsX - arrowUpImage.getWidth() / 2;
                    } else {
                        if (cabsY + c.getHeight() <= trackY) {
                            // we are above the component
                            arrowDownImage = specialTile[1];
                            arrowPosition = (trackX + trackComponent.getWidth() / 2) - cabsX - arrowDownImage.getWidth() / 2;
                        } else {
                            if (cabsX >= trackX + trackComponent.getWidth()) {
                                // we are to the right of the component
                                arrowLeftImage = specialTile[2];
                                arrowPosition = (trackY + trackComponent.getHeight() / 2) - cabsY - arrowLeftImage.getHeight() / 2;
                            } else {
                                if (cabsX + c.getWidth() <= trackX) {
                                    // we are to the left of the component
                                    arrowRightImage = specialTile[3];
                                    arrowPosition = (trackY + trackComponent.getHeight() / 2) - cabsY - arrowRightImage.getHeight() / 2;
                                }
                            }
                        }
                    }
                }
                drawImageBorderLine(g, topLeft, topRight, top, x, y, width, arrowUpImage, arrowPosition, false);
                drawImageBorderLine(g, bottomLeft, bottomRight, bottom, x, y + height - bottom.getHeight(), width, arrowDownImage, arrowPosition, true);
                drawImageBorderColumn(g, topLeft, bottomLeft, left, x, y, height, arrowLeftImage, arrowPosition, false);
                drawImageBorderColumn(g, topRight, bottomRight, right, x + width - right.getWidth(), y, height, arrowRightImage, arrowPosition, true);
                break;
            }
        case TYPE_IMAGE_SCALED:
            {
                int clipX = g.getClipX();
                int clipY = g.getClipY();
                int clipWidth = g.getClipWidth();
                int clipHeight = g.getClipHeight();
                // g.pushClip();
                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);
                if (center != null && width > 0 && height > 0) {
                    int centerWidth = center.getWidth();
                    int centerHeight = center.getHeight();
                    g.drawImage(center, x, y, width, height);
                }
                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);
                // g.popClip();
                // g.pushClip();
                x = xParameter;
                y = yParameter;
                width = widthParameter;
                height = heightParameter;
                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());
                drawImageBorderLineScale(g, topLeft, topRight, top, x, y, width);
                drawImageBorderLineScale(g, bottomLeft, bottomRight, bottom, x, y + height - bottom.getHeight(), width);
                drawImageBorderColumnScale(g, topLeft, bottomLeft, left, x, y, height);
                drawImageBorderColumnScale(g, topRight, bottomRight, right, x + width - right.getWidth(), y, height);
                g.setClip(clipX, clipY, clipWidth, clipHeight);
                // g.popClip();
                break;
            }
        case TYPE_IMAGE_HORIZONTAL:
            {
                Image left = images[0];
                Image right = images[1];
                Image center = images[2];
                if (c.getUIManager().isThemeConstant("centerAlignHBorderBool", false)) {
                    y += Math.max(0, height / 2 - center.getHeight() / 2);
                }
                g.drawImage(left, x, y);
                g.drawImage(right, x + width - right.getWidth(), y);
                g.tileImage(center, x + left.getWidth(), y, width - left.getWidth() - right.getWidth(), center.getHeight());
                break;
            }
        case TYPE_IMAGE_VERTICAL:
            {
                Image top = images[0];
                Image bottom = images[1];
                Image center = images[2];
                g.drawImage(top, x, y);
                g.drawImage(bottom, x, y + height - bottom.getHeight());
                g.tileImage(center, x, y + top.getHeight(), center.getWidth(), height - top.getHeight() - bottom.getHeight());
                break;
            }
    }
    g.setColor(originalColor);
}
Also used : Graphics(com.codename1.ui.Graphics) Painter(com.codename1.ui.Painter) Rectangle(com.codename1.ui.geom.Rectangle) RGBImage(com.codename1.ui.RGBImage) Image(com.codename1.ui.Image) RGBImage(com.codename1.ui.RGBImage)

Example 7 with Component

use of com.codename1.ui.Component in project CodenameOne by codenameone.

the class RoundRectBorder method paintBorderBackground.

@Override
public void paintBorderBackground(Graphics g, final Component c) {
    final int w = c.getWidth();
    final int h = c.getHeight();
    int x = c.getX();
    int y = c.getY();
    if (shadowOpacity == 0) {
        Style s = c.getStyle();
        if (s.getBgImage() == null) {
            byte type = s.getBackgroundType();
            if (type == Style.BACKGROUND_IMAGE_SCALED || type == Style.BACKGROUND_NONE) {
                GeneralPath gp = createShape(w, h);
                byte bgt = c.getStyle().getBgTransparency();
                if (bgt != 0) {
                    int a = g.getAlpha();
                    g.setAlpha(bgt & 0xff);
                    g.setColor(s.getBgColor());
                    g.translate(x, y);
                    g.fillShape(gp);
                    if (this.stroke != null && strokeOpacity > 0 && strokeThickness > 0) {
                        g.setAlpha(strokeOpacity);
                        g.setColor(strokeColor);
                        g.drawShape(gp, this.stroke);
                    }
                    g.translate(-x, -y);
                    g.setAlpha(a);
                }
                if (this.stroke != null && strokeOpacity > 0 && strokeThickness > 0) {
                    int a = g.getAlpha();
                    g.setAlpha(strokeOpacity);
                    g.setColor(strokeColor);
                    g.translate(x, y);
                    g.drawShape(gp, this.stroke);
                    g.translate(-x, -y);
                    g.setAlpha(a);
                }
                return;
            }
        }
    }
    if (w > 0 && h > 0) {
        Image background = (Image) c.getClientProperty(CACHE_KEY + instanceVal);
        if (background != null && background.getWidth() == w && background.getHeight() == h) {
            g.drawImage(background, x, y);
            return;
        }
    } else {
        return;
    }
    Image target = createTargetImage(c, w, h, true);
    g.drawImage(target, x, y);
    c.putClientProperty(CACHE_KEY + instanceVal, target);
    // update the cache with a more refined version and repaint
    Display.getInstance().callSeriallyOnIdle(new Runnable() {

        public void run() {
            if (w == c.getWidth() && h == c.getHeight()) {
                Image target = createTargetImage(c, w, h, false);
                c.putClientProperty(CACHE_KEY + instanceVal, target);
                c.repaint();
            }
        }
    });
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath) Image(com.codename1.ui.Image)

Example 8 with Component

use of com.codename1.ui.Component in project CodenameOne by codenameone.

the class DefaultLookAndFeel method drawLabelText.

/**
 * Draws the text of a label
 *
 * @param g graphics context
 * @param l label component
 * @param text the text for the label
 * @param x position for the label
 * @param y position for the label
 * @param textSpaceW the width available for the component
 * @return the space used by the drawing
 */
protected int drawLabelText(Graphics g, Label l, String text, int x, int y, int textSpaceW) {
    Style style = l.getStyle();
    Font f = style.getFont();
    boolean rtl = l.isRTL();
    boolean isTickerRunning = l.isTickerRunning();
    int txtW = l.getStringWidth(f);
    if ((!isTickerRunning) || rtl) {
        // if there is no space to draw the text add ... at the end
        if (txtW > textSpaceW && textSpaceW > 0) {
            if (rtl) {
                if ((!isTickerRunning) && (l.isEndsWith3Points())) {
                    String points = "...";
                    int pointsW = f.stringWidth(points);
                    g.drawString(points, l.getShiftText() + x, y, l.getStyle().getTextDecoration());
                    g.clipRect(pointsW + l.getShiftText() + x, y, textSpaceW - pointsW, f.getHeight());
                }
                x = x - txtW + textSpaceW;
            } else {
                if (l.isEndsWith3Points()) {
                    String points = "...";
                    int index = 1;
                    int widest = f.charWidth('W');
                    int pointsW = f.stringWidth(points);
                    int tlen = text.length();
                    while (fastCharWidthCheck(text, index, textSpaceW - pointsW, widest, f) && index < tlen) {
                        index++;
                    }
                    text = text.substring(0, Math.min(text.length(), Math.max(1, index - 1))) + points;
                    txtW = f.stringWidth(text);
                }
            }
        }
    }
    g.drawString(text, l.getShiftText() + x, y, style.getTextDecoration());
    return Math.min(txtW, textSpaceW);
}
Also used : Font(com.codename1.ui.Font)

Example 9 with Component

use of com.codename1.ui.Component in project CodenameOne by codenameone.

the class RoundBorder method paintBorderBackground.

@Override
public void paintBorderBackground(Graphics g, final Component c) {
    final int w = c.getWidth();
    final int h = c.getHeight();
    int x = c.getX();
    int y = c.getY();
    if (w > 0 && h > 0) {
        Object k = c.getClientProperty(CACHE_KEY + instanceVal);
        if (k instanceof CacheValue) {
            CacheValue val = (CacheValue) k;
            if (val.modificationTime == modificationTime && val.img.getWidth() == w && val.img.getHeight() == h) {
                g.drawImage(val.img, x, y);
                return;
            }
        }
    } else {
        return;
    }
    Image target = createTargetImage(c, w, h, true);
    g.drawImage(target, x, y);
    c.putClientProperty(CACHE_KEY + instanceVal, new CacheValue(target, modificationTime));
    // update the cache with a more refined version and repaint
    Display.getInstance().callSeriallyOnIdle(new Runnable() {

        public void run() {
            if (w == c.getWidth() && h == c.getHeight()) {
                Image target = createTargetImage(c, w, h, false);
                c.putClientProperty(CACHE_KEY + instanceVal, new CacheValue(target, modificationTime));
                c.repaint();
            }
        }
    });
}
Also used : Image(com.codename1.ui.Image)

Example 10 with Component

use of com.codename1.ui.Component in project CodenameOne by codenameone.

the class GlassTutorial method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g, Rectangle rect) {
    if (internal == null) {
        internal = new Label(" ");
        internal.setUIID("GlassTutorial");
    }
    internal.setSize(rect.getSize());
    internal.paintComponent(g);
    int componentCount = vec.size();
    for (int iter = 0; iter < componentCount; iter++) {
        Component current = (Component) vec.elementAt(iter);
        String pos = (String) current.getClientProperty(POS);
        Component dest = (Component) current.getClientProperty(DEST);
        int xpos = dest.getAbsoluteX();
        int ypos = dest.getAbsoluteY();
        int w = dest.getWidth();
        int h = dest.getHeight();
        if (pos.equals(BorderLayout.CENTER)) {
            current.setX(xpos);
            current.setY(ypos);
            current.setWidth(w);
            current.setHeight(h);
            current.paintComponent(g);
            continue;
        }
        Dimension d = current.getPreferredSize();
        current.setWidth(d.getWidth());
        current.setHeight(d.getHeight());
        if (pos.equals(BorderLayout.SOUTH)) {
            current.setX(xpos + w / 2 - d.getWidth() / 2);
            current.setY(ypos + h);
            current.paintComponent(g);
            continue;
        }
        if (pos.equals(BorderLayout.NORTH)) {
            current.setX(xpos + w / 2 - d.getWidth() / 2);
            current.setY(ypos - d.getHeight());
            current.paintComponent(g);
            continue;
        }
        if (pos.equals(BorderLayout.EAST)) {
            current.setX(xpos + w);
            current.setY(ypos + h / 2 - d.getHeight() / 2);
            current.paintComponent(g);
            continue;
        }
        if (pos.equals(BorderLayout.WEST)) {
            current.setX(xpos - d.getWidth());
            current.setY(ypos + h / 2 - d.getHeight() / 2);
            current.paintComponent(g);
            continue;
        }
    }
}
Also used : Label(com.codename1.ui.Label) Dimension(com.codename1.ui.geom.Dimension) Component(com.codename1.ui.Component)

Aggregations

Component (com.codename1.ui.Component)152 Style (com.codename1.ui.plaf.Style)61 Container (com.codename1.ui.Container)55 Form (com.codename1.ui.Form)41 BorderLayout (com.codename1.ui.layouts.BorderLayout)40 TextArea (com.codename1.ui.TextArea)31 ActionEvent (com.codename1.ui.events.ActionEvent)28 Dimension (com.codename1.ui.geom.Dimension)28 Label (com.codename1.ui.Label)25 Point (com.codename1.ui.geom.Point)22 ArrayList (java.util.ArrayList)22 Rectangle (com.codename1.ui.geom.Rectangle)21 Vector (java.util.Vector)18 Button (com.codename1.ui.Button)16 Dialog (com.codename1.ui.Dialog)16 Hashtable (java.util.Hashtable)16 Image (com.codename1.ui.Image)15 TextField (com.codename1.ui.TextField)15 ActionListener (com.codename1.ui.events.ActionListener)13 BoxLayout (com.codename1.ui.layouts.BoxLayout)13