Search in sources :

Example 56 with Graphics

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

the class RoundBorder method fillShape.

private void fillShape(Graphics g, int color, int opacity, int width, int height, boolean stroke) {
    g.setColor(color);
    g.setAlpha(opacity);
    if (!rectangle || width <= height) {
        int x = 0;
        int y = 0;
        int size = width;
        if (width != height) {
            if (width > height) {
                size = height;
                x = (width - height) / 2;
            } else {
                size = width;
                y = (height - width) / 2;
            }
        }
        if (size < 5) {
            // probably won't be visible anyway so do nothing, otherwise it might throw an exception
            return;
        }
        if (stroke && this.stroke != null) {
            int sw = (int) Math.ceil((stroke && this.stroke != null) ? this.stroke.getLineWidth() : 0);
            GeneralPath arc = new GeneralPath();
            arc.arc(x + sw / 2, y + sw / 2, size - 2 * sw, size - 2 * sw, 0, 2 * Math.PI);
            g.fillShape(arc);
            g.setColor(strokeColor);
            g.setAlpha(strokeOpacity);
            if (strokeAngle != 360) {
                arc = new GeneralPath();
                arc.arc(x + sw / 2, y + sw / 2, size - 2 * sw, size - 2 * sw, Math.PI / 2, -Math.toRadians(strokeAngle));
            }
            g.drawShape(arc, this.stroke);
        } else {
            g.fillArc(x, y, size, size, 0, 360);
        }
    } else {
        GeneralPath gp = new GeneralPath();
        float sw = (stroke && this.stroke != null) ? this.stroke.getLineWidth() : 0;
        gp.moveTo(height / 2.0, sw);
        gp.lineTo(width - (height / 2.0), sw);
        gp.arcTo(width - (height / 2.0), height / 2.0, width - (height / 2.0), height - sw, true);
        gp.lineTo(height / 2.0, height - sw);
        gp.arcTo(height / 2.0, height / 2.0, height / 2.0, sw, true);
        gp.closePath();
        g.fillShape(gp);
        if (stroke && this.stroke != null) {
            g.setAlpha(strokeOpacity);
            g.setColor(strokeColor);
            g.drawShape(gp, this.stroke);
        }
    }
}
Also used : GeneralPath(com.codename1.ui.geom.GeneralPath)

Example 57 with Graphics

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

the class RoundBorder method createTargetImage.

