Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generating images  RSS feed

 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am fairly new to servlets and have written a basic servlet to draw a sort of graph from some data. The servlet returns the necessary html and saves out an image file of the graph as .png format to link to. However, it would be nice to include a logo on the graph that comes from an image file, but I'm unsure how to use the drawImage() method of Graphics2D in this servlet case, where I don't have a Component to refer to. Here's some code...

I hope someone can help with this, it should be simple, but it's got me confused!!!
thanks,
Ben
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Images are a separate HTTP request and therefore have to be returned in a separate Servlet. You can't return the page and image all in one hit.
Basically you have a Servlet that defines that returns a content-type of image/gif or image/jpg etc, you build the image then stuff it down the OutputStream.
I tend to use BufferedImages, the Graphics2D Context and the com.sun.image.codec.jpeg package for converting from Graphics to a jpeg. (the sun package is not recommended and not supported and there are other solutions out there, but I was just messing around)
Dave
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might wont to look at sitraka ServerChart component (http://www.sitraka.com) - this is exactly what they do
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!