1

I am trying to find my way around with the Google Web Toolkit. Right now I am trying to get a Canvas widget up and running.

But I am getting this error and do not understand why:

Compiling module de.kuntze.HelloCanvas
Computing all possible rebind results for 'de.kuntze.client.HelloCanvas.HelloCanvasUiBinder'
  Rebinding de.kuntze.client.HelloCanvas.HelloCanvasUiBinder
     Invoking generator com.google.gwt.uibinder.rebind.UiBinderGenerator
        [ERROR] com.google.gwt.canvas.client.Canvas has no default (zero args) constructor. To fix this, you can define a @UiFactory method on the UiBinder's owner, or annotate a constructor of Canvas with @UiConstructor.
[ERROR] Errors in 'de/kuntze/client/HelloCanvas.java'
  [ERROR] Line 14: Failed to resolve 'de.kuntze.client.HelloCanvas.HelloCanvasUiBinder' via deferred binding
[WARN] For the following type(s), generated source was never committed (did you forget to call commit()?)
  [WARN] de.kuntze.client.HelloCanvas_HelloCanvasUiBinderImpl

My code looks like this:

The module HelloCanvas.gwt.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.1//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.5.1/distro-source/core/src/gwt-module.dtd">
<module>
    <inherits name="com.google.gwt.user.User" />
    <source path="client"/>
    <entry-point class="de.kuntze.client.HelloCanvas"/>
</module>

The UIBinder file HelloCanvas.ui.xml

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui"
xmlns:c='urn:import:com.google.gwt.canvas.client'>
<ui:style>

</ui:style>
<g:HTMLPanel>
    <c:Canvas ui:field="canvas"></c:Canvas>
</g:HTMLPanel>

The Java file HelloCanvas.java

package de.kuntze.client;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class HelloCanvas extends Composite implements EntryPoint{

    private static HelloCanvasUiBinder uiBinder = GWT
            .create(HelloCanvasUiBinder.class);

    @UiField Canvas canvas;

    interface HelloCanvasUiBinder extends UiBinder<Widget, HelloCanvas> {
    }

    public HelloCanvas() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Override
    public void onModuleLoad() {
        canvas = Canvas.createIfSupported();
        canvas.setWidth("400px");
        canvas.setHeight("400px"); 
        canvas.setCoordinateSpaceWidth(400);
        canvas.setCoordinateSpaceHeight(400);
        RootPanel.get().add(this);
    }
}

I bet the answer will be pretty easy but I do not know why I get this error message and why the code does not compile.

Edit: So after trying the advice below it works. Here comes the edited code which draws a black triangle.

The UIBinder file HelloCanvas.ui.xml including a SimplePanel

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
<g:HTMLPanel>
    <g:SimplePanel width="200px" height="200px" ui:field="panel">
    </g:SimplePanel>
</g:HTMLPanel>

The edited Java file HelloCanvas.java

package de.kuntze.client;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class HelloCanvas extends Composite implements EntryPoint {

private static HelloCanvasUiBinder uiBinder = GWT
        .create(HelloCanvasUiBinder.class);

@UiField
SimplePanel panel;

interface HelloCanvasUiBinder extends UiBinder<Widget, HelloCanvas> {
}

public HelloCanvas() {
    initWidget(uiBinder.createAndBindUi(this));
}

@Override
public void onModuleLoad() {
    Canvas tCanvas = Canvas.createIfSupported();
    tCanvas.setWidth("400px");
    tCanvas.setHeight("400px");
    tCanvas.setCoordinateSpaceWidth(400);
    tCanvas.setCoordinateSpaceHeight(400);

    Context2d tContext2d = tCanvas.getContext2d();
    tContext2d.beginPath();
    tContext2d.moveTo(25, 25);
    tContext2d.lineTo(105, 25);
    tContext2d.lineTo(25, 105);
    tContext2d.fill();
    panel.add(tCanvas);

    RootPanel.get().add(this);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
AndreKuntze
  • 163
  • 1
  • 9

1 Answers1

3

You cannot create a Canvas with the UI:Binder, because there is no zero-arg constructor, nor a @UIConstructor.

I would suggst to create a warpper (A simplePanel) and within your Wrapper-Code, you can create a Canvas, by calling Canvas.createIfSupported():

The canvas itself is prvided.

 @UiField(provided = true)
 Canvas canvas;

Before you call binder.createAndBindUi(this); you will have to create the Canvas:

canvas = Canvas.createIfSupported()

I have no simple example, but maybe, this link is helpful: https://code.google.com/p/gwtgae2011/source/browse/src/main/java/com/googlecode/gwtgae2011/client/main/SketchView.java?r=8e7169e7fbb411f320f99f77dcdb27efa27b727a

You also could use a CanvasElement, like described in this question: GWT uibinder CanvasElement wont resize when deployed

Community
  • 1
  • 1
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
  • Thanks for the answer. It has been a busy week but I just tested your advice and it works well. I will mark your answer as solution and add the worrking code above. – AndreKuntze Oct 11 '13 at 20:54