About Mark Anro Silva

Software Engineer | UI/UX Engineer, Java, JavaFX, GWT and Scala.

GWT and HTML5 Canvas Demo

This is my first experiment with GWT and HTML5 Canvas. My first attempt is to create rectangles, with just a few lines of code I came up something like this:

Code:

 

public class GwtHtml5 implements EntryPoint {
 
 static final String canvasHolderId = "canvasholder";
 static final String unsupportedBrowser = "Your browser does not support the HTML5 Canvas";
 static final int height = 400;
 static final int width = 500;
 final CssColor colorRed = CssColor.make("red");
 final CssColor colorGreen = CssColor.make("green");
 final CssColor colorBlue = CssColor.make("blue");
 Canvas canvas;
 Context2d context;
 
 
 public void onModuleLoad() {
  canvas = Canvas.createIfSupported();
  if (canvas == null) {
        RootPanel.get(canvasHolderId).add(new Label(unsupportedBrowser));
        return;
  }
  createCanvas();
 }
 
 private void createCanvas(){
  
     canvas.setWidth(width + "px");
     canvas.setHeight(height + "px");
     canvas.setCoordinateSpaceWidth(width);
     canvas.setCoordinateSpaceHeight(height);
     
     RootPanel.get(canvasHolderId).add(canvas);
     
     context = canvas.getContext2d();
     
     context.beginPath();
     context.setFillStyle(colorRed);
     context.fillRect(100, 50, 100, 100);
     context.setFillStyle(colorGreen);
     context.fillRect(200, 150, 100, 100);
     context.setFillStyle(colorBlue);
     context.fillRect(300, 250, 100, 100);
     context.closePath();
     
 }
 
}

And my Spring balls experiment with some codes that I found on the Web.

Reference: GWT and HTML5 Canvas Demo from our JCG partner Mark Anro Silva at the GlyphSoft blog.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

2 Responses to "GWT and HTML5 Canvas Demo"

  1. Check out http://www.emitrom-lienzo.appspot.com/#ExplorerPlaceImpl:rectangle for some cool canvas-based rectangle example using a product called Lienzo.

    Lienzo is a canvas-based graphics toolkit implemented using GWT. Most of the primitive shapes are supported out the gate, as are events, animations and transformations. Check it out! http://emitrom.com/lienzo/download

    Best regards.

  2. Zhou Ji says:

    what is the difference between setCoordinateSpaceHeight and setHeight? do you really need to set both?

Leave a Reply


4 − = zero



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books