Advertisement

[java] Multiple requests for same image

Started by May 02, 2001 08:45 AM
13 comments, last by dr-m 23 years, 9 months ago
Hello, I''m having problems with a piece of code requesting the same image serveral times from the server. What the code does is cutting out tiles from a image containing a collection of tiles. Consider this simple (complete) test applet;
  
import java.awt.*;
import java.applet.*;
import java.awt.image.*;

class Tile
{
    private int tile[];

    public Tile(Image srcImage, int x, int y, int w, int h)
    {
        tile = new int[w * h];

        PixelGrabber pg = new PixelGrabber(srcImage, x, y, w, h, tile, 0, 24);
        try {
            pg.grabPixels();
        } catch(Exception e)
        {
            return;
        }
    }
}

public class ImageTest extends Applet
{
    private Tile tile[];

    public void init()
    {
        Image srcImage = null;

        MediaTracker tracker
            = new MediaTracker(this);
        try {
            srcImage = getImage(getCodeBase(), "tiles.gif");
            tracker.addImage(srcImage, 0);
            tracker.waitForAll();
        } catch(InterruptedException e) {
            System.err.println(e);
        }

        tile = new Tile[60];

        loadTilesFromImage(srcImage);
    }

    private void loadTilesFromImage(Image tileSetImage)
    {
        int i,j,s;

        for (s=0, i=0; i<3; i++)
        {
            for (j=0; j<20; j++, s++)
            {
                tile[s] =
                    new Tile(tileSetImage, j*24, i*24, j*24+24, i*24+24);
                System.out.println("Grabbed tile " + s);
            }
        }
    }
}
  
When this code is requesting images from a webserver, the requests made can be logged. IE5.5 makes these requests to my server while executing this piece of code, *ONCE*;
quote:
172.16.35.115 - - [02/May/2001:15:36:49 +0200] "GET /test/ImageTest.htm HTTP/1.1" 304 - 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/ImageTest.class HTTP/1.1" 304 - 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/Tile.class HTTP/1.1" 304 - 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 206 17474 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:50 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522 172.16.35.115 - - [02/May/2001:15:36:51 +0200] "GET /test/tiles.gif HTTP/1.1" 200 19522
Netscape seems to post a lot fewer request, but there''s still more than just one, as you might be expecting. Can somebody please enlighten me? Why is this happening? What can I do differently to prevent this rape of bandwith? Launching this applet over a 28k8-modem connection, is really depressing... / dr-m
/ dr-m
I can''t tell you why it is happening but making a .jar archive might be a solution...
- although it seems like you want to load things more dynamically

? is the init method invoked more than once ?
? is the init method the right place to load the tiles for your game ?

- just some ideas ...
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
Advertisement
The example above is just the bare minimum of code to produce the error. I don''t load any resources in my init() in the real applet.

You meen bundling all resources into a JAR and the loading it? Could be a favorable way of doing it. Any examples of loading JAR resources in java 1.1 from applet?

But the program execution progresses like you''d expect otherwise, eg. init() is called once and so on. I can''t see why the VM needs to fetch the same image over and over again, and if it has to do that - why not fetch it 60 times then, one for each tile??

/ dr-m

Try deleting all the code but the init() method then run that. Does the MediaTracker generate just one GET for the image? I''ve never seen a server log, so I don''t know what its supposed to look like.

Dave
Just tried that, but as I expected, only the logical requests were made;

quote:


127.0.0.1 - - [03/May/2001:09:21:24 +0200] "GET /test/ImageTest.htm HTTP/1.1" 304 -
127.0.0.1 - - [03/May/2001:09:21:30 +0200] "GET /test/ImageTest.class HTTP/1.1" 200 828
127.0.0.1 - - [03/May/2001:09:21:30 +0200] "GET /test/tiles.gif HTTP/1.1" 206 17474




Are eveybody who are doing this, just ignoring that the same image gets downloaded serveral times!?!
/ dr-m
I think the problem is that each use of a PixelGrabber is causing the image to be "renewed", which in this cause means that IE has to ask the server for it again. I guess IE is doing this as it''s not sure whether the image has changed after you last used it (its thick), so it''s effectively calling flush on the image.

I guess the solution is for you to pixel grab the entire image once, and then pass in the pixels to the tiles, and let them get the desired sections, bit more fiddly.

John
Advertisement
Really, who knows what pixel grabber does under the hood?

Try cloning the image once you get it from the server, then only use this clone. If the problem is PixelGrabber trying to get the image from it''s origin, the clone might solve it.
Well, I''ll try cloning, but the clone will probally just tell pixelgrabber the same thing - go get the URL resource! Yay!

Here''s more code that will cause an additional request (but "just" one):

(if I could get rid of this one, and use the other pixelgrabber approach mentioned here, I''d be free from the problem...)

  CropImageFilter cropimagefilter =            new CropImageFilter(sourceRc.getX(), sourceRc.getY(),                    sourceRc.getWidth(), sourceRc.getHeight());FilteredImageSource filteredimagesource =            new FilteredImageSource(_sourceImage.getSource(), cropimagefilter);return c.createImage(filteredimagesource);  


/ dr-m
/ dr-m
... Well, almost free from it. Doing it the way lilspikey suggested will cause two request also. One when loading it, and an additional reuqest when doing the one single pixelgrabbing.

I hate to compromise, but I tend to do that a lot when coding games in java..
/ dr-m
You could try not using the mediatracker, as that will force a request, which would happen anyway when the pix grabber grabs the pixels.

John

p.s. As a total hack/kludge. Grab the pixels, uses the pixels to make a new image (using mem image source), and then use the new image as you would have the original. Any request to get the source will just look at the array of pixels. Urgh!

Edited by - lilspikey on May 3, 2001 12:59:57 PM

This topic is closed to new replies.

Advertisement