Search in sources :

Example 1 with RGBImage

use of com.codename1.ui.RGBImage 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 2 with RGBImage

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

the class CommonTransitions method initTransition.

/**
 * {@inheritDoc}
 */
public void initTransition() {
    firstFinished = false;
    if (transitionType == TYPE_EMPTY) {
        return;
    }
    startTime = System.currentTimeMillis();
    Component source = getSource();
    Component destination = getDestination();
    position = 0;
    int w = source.getWidth();
    int h = source.getHeight();
    // improper replace() calls, this may still be valid and shouldn't fail
    if (w <= 0 || h <= 0) {
        return;
    }
    // nothing to prepare in advance  for a shift fade transition
    if (transitionType == TYPE_SLIDE_AND_FADE) {
        if (getSource() instanceof Form && getDestination() instanceof Form) {
            motion = createMotion(100, 200, speed);
            motion2 = createMotion(0, getDestination().getWidth(), speed);
            motion.start();
            motion2.start();
            return;
        }
        transitionType = TYPE_SLIDE;
    }
    if (transitionType == TYPE_PULSATE_DIALOG) {
        if (getDestination() instanceof Dialog) {
            motion = createMotion(600, 1100, 150);
            motion.start();
            motion2 = createMotion(100, 255, 225);
            motion2.start();
            pulseState = 0;
            Component c = getDialogParent(getDestination());
            originalX = c.getX();
            originalY = c.getY();
            originalWidth = c.getWidth();
            originalHeight = c.getHeight();
            Display d = Display.getInstance();
            Dialog dlg = (Dialog) destination;
            // transparent image!
            buffer = Image.createImage(Math.min(d.getDisplayWidth(), getDialogParent(dlg).getWidth()), Math.min(d.getDisplayHeight(), dlg.getContentPane().getParent().getHeight() + getDialogTitleHeight(dlg)), 0);
            Graphics g = buffer.getGraphics();
            Style stl = dlg.getDialogComponent().getStyle();
            byte bgt = stl.getBgTransparency();
            stl.setBgTransparency(0xff);
            drawDialogCmp(buffer.getGraphics(), dlg);
            stl.setBgTransparency(bgt & 0xff, true);
            return;
        }
        transitionType = TYPE_EMPTY;
        motion = createMotion(0, 0, 0);
        pulseState = (byte) 3;
        return;
    }
    if (Display.getInstance().areMutableImagesFast() || transitionType == TYPE_TIMELINE) {
        if (buffer == null) {
            buffer = createMutableImage(w, h);
        } else {
            // this might happen when screen orientation changes
            if (buffer.getWidth() != w || buffer.getHeight() != h) {
                buffer = createMutableImage(w, h);
                rgbBuffer = null;
                // slide motion might need resetting since screen size is different
                motion = null;
            }
        }
    }
    if (transitionType == TYPE_FADE) {
        motion = createMotion(0, 256, speed);
        motion.start();
        if (Display.getInstance().areMutableImagesFast()) {
            Graphics g = buffer.getGraphics();
            g.translate(-source.getAbsoluteX(), -source.getAbsoluteY());
            if (getSource().getParent() != null) {
                getSource().getComponentForm().paintComponent(g);
            }
            getSource().paintBackgrounds(g);
            g.setClip(0, 0, buffer.getWidth() + source.getAbsoluteX(), buffer.getHeight() + source.getAbsoluteY());
            paint(g, getDestination(), 0, 0);
            rgbBuffer = new RGBImage(buffer.getRGBCached(), buffer.getWidth(), buffer.getHeight());
            paint(g, getSource(), 0, 0, true);
            g.translate(source.getAbsoluteX(), source.getAbsoluteY());
        }
        return;
    }
    if (transitionType == TYPE_TIMELINE) {
        Graphics g = buffer.getGraphics();
        g.translate(-source.getAbsoluteX(), -source.getAbsoluteY());
        g.setClip(0, 0, buffer.getWidth() + source.getAbsoluteX(), buffer.getHeight() + source.getAbsoluteY());
        if (timeline.getWidth() != buffer.getWidth() || timeline.getHeight() != buffer.getHeight()) {
            timeline = timeline.scaled(buffer.getWidth(), buffer.getHeight());
        }
        if (timeline instanceof Timeline) {
            ((Timeline) timeline).setTime(0);
            ((Timeline) timeline).setLoop(false);
            ((Timeline) timeline).setAnimationDelay(0);
        }
        paint(g, getDestination(), 0, 0);
        g.translate(source.getAbsoluteX(), source.getAbsoluteY());
        return;
    }
    if (transitionType == TYPE_SLIDE || transitionType == TYPE_FAST_SLIDE || transitionType == TYPE_COVER || transitionType == TYPE_UNCOVER) {
        int dest;
        int startOffset = 0;
        boolean direction = forward;
        // flip the direction only for horizontal slides
        if ((source.getUIManager().getLookAndFeel().isRTL()) && slideType == SLIDE_HORIZONTAL) {
            direction = !direction;
        }
        if (slideType == SLIDE_HORIZONTAL) {
            dest = w;
            if (destination instanceof Dialog) {
                startOffset = w - getDialogParent(destination).getWidth();
                if (direction) {
                    startOffset -= getDialogParent(destination).getStyle().getMarginLeft(destination.isRTL());
                } else {
                    startOffset -= getDialogParent(destination).getStyle().getMarginRight(destination.isRTL());
                }
            } else {
                if (source instanceof Dialog) {
                    dest = getDialogParent(source).getWidth();
                    if (direction) {
                        dest += getDialogParent(source).getStyle().getMarginLeft(source.isRTL());
                    } else {
                        dest += getDialogParent(source).getStyle().getMarginRight(source.isRTL());
                    }
                }
            }
        } else {
            dest = h;
            if (destination instanceof Dialog) {
                startOffset = h - getDialogParent(destination).getHeight() - getDialogTitleHeight((Dialog) destination);
                if (direction) {
                    startOffset -= getDialogParent(destination).getStyle().getMarginBottom();
                } else {
                    startOffset -= getDialogParent(destination).getStyle().getMarginTop();
                    startOffset -= ((Dialog) destination).getTitleStyle().getMarginTop();
                    if (!drawDialogMenu && ((Dialog) destination).getCommandCount() > 0) {
                        Container p = ((Dialog) destination).getSoftButton(0).getParent();
                        if (p != null) {
                            startOffset -= p.getHeight();
                        }
                    }
                }
            } else {
                if (source instanceof Dialog) {
                    dest = getDialogParent(source).getHeight() + getDialogTitleHeight((Dialog) source);
                    if (direction) {
                        dest += getDialogParent(source).getStyle().getMarginBottom();
                    } else {
                        dest += getDialogParent(source).getStyle().getMarginTop();
                        dest += ((Dialog) source).getTitleStyle().getMarginTop();
                        if (((Dialog) source).getCommandCount() > 0) {
                            Container p = ((Dialog) source).getSoftButton(0).getParent();
                            if (p != null) {
                                dest += p.getHeight();
                            }
                        }
                    }
                }
            }
        }
        motion = createMotion(startOffset, dest, speed);
        if (!Display.getInstance().areMutableImagesFast()) {
            motion.start();
            buffer = null;
            return;
        }
        // make sure the destination is painted fully at least once
        // we must use a full buffer otherwise the clipping will take effect
        Graphics g = buffer.getGraphics();
        // tinting is expensive
        if (getSource() instanceof Dialog) {
            paint(g, getDestination(), 0, 0);
            if (transitionType == TYPE_FAST_SLIDE && !(destination instanceof Dialog)) {
                Dialog d = (Dialog) source;
                secondaryBuffer = createMutableImage(getDialogParent(d).getWidth(), getDialogParent(d).getHeight() + getDialogTitleHeight(d));
                drawDialogCmp(secondaryBuffer.getGraphics(), d);
            }
        } else {
            if (getDestination() instanceof Dialog) {
                paint(g, getSource(), 0, 0);
                if (transitionType == TYPE_FAST_SLIDE && !(source instanceof Dialog)) {
                    Dialog d = (Dialog) destination;
                    secondaryBuffer = createMutableImage(getDialogParent(d).getWidth(), d.getContentPane().getParent().getHeight() + getDialogTitleHeight(d));
                    drawDialogCmp(secondaryBuffer.getGraphics(), d);
                }
            } else {
                paint(g, source, -source.getAbsoluteX(), -source.getAbsoluteY(), true);
                if (transitionType == TYPE_FAST_SLIDE) {
                    secondaryBuffer = createMutableImage(destination.getWidth(), destination.getHeight());
                    paint(secondaryBuffer.getGraphics(), destination, -destination.getAbsoluteX(), -destination.getAbsoluteY());
                }
            }
        }
        motion.start();
    }
}
Also used : Graphics(com.codename1.ui.Graphics) Container(com.codename1.ui.Container) Form(com.codename1.ui.Form) Dialog(com.codename1.ui.Dialog) Style(com.codename1.ui.plaf.Style) Component(com.codename1.ui.Component) RGBImage(com.codename1.ui.RGBImage) Display(com.codename1.ui.Display)

