• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic