Example usage for com.google.gwt.canvas.dom.client Context2d strokeRect

List of usage examples for com.google.gwt.canvas.dom.client Context2d strokeRect

Introduction

In this page you can find the example usage for com.google.gwt.canvas.dom.client Context2d strokeRect.

Prototype

public final native void strokeRect(double x, double y, double w, double h) ;

Source Link

Document

Draws the outline of a rectangle with the current stroke style.

Usage

From source file:com.philbeaudoin.quebec.client.scene.Rectangle.java

License:Apache License

@Override
public void drawUntransformed(double time, Context2d context) {
    CanvasGradient gradient = context.createLinearGradient(x0, y0, x0, y1);
    gradient.addColorStop(0, color0);//from www.  j  a va 2s.c  o  m
    gradient.addColorStop(1, color1);
    context.setFillStyle(gradient);
    context.fillRect(x0, y0, w, h);
    if (strokeColor != null && strokeWidth > 0) {
        context.setStrokeStyle(strokeColor);
        context.setLineWidth(strokeWidth);
        context.strokeRect(ox0, oy0, ow, oh);
    }
}

From source file:com.sencha.gxt.chart.client.draw.engine.Canvas2d.java

License:sencha.com license

/**
 * In the Canvas2d class, this method does more or less what renderSprite does in SVG and VML - it
 * actually renders the sprite to the dom.
 * @param sprite the sprite to draw/*w w w  .  j a  v  a  2 s  .  c om*/
 */