private Image createTargetImage(Component c, int w, int h, boolean fast) {
    Image target = Image.createImage(w, h, 0);
    int shapeX = 0;
    int shapeY = 0;
    int shapeW = w;
    int shapeH = h;
    Graphics tg = target.getGraphics();
    tg.setAntiAliased(true);
    int shadowSpreadL = shadowSpread;
    if (shadowMM) {
        shadowSpreadL = Display.getInstance().convertToPixels(shadowSpreadL);
    }
    if (shadowOpacity > 0) {
        shapeW -= shadowSpreadL;
        shapeW -= (shadowBlur / 2);
        shapeH -= shadowSpreadL;
        shapeH -= (shadowBlur / 2);
        shapeX += Math.round((shadowSpreadL + (shadowBlur / 2)) * shadowX);
        shapeY += Math.round((shadowSpreadL + (shadowBlur / 2)) * shadowY);
        // draw a gradient of sort for the shadow
        for (int iter = shadowSpreadL - 1; iter >= 0; iter--) {
            tg.translate(iter, iter);
            fillShape(tg, 0, shadowOpacity / shadowSpreadL, w - (iter * 2), h - (iter * 2), false);
            tg.translate(-iter, -iter);
        }
        if (Display.getInstance().isGaussianBlurSupported() && !fast) {
            Image blured = Display.getInstance().gaussianBlurImage(target, shadowBlur / 2);
            target = Image.createImage(w, h, 0);
            tg = target.getGraphics();
            tg.drawImage(blured, 0, 0);
            tg.setAntiAliased(true);
        }
    }
    tg.translate(shapeX, shapeY);
    if (uiid && tg.isShapeClipSupported()) {
        c.getStyle().setBorder(Border.createEmpty());
        GeneralPath gp = new GeneralPath();
        if (rectangle) {
            float sw = this.stroke != null ? this.stroke.getLineWidth() : 0;
            gp.moveTo(shapeH / 2.0, sw);
            gp.lineTo(shapeW - (shapeH / 2.0), sw);
            gp.arcTo(shapeW - (shapeH / 2.0), shapeH / 2.0, shapeW - (shapeH / 2.0), shapeH - sw, true);
            gp.lineTo(shapeH / 2.0, shapeH - sw);
            gp.arcTo(shapeH / 2.0, shapeH / 2.0, shapeH / 2.0, sw, true);
            gp.closePath();
        } else {
            int size = shapeW;
            int xPos = 0;
            int yPos = 0;
            if (shapeW != shapeH) {
                if (shapeW > shapeH) {
                    size = shapeH;
                    xPos = (shapeW - shapeH) / 2;
                } else {
                    size = shapeW;
                    yPos = (shapeH - shapeW) / 2;
                }
            }
            gp.arc(xPos, yPos, size, size, 0, 2 * Math.PI);
        }
        tg.setClip(gp);
        c.getStyle().getBgPainter().paint(tg, new Rectangle(0, 0, w, h));
        c.getStyle().setBorder(this);
    } else {
        fillShape(tg, color, opacity, shapeW, shapeH, true);
    }
    return target;
}
Also used : Graphics(com.codename1.ui.Graphics) GeneralPath(com.codename1.ui.geom.GeneralPath) Rectangle(com.codename1.ui.geom.Rectangle) Image(com.codename1.ui.Image)

Example 58 with Graphics

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

the class Effects method dropshadow.

/**
 * Generates a shadow for the source image and returns a new larger image containing the shadow
 *
 * @param source the source image for whom the shadow should be generated
 * @param blurRadius a shadow is blurred using a gaussian blur when available, a value of 10 is often satisfactory
 * @param opacity the opacity of the shadow between 0 - 1 where 1 is completely opaque
 * @param xDistance the distance on the x axis from the main image body in pixels e.g. a negative value will represent a lightsource from the right (shadow on the left)
 * @param yDistance the distance on the y axis from the main image body in pixels e.g. a negative value will represent a lightsource from the bottom (shadow on top)
 * @return a new image whose size incorporates x/yDistance
 */
public static Image dropshadow(Image source, int blurRadius, float opacity, int xDistance, int yDistance) {
    Image s = dropshadow(source, blurRadius, opacity);
    Image n = Image.createImage(source.getWidth() + Math.abs(xDistance), source.getHeight() + Math.abs(yDistance), 0);
    Graphics g = n.getGraphics();
    int shadowX = 0, imageX = 0, shadowY = 0, imageY = 0;
    if (xDistance < 0) {
        imageX = xDistance * -1;
    } else {
        shadowX = xDistance;
    }
    if (yDistance < 0) {
        imageY = yDistance * -1;
    } else {
        shadowY = yDistance;
    }
    g.drawImage(s, shadowX, shadowY);
    g.drawImage(source, imageX, imageY);
    return n;
}
Also used : Graphics(com.codename1.ui.Graphics) RGBImage(com.codename1.ui.RGBImage) Image(com.codename1.ui.Image)

Example 59 with Graphics

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

the class SwipeBackSupport method bind.

/**
 * Binds support for swiping to the given forms
 *
 * @param currentForm the current form
 * @param destination the destination form which can be created lazily
 */
