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

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

Introduction

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

Prototype

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

Source Link

Document

Fills a rectangle.

Usage

From source file:com.badlogic.gdx.backends.gwt.GwtApplicationCustom.java

License:Apache License

public PreloaderCallback getPreloaderCallback() {
    final Canvas canvas = Canvas.createIfSupported();
    canvas.setWidth("100%");
    canvas.setHeight("100%");
    getRootPanel().add(canvas);/*  w  w w  .j a v a2  s  .c om*/
    final Context2d context = canvas.getContext2d();
    context.setTextAlign(TextAlign.CENTER);
    context.setTextBaseline(TextBaseline.MIDDLE);
    context.setFont("18pt Calibri");

    return new PreloaderCallback() {
        @Override
        public void done() {
            context.fillRect(0, 0, 300, 40);
        }

        @Override
        public void loaded(String file, int loaded, int total) {
            System.out.println("loaded " + file + "," + loaded + "/" + total);
            //            String color = Pixmap.make(30, 30, 30, 1);
            //            context.setFillStyle(color);
            //            context.setStrokeStyle(color);
            context.fillRect(0, 0, 300, 70);
            //            color = Pixmap.make(200, 200, 200, (((TimeUtils.nanoTime() - loadStart) % 1000000000) / 1000000000f));
            //            context.setFillStyle(color);
            //            context.setStrokeStyle(color);
            context.fillRect(0, 0, 300 * (loaded / (float) total) * 0.97f, 70);

            //            context.setFillStyle(Pixmap.make(50, 50, 50, 1));
            context.fillText("loading", 300 / 2, 70 / 2);
        }

        @Override
        public void error(String file) {
            System.out.println("error: " + file);
        }
    };
}

From source file:com.java33.vizpres.client.view.visualization.CanvasViz.java

License:Open Source License

/**
 * Renders the percent data values using the Canvas 2D API as horizontal
 * bars with a text label.//w  ww.ja va2  s . c o m
 *
 * @param values Is the list of values to render.
 */
@Override
public void setData(final List<PercentValue> values) {
    if (canvas != null) {
        final Context2d ctx = canvas.getContext2d();
        ctx.clearRect(0, 0, width, height);
        ctx.setTextBaseline(Context2d.TextBaseline.TOP);
        if (values != null) {
            final int n = values.size();
            for (int i = 0; i < n; i += 1) {
                final int rowY = i * ROW_HEIGHT;
                final int percent = values.get(i).percentage;
                final double barWidth = (double) (percent * width) / 100.0;
                ctx.setFillStyle("#ff6600");
                ctx.fillRect(0, rowY, barWidth, BAR_HEIGHT);
                ctx.setFillStyle("#ffffff");
                ctx.fillText(Integer.toString(percent), 4, rowY + TEXT_HEIGHT / 2);
            }
        }
    }
}

From source file:com.lambourg.webgallery.client.widgets.TitleBarIcon.java

License:GNU General Public License

private void draw() {
    Context2d ctx;
    Canvas buffer = Canvas.createIfSupported();
    Image img;//from w  ww  . j a  va2s .  c  om
    int size = Style.toPixelRatio(Style.titleIconSize);

    this.canvas.setCoordinateSpaceWidth(size);
    this.canvas.setCoordinateSpaceHeight(size + Style.toPixelRatio(1));
    buffer.setCoordinateSpaceWidth(size);
    buffer.setCoordinateSpaceHeight(size);

    if (this.active) {
        img = this.imgActive;
    } else {
        img = this.imgNormal;
    }

    //  We need to draw the icon itself in a separate buffer, and only
    //  after apply the shadow.
    //  The reason is that in order to draw the icon using the color we
    //  want, we need to use composite operations, and this is not
    //  compatible with the shadow effect we want.
    ctx = buffer.getContext2d();
    if (this.over) {
        ctx.setFillStyle("#fff");
    } else {
        ctx.setFillStyle("#ddd");
    }
    ctx.fillRect(0, 0, size, size);
    ctx.setGlobalCompositeOperation(Composite.DESTINATION_ATOP);
    ctx.drawImage(ImageElement.as(img.getElement()), 0, 0, size, size);
    ctx.restore();

    ctx = this.canvas.getContext2d();
    ctx.setShadowBlur(0.0);
    ctx.setShadowColor("#333");
    ctx.setShadowOffsetY(Style.toPixelRatio(1));

    ctx.drawImage(buffer.getCanvasElement(), 0, 0, size, size);
}

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   w  ww.  jav a 2  s.  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//ww w  .j av a 2s  . c  o  m
 */
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);//from   www .  j  a v  a  2 s .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:examples.geometry.containment.PolygonRectangleContainment.java