Example 3 with RGBImage

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

the class Effects method reflectionImage.

/**
 * Takes the given image and appends an effect of reflection below it that
 * is similar to the way elements appear in water beneath them. This method
 * shouldn't be used when numAlpha is very low.
 *
 * @param source image to add the reflection effect to
 * @param mirrorRatio generally less than 1, a mirror ration of 0.5f will create a mirror image half the
 * height of the image, 0.75f will create a 3 quarter height mirror etc.
 * @param alphaRatio starting point for the alpha value in the mirror, this should be a number between 0 - 255
 * (recommended larger than 0) indicating the opacity of the closest pixel. For a mirror thats completely
 * opaque use 255. A recommended value would be between 128 to 90.
 * @param spacing the distance in pixels between the image and its reflection
 * @return new image with a reflection effect for the source image
 */
public static Image reflectionImage(Image source, float mirrorRatio, int alphaRatio, int spacing) {
    int w = source.getWidth();
    int h = source.getHeight();
    int mirrorHeight = ((int) (h * mirrorRatio)) * w;
    // create an array big enough to hold the mirror data
    RGBImage rgbImg = new RGBImage(new int[w * (h + spacing) + mirrorHeight], w, h + ((int) (h * mirrorRatio) + spacing));
    source.toRGB(rgbImg, 0, 0, 0, 0, w, h);
    int[] imageData = rgbImg.getRGB();
    int ilen = imageData.length;
    for (int iter = 0; iter < mirrorHeight; iter++) {
        int sourcePos = w * h - iter - 1;
        int off = iter % w;
        off = w - off + iter - off;
        int mirrorPos = ilen - (mirrorHeight - off) + (spacing * w);
        int color = imageData[sourcePos];
        // if the color is not transparent
        if ((color & 0xff000000) != 0 && mirrorPos < ilen) {
            int alpha = (int) (alphaRatio * ((float) mirrorHeight - iter) / ((float) mirrorHeight));
            imageData[mirrorPos] = (imageData[sourcePos] & 0xffffff) | ((alpha << 24) & 0xff000000);
        }
    }
    return rgbImg;
}
Also used : RGBImage(com.codename1.ui.RGBImage)

