Java Reference
In-Depth Information
Listing 16-12.
Rendering an SVG Image
InputStream in = …
SVGImage image;
ScalableGraphics sg = ScalableGraphics.createInstance();
Graphics g;
int w, h;
try {
image = (SVGImage)ScalableImage.createImage(in, null);
} catch( IOException e) {};
sg.bindTarget(g);
image.setViewportWidth(w);
image.setViewportHeight(h);
sg.render(0, 0, image);
sg.releaseTarget();
When you create an
SVGImage
instance, you can specify the source data as an instance
of an
InputStream
subclass or as a URL. When you create an instance, you can also specify
an external resource handler that the
SVGImage
uses to load subelements. Usually, you
don't need to do this—you can simply pass
null
. However, you might want to do this
when your image has components to be fetched from over the network, for example; in
that case, you'd provide an implementation of the
ExternalResourceHandler
that wraps the
HttpConnection
.
Of course, Listing 16-12 doesn't translate well to the MIDP graphics environment.
Listing 16-13 builds on Listing 16-12 and shows a simple
SVGImageItem
class that extends
the MIDP
CustomItem
class that I first discuss in Chapter 5. Instances of the
SVGImageItem
class are suitable for addition to
Form
items in your MIDlet.
Listing 16-13.
The SVGImageItem Class for Rendering a Static SVG Image on a Form
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.m2g.*;
public class SVGImageItem extends CustomItem {
private ScalableGraphics sg;
private SVGImage image;
private int prefWidth, prefHeight;