无法在Handler方法之外到达Canvas元素

时间:2015-08-26 13:04:13

标签: java gwt canvas handler drawimage

伙计们我一直在试图弄清楚这里有什么问题。最后,我找到了我坚持的地方。以下代码是我的项目的片段,显示唯一的问题。 那是我的EntryPoint课程:

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.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.madmax.client.gameservice.GWTService;
import com.madmax.client.gameservice.GWTServiceAsync;
import com.madmax.client.resources.ResourceManager;
import com.madmax.client.resources.images.cards.LandCardResources;


public class AltEntryPoint implements EntryPoint {


    public AltEntryPoint() {
    }

    public static GWTServiceAsync getService() {


        return GWT.create(GWTService.class);
    }
    private final Canvas canvas= Canvas.createIfSupported();
    private final Context2d ctx = canvas.getContext2d();



    @Override
    public void onModuleLoad() {
        final ResourceManager resourceManager = new ResourceManager(1000, 1000);
        Button b1 = new Button("Click", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                                ctx.drawImage(resourceManager.getResourceCanvas().getCanvasElement(), 0, 0);
            }
        });

        resourceManager.loadImgResource(LandCardResources.INSTANCE.getHellHorseKnight(), "knight", 0, 0);
        resourceManager.loadImgResource(LandCardResources.INSTANCE.getMeadow(), "meadow", 150, 150);


        canvas.setWidth(500 + "px");
        canvas.setHeight(500 + "px");
        canvas.setCoordinateSpaceWidth(500);
        canvas.setCoordinateSpaceHeight(500);

                 RootPanel.get().add(resourceManager.getResourceCanvas());
                 ctx.drawImage(resourceManager.getResourceCanvas().getCanvasElement(), 0, 0);
                 RootPanel.get().add(canvas);
                 RootPanel.get().add(b1);

    }

}

那是我的ResourceManeger类

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;


public class ResourceManager {

    private Canvas allImageHolderCanvas;
    private Context2d allImageHolderContext;

    public ResourceManager(int lengthDimension, int heightDimension) {
        allImageHolderCanvas = Canvas.createIfSupported();
        allImageHolderCanvas.setWidth(lengthDimension + "px");
        allImageHolderCanvas.setHeight(heightDimension + "px");
        allImageHolderCanvas.setCoordinateSpaceWidth(lengthDimension);
        allImageHolderCanvas.setCoordinateSpaceHeight(heightDimension);
        allImageHolderContext = allImageHolderCanvas.getContext2d();
    }

    public void loadImgResource(ImageResource resource, String name, double sx, double sy) {
        final Image localImage = new Image(resource);
        localImage.setVisible(false);
        final String localName = name;
        final double sxVal = sx;
        final double syVal = sy;

        localImage.addLoadHandler(new LoadHandler() {

            @Override
            public void onLoad(LoadEvent event) {
                allImageHolderContext.drawImage(ImageElement.as(localImage.getElement()),
                        0, 0, localImage.getWidth(), localImage.getHeight(), sxVal, syVal, 1000, 1000);
            }
        });

        RootPanel.get().add(localImage);
    }

    public void drawImageFromMetaDb(Context2d context, double sx, double sy, double sw,
            double sh, double dx, double dy, double dw, double dh){
        context.drawImage(allImageHolderCanvas.getCanvasElement(), sx, sy, sw, sh, dx, dy, dw, dh);
    }

    public Canvas getResourceCanvas(){
        return this.allImageHolderCanvas;
    }

}

一切都很完美,除了一件已经让我烦恼的事情。

ctx.drawImage(resourceManager.getResourceCanvas()。getCanvasElement(),0,0); 在调用Handler内部之前不起作用。

请解释我错在哪里。非常感谢。

0 个答案:

没有答案