• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a Java class from a servlet to display images dynamically.  RSS feed

 
VineetK Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. I am facing a pretty peculiar problem. I am building a dynamic web application, which is supposed to display a particular set of shapes, based upon the user input, given by a JSP. The user's input is handled by a servlet, which performs some calculations and then generates co-ordinates of the shapes to display.

Now, I first tried to implement the display part by using applets, but soon realized that the embedded applets would display only static images and not dynamically. I searched over the net and found that image generation can be done extending JFrame class. But now I am in a fix on how to call the display class, from the servlet.

So my question is....Is it possible to call a java class from a servlet to display set of shapes dynamically. If yes, how am I supposed to pass parameters from the servlet to java class? I searched over the net for this but couldn't find anything.

Any help on how to implement this (including any other method), would be greatly appreciated. Thanks in advance.

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JFrame class by itself won't help with this, since it would be displayed on the server, not on the client.

Applets can absolutely display highly dynamic content, not just static content. The JChart2D library (on SourceForge) could be used for displaying dynamic images in an applet.

If, on the other hand, the image only depends on user input (and maybe some further processing on the server), but doesn't need to be updated after it's been generated (until the point where the user submits new parameters), then something like the cewolf tag library may fit the bill; see the link in my signature.
 
Kumar Kamlesh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vineet,

You can write a generic function to generate shapes based on user's input. For example if you have to draw a rectangle, you can take the width and height as the argument of the function and you can generate the shape. Some of the APIs you can use to generate shapes are:

java.awt.Color, java.awt.Font, java.awt.FontMetrics, java.awt.Graphics2D etc.

You can call this function in the service method of your servlet to generate the image passing the required inputs for image generation. For writing the image on the server you need to do following things:

1. Set the content type to image/jpg. [This is must]
2. Use ImageIO to write the image to servlet.
3. Write a HTML image tag in your jsp which simply points to your servlet. (Also pass the required inputs, you may use query string)

Code snippet for Step 1 & 2:


Hope this helps.

- Kamlesh
 
VineetK Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JChart and Cewolf seem to be used for charting problems. I need to draw 2D shapes like rectangles and squares. Will they be helpful for that?
 
Kumar Kamlesh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vineet,

Since I have not used JChart and Cewolf, I can not comment on that. But I am sure you can generate rectangles and squares using java.awt.Graphics2D class. Have a look at the APIs provided by Graphics2D and BufferedImage class. Specially the drawImage() method of Graphics2D class and the constructor of BufferedImage class.

- Kamlesh
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to draw 2D shapes like rectangles and squares.

That's the kind of information that would have been useful to have in the first post.
JChart and Cewolf seem to be used for charting problems. Will they be helpful for that?

No.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!