[quote]just out of curiosity, is there anything very wrong with this?
private Image loadImage(String fileName, Container con, int transparency) {
logger.loadingImage(getClass(), fileName);
Image loadedImage = Toolkit.getDefaultToolkit().getImage(fileName);
Image returnImage = null;
MediaTracker tracker = new MediaTracker(con);
tracker.addImage(loadedImage, 0);
try {
tracker.waitForAll();
}
catch (InterruptedException e) {
logger.error(getClass(), "Error loading image from file: " + fileName);
}
returnImage = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(loadedImage.getWidth(null), loadedImage.getHeight(null), transparency);
returnImage.getGraphics().drawImage(loadedImage, 0, 0, null);
return returnImage;
}
EDIT: a typo
[/quote]
well, you’ve assumed the target machine only has 1 monitor, and 1 video device.
Image loadedImage = Toolkit.getDefaultToolkit().getImage(fileName);
should realy read
Image loadedImage = con.getToolkit().getImage(fileName);
and actually, the Toolkit class recommends that developers don’t use the toolkit.getImage() either, and instead use the createImage version. (and implement their own caching system)
so, I would say this is most correct :-
Image loadedImage = con.getToolkit().createImage(fileName);
The same goes for
returnImage = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice ().getDefaultConfiguration().createCompatibleImage(loadedImage.getWidth( null), loadedImage.getHeight(null), transparency);
you are assuming the default screen device, and default GraphicsConfiguration for that device. (hence the ‘compatible’ Image returned may not infact be ‘compatible’ for the Frame on which you intend to render the image!
So, i’d say the line should look like :-
returnImage = con.getGraphicsConfiguration().createCompatibleImage(loadedImage.getWidth( null), loadedImage.getHeight(null), transparency);
ofcourse for that line to work, you’d also have to change the definition of the method, so ‘con’ was actually a Frame rather than just a Container.
(but it would make more sense passing in a Frame anyway :-
What You want to load (String, would be better as an URL)
Where you intend to render it (The Frame)
How you intend to render it (The transparency type))
P.S.
I was just thinking about the implications of using toolkit.getImage() on a system with multiple display devices (with regard to how the ‘managed image’ version of image would be handled
heres abit of code explaining what I mean :-
Image fred = getTookit().getImage("fred.png");
Image fred2 = getTookit().getImage("fred.png");
//at this point both fred and fred2 (although different objects) may have the same ImageProducer
GraphicsConfiguration gc1 = <a GraphicsConfiguration for device 1>;
GraphicsConfiguration gc2 = <a GraphicsConfiguration for device 2>;
//2 Graphics Configurations for 2 different devices.
Frame f1 = new Frame(gc1);
Frame f2 = new Frame(gc2);
//The 2 destination Frames (displayed on different screen devices)
f1.show();
f2.show();
f1.getGraphics().drawImage(fred);
f2.getGraphics().drawImage(fred2);
// ^ ok, you would need more than 1 draw for the image to be cached in VRAM,
// but, heres my question...
// how would they be cached :S
in that example, how would the automatic images be managed?
2 images sharing the same ImageProducer, while being rendered to 2 different display devices (and hence requiring 2 different VRam copies [potencially in different pixel formats?]) could cause an awful mess, could it not??