protected void bind(final Form currentForm, final LazyValue<Form> destination) {
    pointerDragged = new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            if (sideSwipePotential) {
                final int x = evt.getX();
                final int y = evt.getY();
                if (Math.abs(y - initialDragY) > x - initialDragX) {
                    sideSwipePotential = false;
                    return;
                }
                evt.consume();
                if (dragActivated) {
                    currentX = x;
                    Display.getInstance().getCurrent().repaint();
                } else {
                    if (x - initialDragX > Display.getInstance().convertToPixels(currentForm.getUIManager().getThemeConstant("backGestureThresholdInt", 5), true)) {
                        dragActivated = true;
                        destinationForm = destination.get();
                        startBackTransition(currentForm, destinationForm);
                    }
                }
            }
        }
    };
    pointerReleased = new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            if (dragActivated) {
                int destNumberX = Display.getInstance().getDisplayWidth();
                int incrementsX = Display.getInstance().convertToPixels(3, true);
                if (currentX < destNumberX / 2) {
                    destinationForm = currentForm;
                    destNumberX = 0;
                    incrementsX *= -1;
                }
                final int destNumber = destNumberX;
                final int increments = incrementsX;
                Display.getInstance().getCurrent().registerAnimated(new Animation() {

                    public boolean animate() {
                        currentX += increments;
                        if (currentX > 0 && currentX >= destNumber || currentX < 0 && currentX <= destNumber) {
                            currentX = destNumber;
                            Transition t = destinationForm.getTransitionInAnimator();
                            destinationForm.setTransitionInAnimator(CommonTransitions.createEmpty());
                            destinationForm.show();
                            destinationForm.setTransitionInAnimator(t);
                            destinationForm = null;
                            dragActivated = false;
                            return false;
                        }
                        return true;
                    }

                    public void paint(Graphics g) {
                    }
                });
            }
        }
    };
    pointerPressed = new ActionListener() {

        public void actionPerformed(ActionEvent evt) {
            sideSwipePotential = false;
            int displayWidth = Display.getInstance().getDisplayWidth();
            sideSwipePotential = !transitionRunning && evt.getX() < displayWidth / currentForm.getUIManager().getThemeConstant("sideSwipeSensitiveInt", 10);
            initialDragX = evt.getX();
            initialDragY = evt.getY();
        /*if (sideSwipePotential) {
                    Component c = Display.getInstance().getCurrent().getComponentAt(initialDragX, initialDragY);
                    if (c != null && c.shouldBlockSideSwipe()) {
                        sideSwipePotential = false;
                    }
                }*/
        }
    };
    currentForm.addPointerDraggedListener(pointerDragged);
    currentForm.addPointerReleasedListener(pointerReleased);
    currentForm.addPointerPressedListener(pointerPressed);
}
Also used : Graphics(com.codename1.ui.Graphics) ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) Transition(com.codename1.ui.animations.Transition) Animation(com.codename1.ui.animations.Animation)

Example 60 with Graphics

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

the class JavaSEPort method getTransform.

@Override
public com.codename1.ui.Transform getTransform(Object graphics) {
    checkEDT();
    com.codename1.ui.Transform t = getNativeScreenGraphicsTransform(graphics);
    if (t == null) {
        return Transform.makeIdentity();
    }
    return t.copy();
}
Also used : Transform(com.codename1.ui.Transform)

Aggregations

Image (com.codename1.ui.Image)18 Component (com.codename1.ui.Component)17 Point (com.codename1.ui.geom.Point)16 Style (com.codename1.ui.plaf.Style)15 Graphics (com.codename1.ui.Graphics)14 Form (com.codename1.ui.Form)12 Font (com.codename1.ui.Font)11 GeneralPath (com.codename1.ui.geom.GeneralPath)9 Rectangle (com.codename1.ui.geom.Rectangle)9 Animation (com.codename1.ui.animations.Animation)8 Dialog (com.codename1.ui.Dialog)7 Dimension (com.codename1.ui.geom.Dimension)6 Painter (com.codename1.ui.Painter)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 FontImage (com.codename1.ui.FontImage)4 RGBImage (com.codename1.ui.RGBImage)4 Motion (com.codename1.ui.animations.Motion)4 ActionListener (com.codename1.ui.events.ActionListener)4 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 IOException (java.io.IOException)4