protected void append(Sprite sprite) {
    if (sprite.isHidden() || sprite.getOpacity() == 0) {
        return;
    }
    Context2d ctx = getContext();
    ctx.save();
    //set global stuff, fill, stroke, clip, etc

    //clip - deal with translation or normal rectangle
    if (sprite.getClipRectangle() != null) {
        PreciseRectangle clip = sprite.getClipRectangle();
        if (sprite.getScaling() != null || sprite.getTranslation() != null || sprite.getRotation() != null) {
            PathSprite transPath = new PathSprite(new RectangleSprite(clip));
            transPath = transPath.map(sprite.transformMatrix());
            appendPath(ctx, transPath);
        } else {
            ctx.beginPath();
            ctx.rect(clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
            ctx.closePath();
        }
        ctx.clip();
    }

    if (sprite.getScaling() != null || sprite.getTranslation() != null || sprite.getRotation() != null
            || (component.isViewBox() && viewbox != null)) {
        Matrix matrix = sprite.transformMatrix();
        if (matrix != null) {
            //TODO consider replacing this transform call with three distinct calls to translate/scale/rotate if cheaper
            ctx.transform(matrix.get(0, 0), matrix.get(1, 0), matrix.get(0, 1), matrix.get(1, 1),
                    matrix.get(0, 2), matrix.get(1, 2));
        }
        if (component.isViewBox() && viewbox != null) {
            double size = Math.min(getWidth() / viewbox.getWidth(), getHeight() / viewbox.getHeight());

            ctx.scale(size, size);
            ctx.translate(-viewbox.getX(), -viewbox.getY());
        }
    }

    //TODO see about caching colors via the dirty flag? If we don't use a color/gradient for a pass or three, dump it
    double opacity = Double.isNaN(sprite.getOpacity()) ? 1.0 : sprite.getOpacity();
    PreciseRectangle untransformedBbox = sprite.getPathSprite().dimensions();
    if (sprite.getStroke() != null && sprite.getStroke() != Color.NONE && sprite.getStrokeWidth() != 0) {
        ctx.setLineWidth(Double.isNaN(sprite.getStrokeWidth()) ? 1.0 : sprite.getStrokeWidth());
        ctx.setStrokeStyle(getColor(sprite.getStroke(), untransformedBbox));//TODO read bbox from cache
    }
    if (sprite.getFill() != null && sprite.getFill() != Color.NONE) {
        ctx.setFillStyle(getColor(sprite.getFill(), untransformedBbox));//TODO read bbox from cache
    }

    if (sprite instanceof PathSprite) {
        appendPath(ctx, (PathSprite) sprite);
    } else if (sprite instanceof TextSprite) {
        TextSprite text = (TextSprite) sprite;
        //TODO style and weight
        ctx.setFont(text.getFontSize() + "px " + text.getFont());
        ctx.setTextAlign(getTextAlign(text.getTextAnchor()));
        ctx.setTextBaseline(getTextBaseline(text.getTextBaseline()));
        ctx.fillText(text.getText(), text.getX(), text.getY());
    } else if (sprite instanceof RectangleSprite) {
        RectangleSprite rect = (RectangleSprite) sprite;
        if (Double.isNaN(rect.getRadius()) || rect.getRadius() == 0) {
            if (sprite.getFill() != null && sprite.getFill() != Color.NONE) {
                ctx.setGlobalAlpha(
                        Double.isNaN(sprite.getFillOpacity()) ? opacity : opacity * sprite.getFillOpacity());
                ctx.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
            }
            if (sprite.getStroke() != null && sprite.getStroke() != Color.NONE
                    && sprite.getStrokeWidth() != 0) {
                ctx.setGlobalAlpha(Double.isNaN(sprite.getStrokeOpacity()) ? opacity
                        : opacity * sprite.getStrokeOpacity());
                ctx.strokeRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
            }
        } else {
            appendPath(ctx, rect.getPathSprite());
        }
    } else if (sprite instanceof CircleSprite) {
        CircleSprite circle = (CircleSprite) sprite;
        ctx.beginPath();
        ctx.arc(circle.getCenterX(), circle.getCenterY(), circle.getRadius(), 0, 2 * Math.PI);
        ctx.closePath();
        if (sprite.getFill() != null && sprite.getFill() != Color.NONE) {
            ctx.setGlobalAlpha(
                    Double.isNaN(sprite.getFillOpacity()) ? opacity : opacity * sprite.getFillOpacity());
            ctx.fill();
        }
        if (sprite.getStroke() != null && sprite.getStroke() != Color.NONE && sprite.getStrokeWidth() != 0) {
            ctx.setGlobalAlpha(
                    Double.isNaN(sprite.getStrokeOpacity()) ? opacity : opacity * sprite.getStrokeOpacity());
            ctx.stroke();
        }
    } else if (sprite instanceof EllipseSprite) {
        appendPath(ctx, sprite.getPathSprite());
    } else if (sprite instanceof ImageSprite) {
        ImageSprite image = (ImageSprite) sprite;
        ImageElement elt = Document.get().createImageElement();
        elt.setSrc(image.getResource().getSafeUri().asString());
        ctx.drawImage(elt, image.getX(), image.getY(), image.getWidth(), image.getHeight());
    }

    ctx.restore();

    if (!REDRAW_ALL) {
        renderedBbox.put(sprite, getBBox(sprite));
    }

    sprite.clearDirtyFlags();
}

From source file:edu.umb.jsPedigrees.client.Pelican.PelicanPerson.java

License:Open Source License

public void drawSymbol() {

    Context2d ctx = canvas.getContext2d();

    // clear old symbol
    ctx.clearRect(0, 0, symbolSize + 1, symbolSize + 1);

    ctx.setStrokeStyle(CssColor.make("0,0,0"));
    ctx.setLineWidth(1.0f);/* w ww.  j a v a 2s .c o m*/

    if (sex == male) {
        ctx.strokeRect(0, 0, symbolSize, symbolSize);
        if (affection == affected) {
            ctx.fillRect(0, 0, symbolSize, symbolSize);
        }
    }

    if (sex == female) {
        // g2.drawArc(0,0,symbolSize,symbolSize,0,360);
        ctx.beginPath();
        ctx.arc(symbolSize / 2, symbolSize / 2, (symbolSize / 2) - 1, 0, 360);

        if (affection == affected) {
            ctx.fill();
        } else {
            ctx.stroke();
        }
    }
}

From source file:gwtcog.examples.client.neural.neat.boxes.DisplayBoxesPanel.java

License:Apache License

public void paint() {//(Graphics g) {

    Context2d g = canvas.getContext2d();

    NEATGenome genome = (NEATGenome) this.pop.getBestGenome();
    Substrate substrate = SubstrateFactory.factorSandwichSubstrate(resolution, resolution);
    HyperNEATCODEC codec = new HyperNEATCODEC();
    NEATNetwork phenotype = (NEATNetwork) codec.decode(this.pop, substrate, genome);

    TrialEvaluation trial = new TrialEvaluation(phenotype, this.testCase);
    IntPair actualPos = trial.query(resolution);

    // clear what was there before
    //g.setColor(Color.white);
    g.setFillStyle("white");
    g.fillRect(0, 0, 400, 400);/*from   w w  w .j a  v  a  2 s .c om*/

    //
    int boxWidth = 400 / resolution;
    int boxHeight = 400 / resolution;
    double delta = 2.0 / resolution;
    int index = 0;

    for (int row = 0; row < resolution; row++) {
        double y = -1 + (row * delta);
        int boxY = row * boxHeight;
        for (int col = 0; col < resolution; col++) {
            double x = -1 + (col * delta);
            int boxX = col * boxWidth;

            if (this.testCase.getPixel(x, y) > 0) {
                //g.setColor(Color.blue);
                g.setFillStyle("blue");
                g.fillRect(boxX, boxY, boxWidth, boxHeight);
            } else {
                double d = trial.getOutput().getData(index);

                int c = trial.normalize(d, 255);
                String hex = Integer.toHexString(c);
                if (hex.length() == 1) {
                    hex = "0" + hex;
                }
                String color = "#ff" + hex + "ff";
                g.setFillStyle(color);
                g.fillRect(boxX, boxY, boxWidth, boxHeight);

                g.setStrokeStyle("black");

                g.strokeRect(boxX, boxY, boxWidth, boxHeight);
                g.strokeRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2);
            }
            index++;
        }
    }

    g.setFillStyle("red");
    g.fillRect(actualPos.getX() * boxWidth, actualPos.getY() * boxHeight, boxWidth, boxHeight);
}