Search in sources :

Example 1 with Stroke

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

the class BorderEditor method updateBorder.

private void updateBorder(boolean updateEnabled) {
    if (updateEnabled) {
        okButton.setEnabled(true);
        for (JComponent c : comboSelectionEnabled[0]) {
            c.setEnabled(false);
        }
        if (borderType.getSelectedIndex() > 1) {
            List<JComponent> colorElements = colorComponents;
            if (borderType.getSelectedIndex() == 6 && !imageMode.isSelected()) {
                colorElements = new ArrayList<JComponent>();
            } else {
                if (borderType.getSelectedIndex() < 6 && !themeColors.isSelected()) {
                    colorElements = new ArrayList<JComponent>();
                } else {
                    if (borderType.getSelectedIndex() > 6) {
                        colorElements = new ArrayList<JComponent>();
                    }
                }
            }
            for (JComponent c : comboSelectionEnabled[borderType.getSelectedIndex()]) {
                // if colors arrive from the theme then don't enable any color related element
                c.setEnabled(!colorElements.contains(c));
            }
        }
    }
    if (borderType.getSelectedIndex() == borderType.getItemCount() - 2) {
        // we need to use a special case because a theme with no images will have a different offset for the border
        currentBorder = RoundBorder.create().color(getColor(backgroundColor)).opacity(((Number) opacity.getValue()).intValue()).rectangle(isRectangle.isSelected()).shadowBlur(((Number) shadowBlur.getValue()).floatValue()).shadowOpacity(((Number) shadowOpacity.getValue()).intValue()).shadowSpread(((Number) shadowSpread.getValue()).intValue()).shadowX(((Number) shadowX.getValue()).floatValue()).shadowY(((Number) shadowY.getValue()).floatValue()).stroke(((Number) strokeThickness.getValue()).floatValue(), strokeMillimeter.isSelected()).strokeColor(getColor(strokeColor)).strokeOpacity(((Number) strokeOpacity.getValue()).intValue());
    } else {
        if (borderType.getSelectedIndex() == borderType.getItemCount() - 1) {
            // we need to use a special case because a theme with no images will have a different offset for the border
            currentBorder = RoundRectBorder.create().shadowBlur(((Number) rrShadowBlur.getValue()).floatValue()).shadowOpacity(((Number) rrShadowOpacity.getValue()).intValue()).shadowSpread(((Number) rrShadowSpread.getValue()).floatValue()).shadowX(((Number) rrShadowX.getValue()).floatValue()).shadowY(((Number) rrShadowY.getValue()).floatValue()).stroke(((Number) rrStrokeThickness.getValue()).floatValue(), rrStrokeMillimeter.isSelected()).strokeColor(getColor(rrStrokeColor)).strokeOpacity(((Number) rrStrokeOpacity.getValue()).intValue()).bezierCorners(rrBezier.isSelected()).cornerRadius(((Number) rrRadius.getValue()).floatValue()).bottomOnlyMode(rrMode.getSelectedIndex() == 1).topOnlyMode(rrMode.getSelectedIndex() == 2);
        } else {
            switch(borderType.getSelectedIndex()) {
                case 0:
                    // null border
                    currentBorder = null;
                    break;
                case 1:
                    // empty border
                    currentBorder = Border.getEmpty();
                    break;
                case 2:
                    // bevel border
                    if (themeColors.isSelected()) {
                        if (raisedBorder.isSelected()) {
                            currentBorder = Border.createBevelRaised();
                        } else {
                            currentBorder = Border.createBevelLowered();
                        }
                    } else {
                        if (raisedBorder.isSelected()) {
                            currentBorder = Border.createBevelRaised(getColor(highlightColor), getColor(secondaryHighlightColor), getColor(shadowColor), getColor(secondaryShadowColor));
                        } else {
                            currentBorder = Border.createBevelLowered(getColor(highlightColor), getColor(secondaryHighlightColor), getColor(shadowColor), getColor(secondaryShadowColor));
                        }
                    }
                    break;
                case 3:
                    // etched border
                    if (themeColors.isSelected()) {
                        if (raisedBorder.isSelected()) {
                            currentBorder = Border.createEtchedRaised();
                        } else {
                            currentBorder = Border.createEtchedLowered();
                        }
                    } else {
                        if (raisedBorder.isSelected()) {
                            currentBorder = Border.createEtchedRaised(getColor(highlightColor), getColor(shadowColor));
                        } else {
                            currentBorder = Border.createEtchedLowered(getColor(highlightColor), getColor(shadowColor));
                        }
                    }
                    break;
                case 7:
                    {
                        // this is a theme with no images
                        if (borderType.getItemCount() < 8) {
                            break;
                        }
                        // image border
                        Image c = getButtonImageBorderIcon(this.center);
                        if (imageMode.isSelected()) {
                            currentBorder = Border.createImageBorder(getButtonImageBorderIconNotNull(top), getButtonImageBorderIconNotNull(topLeft), c);
                        } else {
                            currentBorder = Border.createImageBorder(getButtonImageBorderIconNotNull(top), getButtonImageBorderIconNotNull(bottom), getButtonImageBorderIconNotNull(left), getButtonImageBorderIconNotNull(right), getButtonImageBorderIconNotNull(topLeft), getButtonImageBorderIconNotNull(topRight), getButtonImageBorderIconNotNull(bottomLeft), getButtonImageBorderIconNotNull(bottomRight), c);
                        }
                        break;
                    }
                case 8:
                    {
                        Image c = getButtonImageBorderIcon(this.center);
                        currentBorder = Border.createHorizonalImageBorder(getButtonImageBorderIconNotNull(left), getButtonImageBorderIconNotNull(right), c);
                        break;
                    }
                case 9:
                    {
                        Image c = getButtonImageBorderIcon(this.center);
                        currentBorder = Border.createVerticalImageBorder(getButtonImageBorderIconNotNull(top), getButtonImageBorderIconNotNull(bottom), c);
                        break;
                    }
                case 4:
                    // line border
                    if (thicknessMillimeters.isSelected()) {
                        if (themeColors.isSelected()) {
                            currentBorder = Border.createLineBorder(((Number) thickness.getValue()).floatValue());
                        } else {
                            currentBorder = Border.createLineBorder(((Number) thickness.getValue()).floatValue(), getColor(lineColor));
                        }
                    } else {
                        if (themeColors.isSelected()) {
                            currentBorder = Border.createLineBorder(((Number) thickness.getValue()).intValue());
                        } else {
                            currentBorder = Border.createLineBorder(((Number) thickness.getValue()).intValue(), getColor(lineColor));
                        }
                    }
                    break;
                case 5:
                    // underline border
                    if (thicknessMillimeters.isSelected()) {
                        if (themeColors.isSelected()) {
                            currentBorder = Border.createUndelineBorder(((Number) thickness.getValue()).floatValue());
                        } else {
                            currentBorder = Border.createUnderlineBorder(((Number) thickness.getValue()).floatValue(), getColor(lineColor));
                        }
                    } else {
                        if (themeColors.isSelected()) {
                            currentBorder = Border.createUndelineBorder(((Number) thickness.getValue()).intValue());
                        } else {
                            currentBorder = Border.createUnderlineBorder(((Number) thickness.getValue()).intValue(), getColor(lineColor));
                        }
                    }
                    break;
                case 6:
                    // rounded border
                    if (themeColors.isSelected()) {
                        currentBorder = Border.createRoundBorder(((Number) arcWidth.getValue()).intValue(), ((Number) arcHeight.getValue()).intValue());
                    } else {
                        currentBorder = Border.createRoundBorder(((Number) arcWidth.getValue()).intValue(), ((Number) arcHeight.getValue()).intValue(), getColor(lineColor));
                    }
                    break;
            }
        }
    }
    final CodenameOneComponentWrapper w = (CodenameOneComponentWrapper) imageBorderPreview;
    final Border finalBorder = currentBorder;
    final Button b = (Button) w.getCodenameOneComponent();
    Display.getInstance().callSerially(new Runnable() {

        @Override
        public void run() {
            b.clearClientProperties();
            b.setPreferredSize(new com.codename1.ui.geom.Dimension(200, 100));
            b.getAllStyles().setPadding(20, 20, 20, 20);
            b.getAllStyles().setBorder(finalBorder);
            b.getParent().revalidate();
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    w.revalidate();
                }
            });
        }
    });
}
Also used : CodenameOneComponentWrapper(com.codename1.ui.resource.util.CodenameOneComponentWrapper) JComponent(javax.swing.JComponent) Image(com.codename1.ui.Image) Button(com.codename1.ui.Button) JButton(javax.swing.JButton) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) RoundBorder(com.codename1.ui.plaf.RoundBorder) Border(com.codename1.ui.plaf.Border)

