Search in sources :

Example 1 with Painter

use of com.codename1.ui.Painter 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 Painter

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

the class GlassTutorial method showOn.

/**
 * Install the glass tutorial on a form and seamlessly dismiss it when no longer necessary
 * @param f the form
 */
public void showOn(Form f) {
    Painter oldPane = f.getGlassPane();
    f.setGlassPane(this);
    Dialog dummy = new Dialog() {

        public void keyReleased(int i) {
            dispose();
        }
    };
    int oldTint = f.getTintColor();
    f.setTintColor(0);
    dummy.getDialogStyle().setBgTransparency(0);
    dummy.setDisposeWhenPointerOutOfBounds(true);
    dummy.show(0, Display.getInstance().getDisplayHeight() - 2, 0, Display.getInstance().getDisplayWidth() - 2, true, true);
    f.setTintColor(oldTint);
    f.setGlassPane(oldPane);
}
Also used : Dialog(com.codename1.ui.Dialog) Painter(com.codename1.ui.Painter)

Example 3 with Painter

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

the class SwipeBackSupport method startBackTransition.

void startBackTransition(final Form currentForm, Form destination) {
    final Transition t = destination.getTransitionOutAnimator().copy(true);
    if (t instanceof CommonTransitions) {
        Transition originalTransition = currentForm.getTransitionOutAnimator();
        currentForm.setTransitionOutAnimator(CommonTransitions.createEmpty());
        Form blank = new Form() {

            protected boolean shouldSendPointerReleaseToOtherForm() {
                return true;
            }
        };
        blank.addPointerDraggedListener(pointerDragged);
        blank.addPointerReleasedListener(pointerReleased);
        blank.addPointerPressedListener(pointerPressed);
        blank.setTransitionInAnimator(CommonTransitions.createEmpty());
        blank.setTransitionOutAnimator(CommonTransitions.createEmpty());
        blank.show();
        currentForm.setTransitionOutAnimator(originalTransition);
        ((CommonTransitions) t).setMotion(new LazyValue<Motion>() {

            public Motion get(Object... args) {
                return new ManualMotion(((Integer) args[0]).intValue(), ((Integer) args[1]).intValue(), ((Integer) args[2]).intValue());
            }
        });
        t.init(currentForm, destination);
        t.initTransition();
        blank.setGlassPane(new Painter() {

            public void paint(Graphics g, Rectangle rect) {
                t.animate();
                t.paint(g);
            }
        });
    }
}
Also used : Graphics(com.codename1.ui.Graphics) Motion(com.codename1.ui.animations.Motion) CommonTransitions(com.codename1.ui.animations.CommonTransitions) Form(com.codename1.ui.Form) Transition(com.codename1.ui.animations.Transition) Painter(com.codename1.ui.Painter) Rectangle(com.codename1.ui.geom.Rectangle)

Example 4 with Painter

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

the class Component method deinitializeImpl.

/**
 * Cleansup the initialization flags in the hierachy, notice that paint calls might
 * still occur after deinitilization mostly to perform transitions etc.
 * <p>However interactivity, animation and event tracking code can and probably
 * should be removed by this method.
 */
void deinitializeImpl() {
    if (isInitialized()) {
        hideNativeOverlay();
        paintLockRelease();
        setInitialized(false);
        setDirtyRegion(null);
        Style stl = getStyle();
        Image i = stl.getBgImage();
        if (i != null) {
            i.unlock();
        } else {
            Border b = stl.getBorder();
            if (b != null) {
                b.unlock();
            }
        }
        Painter p = stl.getBgPainter();
        if (p instanceof BGPainter) {
            ((BGPainter) p).radialCache = null;
        }
        deinitialize();
    }
}
Also used : Style(com.codename1.ui.plaf.Style) Border(com.codename1.ui.plaf.Border)

Example 5 with Painter

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

the class Form method showModal.

