Checking image size with GWT

I have a GWT application that generates SVG files.

I want to insert an image from some URL into my SVG, but I want it to resize while maintaining the correct aspect ratio.

I do this by loading an image using the Image class in GWT and checking the height against width, then doing some sums to find the height and width that I need to be in my SVG.

I am embedding image in SVG like this:

<image href="http://some.image/URL.png" height="100" x="50" width="150" y="50"></image>

      

The problem I am having is when I do the following:

Image image = new Image(sourceURL);
int width = image.getWidth();
int height = image.getHeight();
....

      

The first time I do this for a specific URL, the width or height is returned as 0. Unfortunately, trying again in the loop doesn't seem to fix the problem, but if I ask my application to re-generate the SVG, it works.

Any ideas?

+3


source to share


1 answer


Ok, sorry, this is not an answer, but the comments are too small to put some code.

I tried the following and it worked great. So you should probably try to isolate your problem in a smaller piece of code:



    @Override
    public void onModuleLoad() {

    final Image image = new Image("https://www.google.com/images/srpr/logo3w.png");
    image.addLoadHandler(new LoadHandler() {

        @Override
        public void onLoad(LoadEvent event) {
            Window.alert(image.getWidth() + " " + image.getHeight());
        }
    });
    RootLayoutPanel.get().add(image);
    }

      

+4


source







All Articles