load image using mediatracker

Hi,

I am using mediatracker in loading image.

try {

MediaTracker tracker=new MediaTracker(null);

while(i<=filenames.length-1) {

Image im = Toolkit.getDefaultToolkit().getImage("D:/photos/thumb/"+filenames);

tracker.addImage(im,i);

tracker.waitForID(i);

width=im.getWidth(null);

height=im.getHeight(null);

}

}catch(Exception e) { out.println(e.getMessage()); }

but it gived error : null

where is problem?

[499 byte] By [pvanjavaa] at [2007-11-26 15:49:53]
# 1
Print the whole stacktrace instead of the message only. The stacktrace will tell you where the error is.Kaj
kajbja at 2007-7-8 22:09:34 > top of Java-index,Java Essentials,New To Java...
# 2
do we still use mediatracker?i haven't used this in 5-6 years
CarrieHunta at 2007-7-8 22:09:34 > top of Java-index,Java Essentials,New To Java...
# 3
I guess filenames is an array of String.Make sure you concatenate the correct filepath:Image im = Toolkit.getDefaultToolkit().getImage("D:/photos/thumb/"+filenames[i]);
KlausJJa at 2007-7-8 22:09:34 > top of Java-index,Java Essentials,New To Java...
# 4

Why bother with MediaTracker? Use ImageIO from package javax.imageio.

Demo (our glorious leader):

import java.awt.image.*;

import java.io.*;

import java.net.*;

import javax.imageio.*;

import javax.swing.*;

public class ImageIOExample {

public static void main(String[] args) throws IOException {

BufferedImage image = ImageIO.read(new URL("http://today.java.net/jag/bio/JagHeadshot.jpg"));

int w = image.getWidth(); //if you want...

int h = image.getHeight();//if you want...

Icon icon = new ImageIcon(image);

JLabel label = new JLabel(icon);

JFrame f = new JFrame("ImageIOExample");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(label);

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

}

DrLaszloJamfa at 2007-7-8 22:09:34 > top of Java-index,Java Essentials,New To Java...
# 5
But Lazlo isn't MediaTracker used mainly to check the status of image loading process.Can the same be accomplished using ImageIO.I checked the ImageIO api but i didn't strike gold.Can you emlighten me a bit on this.
qUesT_foR_knOwLeDgea at 2007-7-8 22:09:34 > top of Java-index,Java Essentials,New To Java...
# 6

> But Lazlo isn't MediaTracker used mainly to check the

> status of image loading process.Can the same be

> accomplished using ImageIO.I checked the ImageIO api

> but i didn't strike gold.Can you emlighten me a bit

> on this.

Most people (99%, from what I've seen) use the old MediaTracker class to synchronously load an image (calling one of the waitFor* methods). If this is all they're doing, why not use ImageIO and get all its benefits? For example, BufferedImage has plenty of functionality missing from plain old java.awt.Image.

So why are those people still using MediaTracker -- because they haven't heard about ImageIO.

If you want to monitor the loading of an image when using ImageIO, check out javax.imageio.event.IIOReadProgressListener. You can use it to easily drive a JProgressBar to show how far along you are in loading an image.

DrLaszloJamfa at 2007-7-8 22:09:34 > top of Java-index,Java Essentials,New To Java...
# 7

> Most people (99%, from what I've seen) use the old

> MediaTracker class to synchronously load an image

> (calling one of the waitFor* methods). If this is all

> they're doing, why not use ImageIO and get all its

> benefits? For example, BufferedImage has plenty of

> functionality missing from plain old java.awt.Image.

>

>

> So why are those people still using MediaTracker --

> because they haven't heard about ImageIO.

>

> If you want to monitor the loading of an image when

> using ImageIO, check out

> javax.imageio.event.IIOReadProgressListener. You can

> use it to easily drive a JProgressBar to show how far

> along you are in loading an image.

Ya.I doubt many have heard about ImageIO.Infact some people using MediaTracker to be used for a project.Funnily i was informed about ImageIO just a week back by some member of this community but i didn't care to check because MediaTracker helped me accomplish what i wanted.Thanks for enlightening me Jam.

qUesT_foR_knOwLeDgea at 2007-7-8 22:09:34 > top of Java-index,Java Essentials,New To Java...
# 8

Hi,

Thanks,

It is working fine - and return actual width and height in first time.

BufferedImage image = ImageIO.read(new URL("http://today.java.net/jag/bio/JagHeadshot.jpg"));

int w = image.getWidth(); //if you want...

int h = image.getHeight();//if you want...

pvanjavaa at 2007-7-8 22:09:34 > top of Java-index,Java Essentials,New To Java...