/**
 * This method shows the form as a modal alert allowing us to produce a behavior
 * of an alert/dialog box. This method will block the calling thread even if the
 * calling thread is the EDT. Notice that this method will not release the block
 * until dispose is called even if show() from another form is called!
 * <p>Modal dialogs Allow the forms "content" to "hang in mid air" this is especially useful for
 * dialogs where you would want the underlying form to "peek" from behind the
 * form.
 *
 * @param top space in pixels between the top of the screen and the form
 * @param bottom space in pixels between the bottom of the screen and the form
 * @param left space in pixels between the left of the screen and the form
 * @param right space in pixels between the right of the screen and the form
 * @param includeTitle whether the title should hang in the top of the screen or
 * be glued onto the content pane
 * @param modal indictes if this is a modal or modeless dialog true for modal dialogs
 */
void showModal(int top, int bottom, int left, int right, boolean includeTitle, boolean modal, boolean reverse) {
    Display.getInstance().flushEdt();
    if (previousForm == null) {
        previousForm = Display.getInstance().getCurrent();
        // special case for application opening with a dialog before any form is shown
        if (previousForm == null) {
            previousForm = new Form();
            previousForm.show();
        } else {
            if (previousForm instanceof Dialog) {
                Dialog previousDialog = (Dialog) previousForm;
                if (previousDialog.isDisposed()) {
                    previousForm = Display.getInstance().getCurrentUpcoming();
                }
            }
        }
    }
    previousForm.tint = true;
    Painter p = getStyle().getBgPainter();
    if (top > 0 || bottom > 0 || left > 0 || right > 0) {
        if (!title.isVisible()) {
            includeTitle = false;
        }
        Style titleStyle = title.getStyle();
        titleStyle.removeListeners();
        Style contentStyle = contentPane.getUnselectedStyle();
        contentStyle.removeListeners();
        if (includeTitle) {
            titleStyle.setMargin(Component.TOP, top, false);
            titleStyle.setMargin(Component.BOTTOM, 0, false);
            titleStyle.setMargin(Component.LEFT, left, false);
            titleStyle.setMargin(Component.RIGHT, right, false);
            contentStyle.setMargin(Component.TOP, 0, false);
            contentStyle.setMargin(Component.BOTTOM, bottom, false);
            contentStyle.setMargin(Component.LEFT, left, false);
            contentStyle.setMargin(Component.RIGHT, right, false);
        } else {
            titleStyle.setMargin(Component.TOP, 0, false);
            titleStyle.setMargin(Component.BOTTOM, 0, false);
            titleStyle.setMargin(Component.LEFT, 0, false);
            titleStyle.setMargin(Component.RIGHT, 0, false);
            contentStyle.setMargin(Component.TOP, top, false);
            contentStyle.setMargin(Component.BOTTOM, bottom, false);
            contentStyle.setMargin(Component.LEFT, left, false);
            contentStyle.setMargin(Component.RIGHT, right, false);
        }
        titleStyle.setMarginUnit(null);
        contentStyle.setMarginUnit(null);
        initDialogBgPainter(p, previousForm);
        revalidate();
    }
    initFocused();
    if (getTransitionOutAnimator() == null && getTransitionInAnimator() == null) {
        initLaf(getUIManager());
    }
    initComponentImpl();
    Display.getInstance().setCurrent(this, reverse);
    onShow();
    if (modal) {
        // called to display a dialog and wait for modality
        Display.getInstance().invokeAndBlock(new RunnableWrapper(this, p, reverse));
        // if the virtual keyboard was opend by the dialog close it
        Display.getInstance().setShowVirtualKeyboard(false);
    }
}
Also used : Style(com.codename1.ui.plaf.Style)

Aggregations

Painter (com.codename1.ui.Painter)8 Rectangle (com.codename1.ui.geom.Rectangle)4 Component (com.codename1.ui.Component)3 Dialog (com.codename1.ui.Dialog)2 Graphics (com.codename1.ui.Graphics)2 Motion (com.codename1.ui.animations.Motion)2 Point (com.codename1.ui.geom.Point)2 Border (com.codename1.ui.plaf.Border)2 Style (com.codename1.ui.plaf.Style)2 Form (com.codename1.ui.Form)1 Image (com.codename1.ui.Image)1 RGBImage (com.codename1.ui.RGBImage)1 Animation (com.codename1.ui.animations.Animation)1 CommonTransitions (com.codename1.ui.animations.CommonTransitions)1 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)1 Transition (com.codename1.ui.animations.Transition)1 UIManager (com.codename1.ui.plaf.UIManager)1 Vector (java.util.Vector)1