user1456170
user1456170

Reputation: 155

The speed of the function about loading pictures in java

I test use two methods to load more than 2m size picture in my application.

private void loadImages() {
        long currentTime=System.currentTimeMillis();
        MediaTracker mediaTracker = new MediaTracker(this);
        images = new HashMap<String, Image>();
        for (Iterator iterator = model.getAllImages().iterator(); iterator
                .hasNext(); mediaTracker.addImage(tempImage, 0)) {
            String filename = (String) iterator.next();
            try{
            tempImage=ImageIO.read(Game.class.getResourceAsStream(filename));
            }catch(Exception e){

        }
            //URL imageURL = getClass().getResource(filename);
        //  tempImage = getImage(getCodeBase(), imageURL.toString());
            images.put(filename, tempImage);
        }

        try {
            mediaTracker.waitForID(0);
        } catch (InterruptedException interruptedexception) {
        }
        System.err.println(System.currentTimeMillis()-currentTime);
    }

My testing result shows the ImageIO.read() is twice slowly than the getImage().Could someone explain me why? Waiting for you reply.

Upvotes: 0

Views: 83

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

I would say that two functions work in two different ways.

ImageIO is guaranteed to return you an image that can be painted immediately, where as getImage will return you a reference to a possible image which may be loaded at some time in the future, hence the reason you see paint methods that need ImageObserver

This would explain much of the difference between BufferedImage and Image. BufferedImage allows you direct access to things like the pixel and color model data

Upvotes: 1

Related Questions