|
|
How do I create an image with a servlet, for example, a graph out of a list of values? |
|
Servlets allow you to specify the content type of the
response. This ability let you generate dynamic images simply mapping the <img> tag
with the proper servlet URL. To do this, you need to generate an off-screen image object
and send it encoded in your favorite image format. Free GIF image encoders are not
available due to patent restrictions, while JDK 1.2 contains a JPG image encoder (and few
others like PNG may be added in the future). To create an off-screen image you could follow these guidelines: Frame f = new Frame(); f.addNotify() // <-- this creates a "frame peer", which makes it all work Image img = f.createImage(); doMyPaintImage(img.getGraphics()); (etc.) Note that it apparently needs to be a Frame since Canvas doesn't seem to work. It
probably just calls for the toplevel window component to do the job. (Thanks to Bjorn
Sandberg).
| |
|
If you are running on a Unix box, then to create a Frame the Java process needs access to a X server (and the DISPLAY environment variable set). If you do not want the overhead of running an X server, then you can use Xvfb which creates a virtual X server.
| |
| [Append to This Answer] |
| ||||||||||