You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Todd Wilson <to...@byu.edu> on 2002/03/14 19:11:08 UTC

Using GIF, JPEG, and SVG images interchangeably

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.

There are a couple of possible solutions that we've come up with, and 
we're just wondering if anyone else can offer input:

1.  Either write or utilize an SVG wrapper class that implements the 
javax.swing.Icon interface.  This would allow us to pass either an 
ImageIcon object containing a GIF or JPEG, or an SVG wrapped in this 
class to objects, and they could be treated identically.  Does anyone 
know if such a wrapper already exists?

2.  Somehow derive a java.awt.Image object from a Batik-generated SVG 
object.  The closest thing we can find among the Batik packages that 
might be able to do this is the getOffScreen method of the JGVTComponent 
class, though it's not clear to us exactly how the offscreen image is to 
be generated since it doesn't seem to be created by default.  Is there 
another method for deriving an Image object from an existing Batik SVG 
object that we're not seeing?  Does this seem like a viable solution to 
our problem?

Many thanks in advance for any input.

Regards,

Todd Wilson


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Using GIF, JPEG, and SVG images interchangeably

Posted by Justin Couch <ju...@vlc.com.au>.
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: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


RE: Using GIF, JPEG, and SVG images interchangeably

Posted by Tangent <ta...@usa.net>.
Todd,

It will be nice to use different formats for icons on a button just like
images on the web.  In most cases, for a small icon that is 64 by 64 or
smaller, GIF should do most of its trick.  If you want to shoot
something higher with descent transparency, I will suggest PNG.  I might
stay away using SVG as that may take up some unnecessary rasterization
for drawing such element on the screen.  Putting all icons in a similar
format could help maintain the system stability and maintainability.
Just my 2 cents.

- Tangent

> -----Original Message-----
> From: tcw4@email.byu.edu [mailto:tcw4@email.byu.edu] On 
> Behalf Of Todd Wilson
> Sent: Thursday, March 14, 2002 12:11 PM
> To: batik-users@xml.apache.org
> Subject: Using GIF, JPEG, and SVG images interchangeably
> 
> 
> 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.
> 
> There are a couple of possible solutions that we've come up with, and 
> we're just wondering if anyone else can offer input:
> 
> 1.  Either write or utilize an SVG wrapper class that implements the 
> javax.swing.Icon interface.  This would allow us to pass either an 
> ImageIcon object containing a GIF or JPEG, or an SVG wrapped in this 
> class to objects, and they could be treated identically.  Does anyone 
> know if such a wrapper already exists?
> 
> 2.  Somehow derive a java.awt.Image object from a Batik-generated SVG 
> object.  The closest thing we can find among the Batik packages that 
> might be able to do this is the getOffScreen method of the 
> JGVTComponent 
> class, though it's not clear to us exactly how the offscreen 
> image is to 
> be generated since it doesn't seem to be created by default.  
> Is there 
> another method for deriving an Image object from an existing 
> Batik SVG 
> object that we're not seeing?  Does this seem like a viable 
> solution to 
> our problem?
> 
> Many thanks in advance for any input.
> 
> Regards,
> 
> Todd Wilson
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org