Home » Java » Desktop Java » GWT and HTML5 Canvas Demo

About Mark Anro Silva

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:



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));
 private void createCanvas(){
     canvas.setWidth(width + "px");
     canvas.setHeight(height + "px");
     context = canvas.getContext2d();
     context.fillRect(100, 50, 100, 100);
     context.fillRect(200, 150, 100, 100);
     context.fillRect(300, 250, 100, 100);

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area from our partners over at ZipRecruiter



  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. what is the difference between setCoordinateSpaceHeight and setHeight? do you really need to set both?

Leave a Reply

Your email address will not be published. Required fields are marked *


Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns