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

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

Introduction

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

Prototype

public final void setFillStyle(String fillStyleColor) 

Source Link

Document

Convenience method to set the context's fillStyle to a CssColor , specified in String form.

Usage

From source file:examples.geometry.containment.PolygonPolygonContainment.java

License:Open Source License

@Override
protected AbstractControllableShape createControllableShape2(final Canvas canvas) {
    return new AbstractControllableShape(canvas) {
        @Override//w  w w .j av  a 2  s  . c  om
        public void createControlPoints() {
            addControlPoint(new Point(200, 200));
        }

        @Override
        public Polygon createGeometry() {
            Point[] points = getControlPoints();
            Polygon polygon = new Polygon(points[0].x - 30, points[0].y - 30, points[0].x + 80,
                    points[0].y - 20, points[0].x - 20, points[0].y + 40);
            return polygon;
        }

        @Override
        public void drawShape() {
            Polygon polygon = createGeometry();
            Context2d c = canvas.getContext2d();
            CanvasDrawer.strokePath(polygon.toPath(), c);
        }

        @Override
        public void fillShape(CssColor color) {
            Polygon polygon = createGeometry();

            Context2d c = canvas.getContext2d();
            FillStrokeStyle style = c.getFillStyle();

            c.setFillStyle(color.value());
            CanvasDrawer.fillPath(polygon.toPath(), c);

            c.setFillStyle(style);
        }
    };
}

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/* w w w  . j  a  va2  s.  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.CAGExample.java

License:Open Source License

protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
    final ControllableShape csTriangle = new ControllableShape(canvas, eventBus) {
        {//w w w . j a  v  a  2 s  .  co  m
            addControlPoints(new Point(100, 150), new Point(350, 120), new Point(200, 300));
        }

        @Override
        public Polygon getShape() {
            return new Polygon(getPoints());
        }

        @Override
        public void onDraw(Canvas gc) {
            // we draw them later
        }
    };
    final ControllableShape csEllipse = new ControllableShape(canvas, eventBus) {
        {
            addControlPoints(new Point(280, 230), new Point(380, 280));
        }

        @Override
        public Ellipse getShape() {
            Point[] points = getPoints();
            double a = Math.abs(points[0].x - points[1].x);
            double b = Math.abs(points[0].y - points[1].y);
            return new Ellipse(points[0].x - a, points[0].y - b, 2 * a, 2 * b);
        }

        @Override
        public void onDraw(Canvas gc) {
            // we draw them later
        }

        @Override
        public void onMove(int dragPointIndex, double oldX, double oldY) {
            if (dragPointIndex == 0) {
                double dx = controlPoints.get(0).getX() - oldX;
                double dy = controlPoints.get(0).getY() - oldY;
                ControlPoint cp = controlPoints.get(1);
                cp.setX(cp.getX() + dx);
                cp.setY(cp.getY() + dy);
            }
        }
    };
    ControllableShape other = new ControllableShape(canvas, eventBus) {
        @Override
        public IGeometry getShape() {
            return null; // does not control a geometry
        }

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

            Path trianglePath = csTriangle.getShape().toPath();
            Path ellipsePath = csEllipse.getShape().toPath();
            Path intersection = Path.intersect(trianglePath, ellipsePath);

            context.setFillStyle("red");
            CanvasDrawer.fillPath(trianglePath, context);

            context.setFillStyle("green");
            CanvasDrawer.fillPath(ellipsePath, context);

            context.setFillStyle("yellow");
            CanvasDrawer.fillPath(intersection, context);
        }
    };

    return new ControllableShape[] { csTriangle, csEllipse, other };
}

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) {
        {/*from   w  ww . j a  v  a  2s .c om*/
            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  www  . j ava  2s . c  o  m
            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 {/*from  w  ww.ja va 2s  .c o m*/
        // 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);
    }
}

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   ww w .j av  a2  s.c  o  m*/

    //
    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);
}

From source file:jetbrains.jetpad.projectional.domUtil.TextMetricsCalculator.java

License:Apache License

