Java Code Examples for com.google.gwt.canvas.client.Canvas#getContext2d()

The following examples show how to use com.google.gwt.canvas.client.Canvas#getContext2d() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TexturedCube.java    From TGAReader with MIT License 6 votes vote down vote up
Canvas createImageCanvas(int [] pixels, int width, int height) {

	    Canvas canvas = Canvas.createIfSupported();
	    canvas.setCoordinateSpaceWidth(width);
	    canvas.setCoordinateSpaceHeight(height);

	    Context2d context = canvas.getContext2d();
	    ImageData data = context.createImageData(width, height);

	    CanvasPixelArray array = data.getData();
	    for(int i=0; i<width*height; i++) {
	        array.set(4*i+0, pixels[i] & 0xFF);
	        array.set(4*i+1, (pixels[i] >> 8) & 0xFF);
	        array.set(4*i+2, (pixels[i] >> 16) & 0xFF);
	        array.set(4*i+3, (pixels[i] >> 24) & 0xFF);
	    }
	    context.putImageData(data, 0, 0);

	    return canvas;

	}
 
Example 2
Source File: ImageCanvasTest.java    From TGAReader with MIT License 6 votes vote down vote up
private Canvas createImageCanvas(int [] pixels, int width, int height) {
	
	Canvas canvas = Canvas.createIfSupported();
	canvas.setCoordinateSpaceWidth(width);
	canvas.setCoordinateSpaceHeight(height);
	
	Context2d context = canvas.getContext2d();
	ImageData data = context.createImageData(width, height);

	CanvasPixelArray array = data.getData();
	for(int i=0; i<width*height; i++) { // ABGR
		array.set(4*i+0, pixels[i] & 0xFF);
		array.set(4*i+1, (pixels[i] >> 8) & 0xFF);
		array.set(4*i+2, (pixels[i] >> 16) & 0xFF);
		array.set(4*i+3, (pixels[i] >> 24) & 0xFF);
	}
	context.putImageData(data, 0, 0);
	
	canvas.getElement().getStyle().setMargin(4, Unit.PX);
	
	return canvas;
	
}
 
Example 3
Source File: StructureView.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void draw(CanvasElement el, String idcode, String coordinates) {
    Canvas canvas = Canvas.wrap(el);
    if (idcode != null && idcode.length() > 0) {
        String combined = idcode + (coordinates != null ? " " + coordinates : "");
        Context2d ctx = canvas.getContext2d();
        drawMolecule(ctx, combined, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight());
    }
}
 
Example 4
Source File: StructureView.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void drawIDCode(CanvasElement el, String idcode, String coordinates, int displayMode,
        String[] atomText) {
    Canvas canvas = Canvas.wrap(el);
    if (idcode != null && idcode.length() > 0) {
        String combined = idcode + (coordinates != null ? " " + coordinates : "");
        Context2d ctx = canvas.getContext2d();
        drawMolecule(ctx, combined, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight(),
                displayMode, atomText);
    }
}
 
Example 5
Source File: DrawArea.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void draw(Canvas canvas) {
  long fg = builder.getForegroundColor();
  Context2d context2d = canvas.getContext2d();
  int w = (int) model.getDisplaySize().getWidth();
  int h = (int) model.getDisplaySize().getHeight();

  int displayMode = model.getDisplayMode();

  drawBackground(context2d, w, h);

  if (isReaction()) {
    model.analyzeFragmentMembership();
    if (model.needsLayout())
      model.cleanReaction(true);
  }

  AbstractExtendedDepictor depictor = createDepictor();
  depictor.setDisplayMode(displayMode);

  if (model.needsLayout()) {
    depictor.updateCoords(null, new java.awt.geom.Rectangle2D.Double(0, 0, (float) w, (float) h),
        AbstractDepictor.cModeInflateToMaxAVBL);
  }
  model.needsLayout(false);

  depictor.paint(context2d);
  // Let the actions draw if needed e.g. NewChainAction
  if (action != null) {
    GraphicsContext ctx = new GraphicsContext(context2d);
    ctx.save();
    ctx.setStroke(fg);
    ctx.setFill(fg);
    action.paint(ctx);
    ctx.restore();
  }
}
 
Example 6
Source File: Util.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void scaleCanvas(Canvas canvas, int width, int height) {
  canvas.setWidth(width + "px");
  canvas.setHeight(height + "px");

  Context2d ctx = canvas.getContext2d();

  double ratio = Util.getDevicePixelRatio();
  canvas.setCoordinateSpaceWidth((int)(width * ratio));
  canvas.setCoordinateSpaceHeight((int)(height * ratio));
  ctx.scale(ratio, ratio);
}
 
