Search code examples
gwtcanvas

GWT Canvas2D draw Image


/**
 * This is the entry point method.
 */
public void onModuleLoad() {
    Canvas canvas = Canvas.createIfSupported();
    final Context2d context2d = canvas.getContext2d();

    RootPanel.get("canvas").add(canvas);
    Image img = new Image("face.png");
    final ImageElement face = ImageElement.as(img.getElement());
    img.addLoadHandler(new LoadHandler() {

        @Override
        public void onLoad(LoadEvent event) {
            context2d.drawImage(face, 0, 0);
        }
    });
    //RootPanel.get("canvas").add(img);
}

This is my code. I want to draw the image onto the canvas. This works if the last line:

RootPanel.get("canvas").add(img);

is NOT commented out.

But with the line commented it seems that the image wont be loaded or so. Any ideas?


Solution

  • Have you placed the image within the WEB-INF folder? That's where GWT loads the images from. Normally it's recommend to create an "images" folder so you can load it with "images/face.png".