Todd Wilson wrote: > We're building a client-side Java app in which we'd like to > interchangeably use JPG, GIF, and SVG images as transparently as > possible. So, for example, we could pass a GIF, JPEG, or SVG to a > JButton to use as an icon, and it wouldn't care what the file format was.
The best way of doing this is to implement content loaders. With a content loader, any time content of a particular mime-type is found, the content handler for that type is loaded, it processes the file format and hands back an object in return. Take a look at java.net.ContentHandler. Inside the content handler, when you get the stream, you fire up the Batik parser and renderer do the processing and then the return value is your SVG image. If you still want to work within the AWT Toolkit API, the return value should be ImageProducer rather than Image. The ImageProducer is used by Toolkit.createImage() to then create its own Image instance that is returned to you. Of course, if using Toolkit is high on your priority, and only using java.net.URL instances is good enough, then you can skip the ImageProducer bit. I've got an imageloader library that does just this at http://www.vlc.com.au/imageloader, but doesn't do SVG content currently. In the end, your code looks like this: URL.setContentLoaderFactory(new MySVGContentLoaderFactory()); URL url = new URL("http://foo.com/myimage.svg"); Object img_src = url.getContent(); Icon icon = null; if(img_src instanceof Image) icon = new ImageIcon((Image)img_src); else { ImageProducer prod = (ImageProducer)img_src; Image img = Toolkit.getDefaultToolkit().createImage(prod); icon = new ImageIcon(img); } This last bit can be replaced by any number of different bits of code. I have my own ImageUtils class that does the same thing and is a lot more efficient than the standard toolkit code. If you need it, I can send you that as well. The hard part of your implementation will be configuring Batik to produce the single image for you. I've never done anything with ImageProducer that would generate animated images (ie animated GIFs). I'm not sure how well an SVG file with animation would work in this case. I suspect you would have to do some funky stuff with a derived version of BufferedImage or something like that so that you can force swing to repaint your icon all the time during the animation cycle. The standard toolkit code certainly wouldn't work with an ImageProducer as that code expects a fixed number of frames, not any endless frame rendering that an SVG file could give you. However, if you are just defining static images, it's a non-issue. -- Justin Couch http://www.vlc.com.au/~justin/ Java Architect & Bit Twiddler http://www.yumetech.com/ Author, Java 3D FAQ Maintainer http://www.j3d.org/ ------------------------------------------------------------------- "Humanism is dead. Animals think, feel; so do machines now. Neither man nor woman is the measure of all things. Every organism processes data according to its domain, its environment; you, with all your brains, would be useless in a mouse's universe..." - Greg Bear, Slant ------------------------------------------------------------------- --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
