Server Side SVG
If you've been using SVG or reading XML.com, you probably know about the Adobe SVG Viewer, and you may have heard of the Apache Batik project. Although Batik is most widely known for its SVG viewer component, it's more than that. Batik, according to its web site, is a “Java technology-based toolkit for applications that want to use images in the Scalable Vector Graphics (SVG) format for various purposes, such as viewing, generation or manipulation.”
The Batik viewer application uses the
component, which is a Java class that accepts SVG as input and
displays it on screen. In this article, we'll use two of the other
SVGGraphics2D, and the Batik
SVGGraphics2D class is the inverse of
JSVGCanvas; you draw into an
environment using the standard Java two-dimensional graphics methods,
and the result is an SVG document. The transcoders take an SVG
document as input and produce either JPG or PNG as output.
The context in which we'll use these tools is a servlet that generates geometric art in the style of Piet Mondrian. If the client supports SVG, the servlet will return an SVG document. Otherwise, it will return a JPEG or PNG image, depending upon client support for those image formats.
The Client Side
The web page that we show users will let them choose the orientation and color scheme of their painting. See the screenshot below for an example of how this might look.
Here's the HTML:
<html> <head> <title>Art-O-Matic</title> </head> <body> <h2>Art-O-Matic</h2> <p> Yes, you too can become a famous artist! With a few simple clicks of the mouse, you can generate geometric art in the style of Piet Mondrian. </p> <form id="artForm" action="http://jde:8080/artmaker/servlet/ArtMaker"> <p> What kind of picture would you like? <br /> <input type="radio" name="picType" value="landscape" checked="checked" /> Landscape <input type="radio" name="picType" value="portrait" /> Portrait <input type="radio" name="picType" value="square" /> Square </p> <p> Which color scheme would you like? <br /> <input type="radio" name="scheme" value="bright" checked="checked" /> Vivid colors <input type="radio" name="scheme" value="pastel" /> Soft pastel </p> </body> </html>
Determining whether the client has SVG support or not must be handled on the client side. We'll add this script, taken from a Sun Microsystems technical note, to do this detection.
In order to have the web page send this information back to the
server, we'll have to change the
<form> tag and add
a hidden field to our form:
<form id="artForm" action="http://jde:8080/artmaker/servlet/ArtMaker" onsubmit="return setSVGStatus();"> <input type="hidden" name="imgType" value="jpg" />
Which requires an additional script to set the hidden field before we send the data to the server:
Pages: 1, 2