Example 2 with Stroke

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

the class AndroidGraphics method setStroke.

/**
 * Sets the stroke of the current Paint object.
 * @param stroke The stroke to set.
 * @return The old stroke.
 */
private Stroke setStroke(Stroke stroke) {
    Stroke old = new Stroke(paint.getStrokeWidth(), convertStrokeCap(paint.getStrokeCap()), convertStrokeJoin(paint.getStrokeJoin()), paint.getStrokeMiter());
    paint.setStrokeCap(convertStrokeCap(stroke.getCapStyle()));
    paint.setStrokeJoin(convertStrokeJoin(stroke.getJoinStyle()));
    paint.setStrokeMiter(stroke.getMiterLimit());
    paint.setStrokeWidth(stroke.getLineWidth());
    return old;
}
Also used : Stroke(com.codename1.ui.Stroke)

Example 3 with Stroke

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

the class AndroidGraphics method drawPath.

public void drawPath(Path p, Stroke stroke) {
    paint.setStyle(Paint.Style.STROKE);
    Stroke old = setStroke(stroke);
    // System.out.println("Transform semaphore "+transformSemaphore);
    if (getTransform().isIdentity()) {
        canvas.drawPath(p, paint);
    } else {
        RectF bounds = new RectF();
        p.computeBounds(bounds, false);
        Path p2 = new Path();
        p.transform(getTransformMatrix(), p2);
        RectF bounds2 = new RectF();
        p2.computeBounds(bounds2, false);
        float b2w = bounds2.width();
        float bw = bounds.width();
        float bw2 = Math.max(1, b2w) / Math.max(1, bw);
        float bh2 = Math.max(1, bounds2.height()) / Math.max(1, bounds.height());
        float ratio = Math.max(bw2, bh2);
        if (ratio > 2 && !isMutableImageGraphics) {
            // If the canvas is hardware accelerated, then it will rasterize the path
            // first, then apply the transform which leads to blurry paths if the transform does
            // significant scaling.
            // In such cases, we
            float strokeWidthUpperBound = ratio * stroke.getLineWidth();
            int ww = Math.max(1, (int) (bounds2.width() + 2 * strokeWidthUpperBound));
            int hh = Math.max(1, (int) (bounds2.height() + 2 * strokeWidthUpperBound));
            Bitmap nativeBuffer = Bitmap.createBitmap(ww, hh, Bitmap.Config.ARGB_8888);
            // int restorePoint = canvas.saveLayer(bounds2, paint, Canvas.ALL_SAVE_FLAG);
            Canvas c = new Canvas(nativeBuffer);
            Matrix translateM = new Matrix();
            translateM.set(getTransformMatrix());
            translateM.postTranslate(-bounds2.left + strokeWidthUpperBound, -bounds2.top + strokeWidthUpperBound);
            c.concat(translateM);
            c.drawPath(p, paint);
            canvas.drawBitmap(nativeBuffer, bounds2.left - strokeWidthUpperBound, bounds2.top - strokeWidthUpperBound, paint);
        } else {
            canvas.save();
            applyTransform();
            canvas.drawPath(p, paint);
            unapplyTransform();
            canvas.restore();
        }
    }
    setStroke(old);
}
Also used : RectF(android.graphics.RectF) Path(android.graphics.Path) Stroke(com.codename1.ui.Stroke) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint)

Example 4 with Stroke

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

the class IOSImplementation method createAlphaMask.

// -------------------------------------------------------------------------
// METHODS FOR DRAWING SHAPES AND TRANSFORMATIONS
// -------------------------------------------------------------------------
/**
 * Creates a platform-specific alpha mask for a shape.  This is used to cache
 * masks in the {@link com.codename1.ui.GeneralPath} class.  On iOS the alpha
 * mask is an OpenGL texture ID (not a raster of alpha pixels), but other platforms
 * may use different representations if they like.
 *
 * <p>The {@link com.codename1.ui.Graphics#drawAlphaMask} method
 * is used to draw a mask on the graphics context and this will ultimately call {@link #drawAlphaMask}
 * which can be platform specific also.
 * </p>
 * @param shape The shape that will have an alpha mask created.
 * @param stroke The stroke settings for stroking the outline of the mask.  Leave null to produce a fill
 * mask.
 * @return The platform specific alpha mask object or null if it is not supported or failed.
 * @see #deleteAlphaMask
 * @see #drawAlphaMask
 * @see #isAlphaMaskSupported
 * @see com.codename1.ui.Graphics#drawAlphaMask
 * @see com.codename1.ui.GeneralPath#getAlphaMask
 */
public TextureAlphaMask createAlphaMask(Shape shape, Stroke stroke) {
    int[] bounds = new int[] { 0, 0, 0, 0 };
    long tex = nativeCreateAlphaMaskForShape(shape, stroke, bounds);
    Rectangle shapeBounds = shape.getBounds();
    int[] padding = new int[] { // top
    shapeBounds.getY() - bounds[1], // right
    bounds[2] - (shapeBounds.getX() + shapeBounds.getWidth()), // bottom
    bounds[3] - (shapeBounds.getY() + shapeBounds.getHeight()), // left
    shapeBounds.getX() - bounds[0] };
    if (tex == 0) {
        return null;
    }
    return new TextureAlphaMask(tex, new Rectangle(bounds[0], bounds[1], bounds[2] - bounds[0], bounds[3] - bounds[1]), padding);
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 5 with Stroke

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

the class IOSImplementation method renderShape.

private NativePathRenderer renderShape(Shape shape, Stroke stroke) {
    if (stroke != null) {
        float lineWidth = stroke.getLineWidth();
        int capStyle = stroke.getCapStyle();
        int miterStyle = stroke.getJoinStyle();
        float miterLimit = stroke.getMiterLimit();
        PathIterator path = shape.getPathIterator();
        Rectangle rb = shape.getBounds();
        // Notice that these will be cleaned up in the dealloc method of the DrawPath objective-c class
        int padding = (int) Math.ceil(lineWidth);
        int padding2 = padding * 2;
        NativePathRenderer renderer = new NativePathRenderer(rb.getX() - padding, rb.getY() - padding, rb.getWidth() + padding2, rb.getHeight() + padding2, path.getWindingRule());
        NativePathStroker stroker = new NativePathStroker(renderer, lineWidth, capStyle, miterStyle, miterLimit);
        NativePathConsumer c = stroker.consumer;
        fillPathConsumer(path, c);
        // We don't need the stroker anymore because it has passed the strokes to the renderer.
        stroker.destroy();
        return renderer;
    } else {
        Rectangle rb = shape.getBounds();
        PathIterator path = shape.getPathIterator();
        // Notice that this will be cleaned up in the dealloc method of the DrawPath objective-c class.
        NativePathRenderer renderer = new NativePathRenderer(rb.getX(), rb.getY(), rb.getWidth(), rb.getHeight(), path.getWindingRule());
        NativePathConsumer c = renderer.consumer;
        fillPathConsumer(path, c);
        return renderer;
    }
}
Also used : PathIterator(com.codename1.ui.geom.PathIterator) Rectangle(com.codename1.ui.geom.Rectangle)

Aggregations

Stroke (com.codename1.ui.Stroke)4 GeneralPath (com.codename1.ui.geom.GeneralPath)3 Paint (com.codename1.charts.compat.Paint)2 Image (com.codename1.ui.Image)2 Rectangle (com.codename1.ui.geom.Rectangle)2 CodenameOneComponentWrapper (com.codename1.ui.resource.util.CodenameOneComponentWrapper)2 JButton (javax.swing.JButton)2 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1 Matrix (android.graphics.Matrix)1 Paint (android.graphics.Paint)1 Path (android.graphics.Path)1 RectF (android.graphics.RectF)1 Style (com.codename1.charts.compat.Paint.Style)1 Point (com.codename1.charts.models.Point)1 BasicStroke (com.codename1.charts.renderers.BasicStroke)1 Button (com.codename1.ui.Button)1 PathIterator (com.codename1.ui.geom.PathIterator)1 Border (com.codename1.ui.plaf.Border)1 RoundBorder (com.codename1.ui.plaf.RoundBorder)1