License:Open Source License

@Override
protected AbstractControllableShape createControllableShape2(final Canvas canvas) {
    return new AbstractControllableShape(canvas) {
        private final double WIDTH = 50;
        private final double HEIGHT = 75;

        @Override//from w ww.j  a v  a 2s  .c  om
        public void createControlPoints() {
            addControlPoint(new Point(110, 70));
        }

        @Override
        public Rectangle createGeometry() {
            Point[] points = getControlPoints();
            return new Rectangle(points[0].x - WIDTH / 2, points[0].y - HEIGHT / 2, WIDTH, HEIGHT);
        }

        @Override
        public void drawShape() {
            Rectangle rect = createGeometry();
            Context2d context2d = canvas.getContext2d();
            context2d.rect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
            context2d.stroke();
        }

        @Override
        public void fillShape(CssColor color) {
            Rectangle rect = createGeometry();
            Context2d context2d = canvas.getContext2d();
            FillStrokeStyle style = context2d.getFillStyle();
            context2d.setFillStyle(color.value());

            context2d.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
            context2d.setFillStyle(style);
        }
    };
}

From source file:examples.geometry.demos.RegionClippingExample.java

License:Open Source License

@Override
protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
    return new ControllableShape[] { new ControllableShape(canvas, eventBus) {
        {//  w  w w.  j a va  2  s. c o m
            addControlPoints(new Point(100, 100), new Point(200, 200));
            addControlPoints(new Point(150, 150), new Point(250, 250));
        }

        @Override
        public Region getShape() {
            Point[] cp = getPoints();
            Region region = new Region(new Rectangle(cp[0], cp[1]), new Rectangle(cp[2], cp[3]));
            return region;
        }

        @Override
        public void onDraw(Canvas canvas) {
            Context2d context = canvas.getContext2d();
            Region region = getShape();

            context.save();
            context.beginPath();
            Rectangle rr = region.getBounds();
            context.rect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight());
            //            context.fill();
            context.clip();

            for (int y = 0; y < 800; y += 20) {
                context.fillText(
                        "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
                        20, y);
            }

            context.restore();

            context.setFillStyle("blue");
            context.setGlobalAlpha(0.5);
            context.beginPath();
            for (Rectangle r : region.getShapes()) {
                context.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
            }
            context.closePath();
            context.setFillStyle("black");
            context.setGlobalAlpha(1);
        }
    } };
}

From source file:examples.geometry.demos.RegionOutlineExample.java

License:Open Source License

@Override
protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
    return new ControllableShape[] { new ControllableShape(canvas, eventBus) {
        {/*from w  w w. j  a va 2 s . c  om*/
            addControlPoints(new Point(100, 50), new Point(300, 100));
            addControlPoints(new Point(250, 200), new Point(350, 330));
            addControlPoints(new Point(100, 200), new Point(190, 325));
            addControlPoints(new Point(150, 300), new Point(280, 380));
        }

        @Override
        public Region getShape() {
            Point[] cp = getPoints();

            Rectangle[] rectangles = new Rectangle[cp.length / 2];
            for (int i = 0; i < rectangles.length; i++) {
                rectangles[i] = new Rectangle(cp[2 * i], cp[2 * i + 1]);
            }

            return new Region(rectangles);
        }

        @Override
        public void onDraw(Canvas canvas) {
            Context2d context = canvas.getContext2d();
            Region region = getShape();

            context.setFillStyle("rgba(0, 0, 255, 0.5)");
            context.setGlobalAlpha(0.5);

            context.beginPath();
            for (Rectangle r : region.getShapes()) {
                context.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
            }
            context.closePath();

            //            gc.setAlpha(255);
            context.setFillStyle("rgba(255, 255, 255, 1)");
            context.setGlobalAlpha(1);
            // gc.setForeground(Display.getCurrent().getSystemColor(
            // SWT.COLOR_RED));
            // for (Rectangle r : region.getShapes()) {
            // gc.drawRectangle(r.toSWTRectangle());
            // }
            //            gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
            context.beginPath();
            for (Line l : region.getOutlineSegments()) {
                Point p1 = l.getP1();
                Point p2 = l.getP2();
                context.moveTo(p1.x, p1.y);
                context.lineTo(p2.x, p2.y);
                context.stroke();
            }
            context.closePath();
        }
    } };
}

From source file:gov.nist.spectrumbrowser.client.SensorDataStream.java

License:Open Source License

