List of usage examples for com.google.gwt.canvas.dom.client Context2d setTextAlign
public final void setTextAlign(TextAlign align)
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);// www . j av a2s . com 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.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//from w w w . j a va 2 s . co 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:gov.nist.spectrumbrowser.client.SensorDataStream.java
License:Open Source License
@Override public void onMessage(String msg) { // TODO : Make this configurable. int nSpectrums = 200; // # of spectrums to show in the spectrogram window. double yScale = 0; try {// ww w. j a va 2 s . c o m if (state == STATUS_MESSAGE_NOT_SEEN) { JSONValue statusMessage = JSONParser.parseLenient(msg); JSONObject jsonObj = statusMessage.isObject(); if (jsonObj.get("status").isString().stringValue().equals("NO_DATA")) { Window.alert("NO Data Available"); websocket.close(); spectrumBrowserShowDatasets.draw(); } else if (jsonObj.get("status").isString().stringValue().equals("OK")) { state = STATUS_MESSAGE_SEEN; } } else if (state == STATUS_MESSAGE_SEEN) { dataMessage = JSONParser.parseLenient(msg); logger.finer("msg = " + msg); JSONObject mpar = dataMessage.isObject().get("mPar").isObject(); nFrequencyBins = (int) mpar.get("n").isNumber().doubleValue(); // The default cutoff value (add 2 to the noise floor). cutoff = round(dataMessage.isObject().get("cutoff").isNumber().doubleValue()); if (cutoff < 0) cutoffTextBox.setText(Integer.toString((int) (cutoff - 0.5))); else cutoffTextBox.setText(Integer.toString((int) (cutoff + 0.5))); logger.finer("n = " + nFrequencyBins); minFreqHz = (long) mpar.get("fStart").isNumber().doubleValue(); maxFreqHz = (long) mpar.get("fStop").isNumber().doubleValue(); minFreq = (mpar.get("fStart").isNumber().doubleValue() / 1E6); maxFreq = mpar.get("fStop").isNumber().doubleValue() / 1E6; sys2detect = dataMessage.isObject().get("Sys2Detect").isString().stringValue(); // For computing the occupancy, determine the cutoff. logger.finer("fStart / fStop = " + Double.toString(minFreq) + " " + Double.toString(maxFreq)); Context2d ctx = frequencyValuesCanvas.getContext2d(); ctx.setTextAlign(TextAlign.LEFT); ctx.fillText(Double.toString(maxFreq), 0, 10, 100); ctx.fillText("Freq (MHz)", 0, SpectrumBrowser.SPEC_HEIGHT / 2 - 4, 100); ctx.fillText(Double.toString(minFreq), 0, SpectrumBrowser.SPEC_HEIGHT - 4, 100); spectrogramFragment = Canvas.createIfSupported(); spectrogramFragment.setWidth(SpectrumBrowser.SPEC_WIDTH + "px"); spectrogramFragment.setHeight(SpectrumBrowser.SPEC_HEIGHT + "px"); spectrogramFragment.setCoordinateSpaceWidth(SpectrumBrowser.SPEC_WIDTH); spectrogramFragment.setCoordinateSpaceHeight(SpectrumBrowser.SPEC_HEIGHT); spectrogramFragment.getCanvasElement().setWidth(SpectrumBrowser.SPEC_WIDTH); spectrogramFragment.getCanvasElement().setHeight(SpectrumBrowser.SPEC_HEIGHT); state = DATA_MESSAGE_SEEN; context2d.setFillStyle(CssColor.make("black")); context2d.fillRect(0, 0, SpectrumBrowser.SPEC_WIDTH, SpectrumBrowser.SPEC_HEIGHT); spectrogramFragment.setVisible(false); double timePerMeasurement = (float) mpar.get("tm").isNumber().doubleValue(); timeResolution = (float) (dataMessage.isObject().get("_spectrumsPerFrame").isNumber().doubleValue() * timePerMeasurement); HTML html = new HTML("<h2>Sensor Data Stream for " + sensorId + "</h2>"); titlePanel.add(html); HTML help = new HTML("<p>Click on spectrogram to freeze/unfreze. " + "Click on occupancy point to show spectrum</p>"); titlePanel.add(help); String filter = dataMessage.isObject().get(Defines.STREAMING_FILTER).isString().stringValue(); float freqResolution = round((float) (maxFreq - minFreq) / nFrequencyBins * 1000); html = new HTML("<h3>Resolution Bandwidth = " + freqResolution + " kHz.; Detected System = " + sys2detect + "; Time resoultion = " + AbstractSpectrumBrowserScreen.round2(timeResolution) + " s. Aggregation Filter = " + filter + " </h3>"); titlePanel.add(html); } else if (state == DATA_MESSAGE_SEEN) { String[] values = msg.split(","); int powerValues[] = new int[values.length]; int occupancyCount = 0; for (int i = 0; i < values.length; i++) { powerValues[i] = Integer.parseInt(values[i].trim()); if (powerValues[i] > cutoff) { occupancyCount++; } } float occupancy = round(((double) occupancyCount / (double) values.length) * 100); double xScale = ((double) SpectrumBrowser.SPEC_WIDTH / (double) nSpectrums); if (chartApiLoaded && occupancyDataTable == null) { occupancyDataTable = DataTable.create(); spectrumDataTable = DataTable.create(); occupancyPlotOptions = ScatterChartOptions.create(); occupancyPlotOptions.setBackgroundColor("#f0f0f0"); occupancyPlotOptions.setPointSize(5); occupancyPlotOptions.setHAxis(HAxis.create("Time (sec)")); VAxis vaxis = VAxis.create("Occupancy %"); vaxis.setMaxValue(100.0); vaxis.setMinValue(0); occupancyPlotOptions.setVAxis(vaxis); Legend legend = Legend.create(); legend.setPosition(LegendPosition.NONE); occupancyPlotOptions.setLegend(legend); spectrumPlotOptions = ScatterChartOptions.create(); spectrumPlotOptions.setBackgroundColor("#f0f0f0"); spectrumPlotOptions.setPointSize(5); spectrumPlotOptions.setHAxis(HAxis.create("Frequency (MHz)")); spectrumPlotOptions.setVAxis(VAxis.create("Power (dBm)")); legend = Legend.create(); legend.setPosition(LegendPosition.NONE); spectrumPlotOptions.setLegend(legend); occupancyPlot = new ScatterChart(); spectrumPlot = new ScatterChart(); occupancyPlot.addSelectHandler(new SelectHandler() { @Override public void onSelect(SelectEvent event) { if (!isFrozen) { logger.finer("Please Freeze canvas before clicking"); return; } else { double minPower = 10000; double maxPower = -10000; for (int i = 0; i < powerValuesList.size(); i++) { int[] data = powerValuesList.get(i); for (int j = 0; j < data.length; j++) { if (data[j] < minPower) { minPower = data[j]; } if (data[j] > maxPower) { maxPower = data[j]; } } } JsArray<Selection> selection = occupancyPlot.getSelection(); int row = selection.get(0).getRow(); logger.finer("Selected row" + row); int[] spectrumData = powerValuesList.get(row); double mhzPerDivision = (maxFreq - minFreq) / spectrumData.length; for (int i = 0; i < spectrumData.length; i++) { double freq = minFreq + mhzPerDivision * i; spectrumDataTable.setCell(i, 0, freq, freq + " Mhz"); spectrumDataTable.setCell(i, 1, spectrumData[i], spectrumData[i] + " dBm"); } HAxis haxis = HAxis.create("Freq. MHz."); haxis.setMinValue(minFreq); haxis.setMaxValue(maxFreq); VAxis vaxis = VAxis.create("Power (dBm)"); vaxis.setMinValue(minPower); vaxis.setMaxValue(maxPower); spectrumPlotOptions.setVAxis(vaxis); spectrumPlotOptions.setHAxis(haxis); spectrumPlot.draw(spectrumDataTable, spectrumPlotOptions); } } }); occupancyPlot.setPixelSize(SpectrumBrowser.SPEC_WIDTH + 260, SpectrumBrowser.SPEC_HEIGHT); occupancyPlot.setTitle("Occupancy"); spectrumPlot.setPixelSize(SpectrumBrowser.SPEC_WIDTH + 260, SpectrumBrowser.SPEC_HEIGHT); occupancyPanel.add(occupancyPlot); spectrumPanel.add(spectrumPlot); occupancyDataTable.addColumn(ColumnType.NUMBER, "Time (sec)"); occupancyDataTable.addColumn(ColumnType.NUMBER, "Occupancy %"); spectrumDataTable.addColumn(ColumnType.NUMBER, "Frequency (MHz)"); spectrumDataTable.addColumn(ColumnType.NUMBER, "Power (milliwatts)"); spectrumDataTable.setColumnLabel(0, "Frequency (MHz)"); spectrumDataTable.setColumnLabel(1, "Power (mw)"); occupancyDataTable.addRows(nSpectrums); spectrumDataTable.addRows(powerValues.length); DataView dataView = DataView.create(occupancyDataTable); for (int i = 0; i < nSpectrums; i++) { occupancyDataTable.setCell(i, 0, round3(i * timeResolution), round3(i * timeResolution) + " sec"); occupancyDataTable.setCell(i, 1, 0, "0 % occupancy"); occupancyPlot.draw(dataView, occupancyPlotOptions); } // Initialize the spectrum list for (int i = 0; i < nSpectrums; i++) { int[] dummyValues = new int[values.length]; for (int j = 0; j < dummyValues.length; j++) { dummyValues[j] = 0; } powerValuesList.add(dummyValues); } counter = nSpectrums - 1; } if (!isFrozen) { if (occupancyDataTable != null) { occupancyDataTable.removeRow(0); occupancyDataTable.addRow(); int rowCount = occupancyDataTable.getNumberOfRows(); counter++; for (int i = 0; i < nSpectrums; i++) { occupancyDataTable.setCell(i, 0, round3(i * timeResolution), round3(i * timeResolution) + " sec"); } occupancyDataTable.setCell(rowCount - 1, 1, occupancy, occupancy + " % occupancy"); occupancyPlot.redraw(); powerValuesList.remove(0); powerValuesList.add(powerValues); // occupancyPlot.draw(dataTable); } context2d.save(); Context2d tempContext = spectrogramFragment.getContext2d(); tempContext.drawImage(spectrogramCanvas.getCanvasElement(), 0, 0, (double) SpectrumBrowser.SPEC_WIDTH, (double) SpectrumBrowser.SPEC_HEIGHT); RootPanel.get().add(spectrogramFragment); // nSpectrums = powerValues.length / nFrequencyBins; yScale = (double) SpectrumBrowser.SPEC_HEIGHT / (double) nFrequencyBins; for (int i = 0; i < powerValues.length; i++) { CssColor color = colorMap.getColor(powerValues[i]); int row = (int) ((i % nFrequencyBins) * yScale); int col = (int) ((i / nFrequencyBins) * xScale); context2d.setFillStyle(color); double x = SpectrumBrowser.SPEC_WIDTH - col - xScale; double y = SpectrumBrowser.SPEC_HEIGHT - row - yScale; double w = xScale; double h = yScale; context2d.fillRect(x, y, w, h); } context2d.translate(-xScale, 0); context2d.drawImage(spectrogramFragment.getCanvasElement(), 0, 0, spectrogramFragment.getCanvasElement().getWidth(), spectrogramFragment.getCanvasElement().getHeight(), 0, 0, SpectrumBrowser.SPEC_WIDTH, SpectrumBrowser.SPEC_HEIGHT); // reset the transformation matrix context2d.setTransform(1, 0, 0, 1, 0, 0); RootPanel.get().remove(spectrogramFragment); } } } catch (Throwable ex) { logger.log(Level.SEVERE, "ERROR parsing data ", ex); } }
From source file:gov.nist.spectrumbrowser.client.SensorDataStream.java
License:Open Source License
@Override public void draw() { try {//from w ww .j ava2 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); } }
From source file:org.catrobat.html5player.client.Scene.java
License:Open Source License
/** * * @param text// w w w .j av a2s. c o m * @param x * @param y * @param align */ public void write(String text, double x, double y, String align) { Context2d context = sceneCanvas.getContext2d(); context.setTextAlign(align); context.fillText(text, x, y); }
From source file:stroom.pipeline.structure.client.view.PipelineElementRenderer.java
License:Apache License
private void drawText(final Context2d ctx, final double x, final double y, final PipelineElement element) { final CssColor fill = CssColor.make(textColor); ctx.setFont(font);// w w w . ja v a 2s . c om ctx.setTextAlign(TextAlign.LEFT); ctx.setFillStyle(fill); ctx.fillText(element.getId(), x, y); }
From source file:stroom.widget.htree.client.TextCellRenderer.java
License:Apache License
private void drawText(final Context2d ctx, final Bounds bounds, final String text) { final CssColor fill = CssColor.make(textColor); ctx.setFont(font);//from w w w . j ava 2 s . c o m ctx.setTextAlign(TextAlign.LEFT); ctx.setFillStyle(fill); ctx.fillText(text, bounds.getX() + textPadding, bounds.getY() + textPadding + textSize - 1); }