private static int measureHeight(Font font, String text) {
    Canvas canvas = canvas();/*from   www .j a v  a 2 s . c o  m*/
    Context2d ctx = canvas.getContext2d();

    ctx.setFont(getFontString(font));
    ctx.setFillStyle("rgb(255, 0, 0)");

    int width = (int) ctx.measureText(text).getWidth();
    int canvasHeight = font.getSize() * 2;
    canvas.setHeight(canvasHeight + "px");
    canvas.setHeight(font.getSize() * 2 + "px");
    canvas.setWidth(width + "px");

    ctx.fillText(text, 0, font.getSize());
    ImageData data = ctx.getImageData(0, 0, width, canvasHeight);
    int firstY = canvasHeight - 1;
    int lastY = 0;
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < canvasHeight; y++) {
            int red = data.getRedAt(x, y);
            if (red != 0) {
                if (firstY > y) {
                    firstY = y;
                }
                if (lastY < y) {
                    lastY = y;
                }
            }
        }
    }
    return lastY - firstY;
}

From source file:nl.mpi.tg.eg.experiment.client.view.ColourPickerCanvasView.java

License:Open Source License

private void sizeAndPaintCanvases(final int canvasHeight, final int canvasWidth, final int barWidth) {
    this.canvasHeight = canvasHeight;
    this.canvasWidth = canvasWidth;
    mainCanvas.setCoordinateSpaceHeight(canvasHeight);
    mainCanvas.setCoordinateSpaceWidth(canvasWidth);
    mainCanvas.setSize(canvasWidth + "px", canvasHeight + "px");
    hueCanvas.setCoordinateSpaceHeight(canvasHeight);
    hueCanvas.setCoordinateSpaceWidth(barWidth);
    hueCanvas.setSize(barWidth + "px", canvasHeight + "px");
    final Context2d hueContext2d = hueCanvas.getContext2d();

    CanvasGradient hueGradient = hueContext2d.createLinearGradient(0, 0, 0, canvasHeight);
    for (double stop = 0; stop <= 10; stop += 0.005) {
        hueGradient.addColorStop(stop * 0.1f, "hsl(" + 36 * stop + ",100%,50%)");
    }//from   ww w.j  a v  a  2 s . c  o  m
    hueContext2d.setFillStyle(hueGradient);
    hueContext2d.fillRect(0, 0, barWidth, canvasHeight);
}

From source file:nl.mpi.tg.eg.experiment.client.view.ColourPickerCanvasView.java

License:Open Source License

synchronized private void setHue(String colourCss) {
    currentHueCss = colourCss;//from   www. j av  a2  s .com
    // " Android clearRect / fillRect bug" ???
    // GWT documentation: JavaScript interpreters are single-threaded, so while GWT silently accepts the synchronized keyword, it has no real effect.
    // So we are using a simple boolean which should be adequate most of the time. We could use a timer call back, but we want to keep this simple.
    // However the browser is probably only single threaded anyway.
    if (hueChangeInProgress) {
        return;
    }
    hueChangeInProgress = true;
    final Context2d mainContext2dA = mainCanvas.getContext2d();
    CanvasGradient linearColour = mainContext2dA.createLinearGradient(0, 0, canvasWidth, 0);
    linearColour.addColorStop(1f, "white");
    linearColour.addColorStop(0f, colourCss);
    mainContext2dA.setFillStyle(linearColour);
    mainContext2dA.fillRect(0, 0, canvasWidth, canvasHeight);

    // todo: remove the second context get if it proves unhelpful witht the samsung 4.2.2 issue
    final Context2d mainContext2dB = mainCanvas.getContext2d();
    CanvasGradient linearGrey = mainContext2dB.createLinearGradient(0, 0, 0, canvasHeight);
    linearGrey.addColorStop(1f, "black");
    linearGrey.addColorStop(0f, "rgba(0,0,0,0)");
    mainContext2dB.setFillStyle(linearGrey);
    mainContext2dB.fillRect(0, 0, canvasWidth, canvasHeight);
    hueChangeInProgress = false;
}