@Override
public void draw() {
    try {/* w ww.  j  a v a  2  s . com*/
        // TODO Auto-generated method stub
        verticalPanel.clear();
        drawMenuItems();

        HorizontalPanel spectrogramPanel = new HorizontalPanel();

        colorMap = new ColorMap((int) maxPower, (int) minPower);

        verticalPanel.add(spectrogramPanel);

        frequencyValuesCanvas = Canvas.createIfSupported();
        frequencyValuesCanvas.setWidth(100 + "px");
        frequencyValuesCanvas.setHeight(SpectrumBrowser.SPEC_HEIGHT + "px");
        frequencyValuesCanvas.setCoordinateSpaceHeight(SpectrumBrowser.SPEC_HEIGHT);
        frequencyValuesCanvas.setCoordinateSpaceWidth(100);

        spectrogramCanvas = Canvas.createIfSupported();
        spectrogramCanvas.setTitle("Click to freeze/unfreeze");
        spectrogramCanvas.setWidth(SpectrumBrowser.SPEC_WIDTH + "px");
        spectrogramCanvas.setHeight(SpectrumBrowser.SPEC_HEIGHT + "px");
        spectrogramCanvas.setCoordinateSpaceWidth(SpectrumBrowser.SPEC_WIDTH);
        spectrogramCanvas.setCoordinateSpaceHeight(SpectrumBrowser.SPEC_HEIGHT);
        spectrogramCanvas.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                isFrozen = !isFrozen;
                if (isFrozen) {
                    freezeButton.setText("Unfreeze");
                } else {
                    freezeButton.setText("Freeze");
                }
            }
        });
        spectrogramPanel.add(frequencyValuesCanvas);
        spectrogramPanel.add(spectrogramCanvas);
        spectrogramPanel.setBorderWidth(3);
        // Draw the colorbar canvas.
        HorizontalPanel colorbarFrame = new HorizontalPanel();
        colorbarFrame.setBorderWidth(3);
        Canvas colorbarCanvas = Canvas.createIfSupported();
        Canvas colorbarTextCanvas = Canvas.createIfSupported();
        colorbarCanvas.setWidth(30 + "px");
        colorbarCanvas.setCoordinateSpaceHeight(SpectrumBrowser.SPEC_HEIGHT);
        colorbarCanvas.setCoordinateSpaceWidth(30);
        colorbarTextCanvas.setWidth(30 + "px");
        colorbarCanvas.setHeight(SpectrumBrowser.SPEC_HEIGHT + "px");
        colorbarTextCanvas.setCoordinateSpaceHeight(SpectrumBrowser.SPEC_HEIGHT);
        colorbarTextCanvas.setHeight(SpectrumBrowser.SPEC_HEIGHT + "px");
        colorbarTextCanvas.setCoordinateSpaceWidth(30);
        int nStops = colorMap.getColorStopCount();
        colorbarFrame.add(colorbarCanvas);
        colorbarFrame.add(colorbarTextCanvas);
        spectrogramPanel.add(colorbarFrame);
        verticalPanel.add(spectrogramPanel);
        double rectHeight = (double) SpectrumBrowser.SPEC_HEIGHT / (double) nStops;
        int i = 0;
        for (ColorStop colorStop : colorMap.getColorStops()) {
            CssColor color = colorStop.cssColor;
            Context2d context = colorbarCanvas.getContext2d();
            context.setFillStyle(color);
            double y = SpectrumBrowser.SPEC_HEIGHT - (i + 1) * rectHeight;
            context.fillRect(0, y, 30, (int) rectHeight);
            Context2d textContext = colorbarTextCanvas.getContext2d();
            textContext.setTextAlign(TextAlign.LEFT);
            textContext.fillText(Integer.toString((int) colorStop.stopValue), 0, (int) (y + rectHeight / 2));
            i++;
        }

        occupancyPanel = new HorizontalPanel();
        VerticalPanel pad = new VerticalPanel();
        pad.setWidth("25px");
        occupancyPanel.add(pad);
        spectrumPanel = new HorizontalPanel();
        ChartLoader chartLoader = new ChartLoader(ChartPackage.CORECHART);

        chartLoader.loadApi(new Runnable() {
            @Override
            public void run() {
                chartApiLoaded = true;

            }
        });
        verticalPanel.add(occupancyPanel);
        verticalPanel.add(spectrumPanel);

        context2d = spectrogramCanvas.getContext2d();
        openWebSocket();
    } catch (Throwable th) {
        logger.log(Level.SEVERE, "ERROR drawing screen", th);
    }
}