Example 4 with RGBImage

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

the class EncodedImage method createFromRGB.

/**
 * Tries to create an encoded image from RGB which is more efficient,
 * however if this fails it falls back to regular RGB image. This method
 * is slower than creating an RGB image (not to be confused with the RGBImage class which is
 * something ENTIRELY different!).
 *
 * @param argb an argb array
 * @param width the width for the image
 * @param height the height for the image
 * @param jpeg uses jpeg format internally which is opaque and could be faster/smaller
 * @return an image which we hope is an encoded image
 */
public static Image createFromRGB(int[] argb, int width, int height, boolean jpeg) {
    Image i = Image.createImage(argb, width, height);
    ImageIO io = ImageIO.getImageIO();
    if (io != null) {
        String format;
        if (jpeg) {
            if (!io.isFormatSupported(ImageIO.FORMAT_JPEG)) {
                return i;
            }
            format = ImageIO.FORMAT_JPEG;
        } else {
            if (!io.isFormatSupported(ImageIO.FORMAT_PNG)) {
                return i;
            }
            format = ImageIO.FORMAT_PNG;
        }
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            io.save(i, bo, format, 0.9f);
            EncodedImage enc = EncodedImage.create(bo.toByteArray());
            Util.cleanup(bo);
            enc.width = width;
            enc.height = height;
            if (jpeg) {
                enc.opaque = true;
                enc.opaqueChecked = true;
            }
            enc.cache = Display.getInstance().createSoftWeakRef(i);
            return enc;
        } catch (IOException err) {
            Log.e(err);
        }
    }
    return i;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageIO(com.codename1.ui.util.ImageIO)

Aggregations

RGBImage (com.codename1.ui.RGBImage)3 Graphics (com.codename1.ui.Graphics)2 Component (com.codename1.ui.Component)1 Container (com.codename1.ui.Container)1 Dialog (com.codename1.ui.Dialog)1 Display (com.codename1.ui.Display)1 Form (com.codename1.ui.Form)1 Image (com.codename1.ui.Image)1 Painter (com.codename1.ui.Painter)1 Rectangle (com.codename1.ui.geom.Rectangle)1 Style (com.codename1.ui.plaf.Style)1 ImageIO (com.codename1.ui.util.ImageIO)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1