|
ImageLoader
Loader class which invokes a callback when the specified image resources are finished loading in the browser.
Code ExamplesThe following example shows how ImageLoader can be used with GWTCanvas to draw images with transformations. Drawing an image (Scaling THEN translating before drawing). Please note that you must put all drawing code involving the image in the supplied callback. Drawing order for images and paths is guaranteed only within the callback method when working with images (since they have to be loaded first, and it can not be known ahead of time how long that will take). import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.widgetideas.graphics.client.GWTCanvas;
import com.google.gwt.widgetideas.graphics.client.ImageHandle;
import com.google.gwt.widgetideas.graphics.client.ImageLoader;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class MyEntryPoint implements EntryPoint {
public void onModuleLoad() {
// Make a new canvas 400x400 pixels
final GWTCanvas canvas = new GWTCanvas(400,400);
String[] urls = new String[] {"gwt_logo.jpg"};
ImageLoader.loadImages(urls, new ImageLoader.CallBack() {
public void onImagesLoaded(ImageHandle[] imageHandles) {
ImageHandle img = imageHandles[0];
canvas.translate(40, 40);
canvas.scale(0.5f, 0.5f);
canvas.drawImage(img, 0, 0);
}
});
RootPanel.get().add(canvas);
}
}
|
► Sign in to add a comment
In version for 1.7 ImageHandle? has been replaced by ImageElement?
I have spent hours trying to get drawImage() to work. I changed ImageHandle? to ImageElement? as per the comment but onImagesLoaded() never executes. Any help would be MUCH appreciated. I am using ver 1.7. I love GWT but unless I can get drawImage() to work it will not be useful to me. --David
I'm having the same problems (onImagesLoaded() never executes). I see there's a bug logged for this but a comment afterwards says the submitter found that it was there problem.
This seems to be a common issue. I suspect it's something that we've done wrong but since multiple people have encountered the issue it might be a good idea to document it here. If someone has a solution, please post it here. - Rob
onImagesLoaded does get executed. You need to create a "public" folder in your projects root folder. Put all your images there. This little bit of info is not mentioned anywhere but it is located in a sample program for gwt. tested with gwt 2.0
Now if someone can help me with my problem. I need to reference the imageElements list from outside of the callback function - within the same class it was executed. Any ideas?
@nonoufriou: you can make the ImageElement? a class-variable.
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.dom.client.ImageElement; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.widgetideas.graphics.client.Color; import com.google.gwt.widgetideas.graphics.client.GWTCanvas; import com.google.gwt.widgetideas.graphics.client.ImageLoader; public class MyEntryPoint implements EntryPoint { /* So we can access the ImageElement set by the callback method */ private ImageElement imageElement; public void onModuleLoad() { final GWTCanvas canvas = new GWTCanvas(600, 400); canvas.setBackgroundColor(Color.ALPHA_GREY); RootPanel.get().add(canvas); String[] urls = new String[] {"http://www.gstatic.com/codesite/ph/images/defaultlogo.png"}; ImageLoader.loadImages(urls, new ImageLoader.CallBack() { public void onImagesLoaded(ImageElement[] imageElements) { imageElement = imageElements[0]; canvas.drawImage(imageElement, 100, 100); } }); } }I'm trying to load images using a servlet which locates images on the server and sends them to the browser. Sometimes the servlet might throw error because the image is not available. How can I be notified that something errored and the image will not be loaded? Usually the browsers receive 404 or 500 HTTP messages if something goes wrong. But how can I detect that using the ImageLoader? Thanks, Daniel
I found the answer to the above question by myself. I implemented my own image loader which handles onerror event on the Image object. This way I can notify the caller if the Image failed to be loaded. In my case I made it simple so that my image loader handles one Image at a time, but I'm sure the incubator version can be somehow improved to handle the onerror event too. I think this would be a valuable improvement for the future.
Has ImageLoader morphed into something new for GWT 2.2?