Example 7
Source File: SVGToolBarImpl.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Drawing function
 *
 * @param canvas
 */
private void draw(Canvas canvas) {
  Context2d context2d = canvas.getContext2d();
  GraphicsContext ctx = new GraphicsContext(context2d);
  drawButtons(ctx);
  drawESRButtons(ctx);
  drawHover(ctx);
}
 
Example 8
Source File: TextMetricsCalculator.java    From jetpad-projectional-open-source with Apache License 2.0 5 votes vote down vote up
private static int measureHeight(Font font, String text) {
  Canvas canvas = canvas();
  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;
}
 
Example 9
Source File: CirSim.java    From circuitjs1 with GNU General Public License v2.0 4 votes vote down vote up
public Canvas getCircuitAsCanvas(boolean print) {
    	// create canvas to draw circuit into
    	Canvas cv = Canvas.createIfSupported();
    	Rectangle bounds = getCircuitBounds();
    	
	// add some space on edges because bounds calculation is not perfect
    	int wmargin = 140;
    	int hmargin = 100;
    	int w = (bounds.width+wmargin) ;
    	int h = (bounds.height+hmargin) ;
    	cv.setCoordinateSpaceWidth(w);
    	cv.setCoordinateSpaceHeight(h);
    	double oldTransform[] = Arrays.copyOf(transform, 6);
    
	Context2d context = cv.getContext2d();
	Graphics g = new Graphics(context);
	context.setTransform(1, 0, 0, 1, 0, 0);
        
        double scale = 1;
        
	// turn on white background, turn off current display
	boolean p = printableCheckItem.getState();
	boolean c = dotsCheckItem.getState();
	if (print)
	    printableCheckItem.setState(true);
        if (printableCheckItem.getState()) {
            CircuitElm.whiteColor = Color.black;
            CircuitElm.lightGrayColor = Color.black;
            g.setColor(Color.white);
        } else {
            CircuitElm.whiteColor = Color.white;
            CircuitElm.lightGrayColor = Color.lightGray;
            g.setColor(Color.black);
            g.fillRect(0, 0, g.context.getCanvas().getWidth(), g.context.getCanvas().getHeight());
        }
	dotsCheckItem.setState(false);

        if (bounds != null)
            scale = Math.min(w /(double)(bounds.width+wmargin),
                             h/(double)(bounds.height+hmargin));
        scale = Math.min(scale, 1.5); // Limit scale so we don't create enormous circuits in big windows
        
        // ScopeElms need the transform array to be updated
	transform[0] = transform[3] = scale;
	transform[4] = -(bounds.x-wmargin/2);
	transform[5] = -(bounds.y-hmargin/2);
	context.scale(scale, scale);
	context.translate(transform[4], transform[5]);
	
	// draw elements
	int i;
	for (i = 0; i != elmList.size(); i++) {
	    getElm(i).draw(g);
	}
	
	// restore everything
	printableCheckItem.setState(p);
	dotsCheckItem.setState(c);
	transform = oldTransform;
	return cv;
}
 
Example 10
Source File: StructureView.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void drawMolecule(CanvasElement el, JSMolecule mol, int displayMode, String[] atomText) {
    Canvas canvas = Canvas.wrap(el);
    Context2d ctx = canvas.getContext2d();
    drawMolecule(ctx, mol.getStereoMolecule(), canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight(),
            displayMode, atomText);
}
 
Example 11
Source File: AbstractESRPane.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void draw(Canvas toolBar) {
  Context2d context2d = toolBar.getContext2d();
  drawButtons(new GraphicsContext(context2d));
}
 
Example 12
Source File: ToolBarImpl.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void draw(Canvas toolBar) {
  Context2d context2d = toolBar.getContext2d();
  GraphicsContext ctx = new GraphicsContext(context2d);
  drawButtons(ctx);
  drawESRButtons(ctx);
}
 
Example 13
Source File: TextMetricsCalculator.java    From jetpad-projectional-open-source with Apache License 2.0 4 votes vote down vote up
static double calculateWidth(Font font, String text) {
  Canvas canvas = canvas();
  Context2d ctx = canvas.getContext2d();
  ctx.setFont(getFontString(font));
  return ctx.measureText(normalize(text)).getWidth();
}