Solved

draw graphics through Servlet!!!

Posted on 2003-10-27
11
793 Views
Last Modified: 2012-06-27
Does anybody know if servlet supports  java.awt.* and javax.swing.*?

The following sentences are not working because of exception!

Toolkit.getDefaultToolkit().getImage("map800grid.gif");
g.drawLine(0, 0,800,800);

Urgent! Does anybody know why?????? Thanks a lot!
0
Comment
Question by:anxx0018
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 14

Expert Comment

by:kennethxu
ID: 9630341
>> Does anybody know if servlet supports  java.awt.* and javax.swing.*?
The answer is NO.
servlet is running at server side doesn't have access to client window. applet is designed for this purpose.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9630344
>> Toolkit.getDefaultToolkit().getImage("map800grid.gif");
if you already have a gif file, why don't sent it to browser directly?
0
 

Author Comment

by:anxx0018
ID: 9630546
But I still need to get data from database on server and making drawings(draw lines to simulate some intrument) according to these data.
What do you think is a good way for it?

Thanks a lot!!!!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
vk33 earned 100 total points
ID: 9631688
Hi!

I suggest using Image I/O API in your servlet code:

1. Read your background "grid" image like this:
File f = new File("c:\images\myimage.gif");
BufferedImage bi = ImageIO.read(f);

2. Make all necessarry changes upon the loaded image (e.g. drawing lines) using standart Imaging API.

3. Return your final image through ImageWriter:
Iterator writers = ImageIO.getImageWritersByFormatName("png");
ImageWriter writer = (ImageWriter)writers.next();
writer.setOutput(response.getOutputStream());
writer.write(bi);

4. In your JSP you put something like this:
<img src="MyServlet">

For more information have a look here:
http://java.sun.com/j2se/1.4.1/docs/guide/imageio/

Good luck!
0
 

Author Comment

by:anxx0018
ID: 9631930
Thank you, vk33 and Kennethxu.

So Imaging I/O API is supported by servlet.  Why java.awt.* and javax.swing.* are not supported by servlet?

I will try the imaging I/O API and if I have new questions, I will bother you guys further. Thanks a lot!
0
 
LVL 4

Expert Comment

by:vk33
ID: 9632774
Well, both awt and swing are GUI libraries. They are used for building user interface in standalone programs and applets. Servlet is (certainly) running on server side and serving client requests such as HTTP GET and POST etc. Servlet has no "graphics context" (applet region, window, full screen), it's "invisible". Just handling requests and giving out responses, nothing else...

So, the purpose of awt and swing is maintaining GUI, not image processing. But of course some basic features are available. On the other hand, Imaging API is designed for processing images.

Regards!
0
 

Author Comment

by:anxx0018
ID: 9637115
vk33,

I tried the below program, but there is exception. I do not know why. Can you help me with this?

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class Try extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException {

      File f= new File("/home/anqian/Java/Graphics/map400grid.gif");
      BufferedImage bi = ImageIO.read(f);

      Iterator writers = ImageIO.getImageWritersByFormatName("gif");
      ImageWriter writer = (ImageWriter)writers.next();
      writer.setOutput(response.getOutputStream());
      writer.write(bi);

     }
}


The error is:
java.util.NoSuchElementException
      at javax.imageio.spi.FilterIterator.next(ServiceRegistry.java:804)
      at javax.imageio.ImageIO$ImageWriterIterator.next(ImageIO.java:814)
      at Try.doGet(Try.java:18)

Sorry for bother you again! Thanks a lot!
0
 

Author Comment

by:anxx0018
ID: 9637213
One more question:

Is java.awt.Graphics2D  standard java imaging API ? Thanks a lot!!!
0
 

Author Comment

by:anxx0018
ID: 9637465
Please ignore my last two questions. I have solved them. :)

But I have a new question here:

Make all necessarry changes upon the loaded image (e.g. drawing lines) using standart Imaging API.

      OutputStream out = response.getOutputStream();
      BufferedImage bi = ImageIO.read(f);
      Graphics2D image=bi.createGraphics();
      Line2D line = new Line2D.Float(0,0,400,400);
      image.draw(line);

????? How could I write this new image out?
      ??????       ImageIO.write((BufferedImage)image,"png",out);
Thanks a lot!!!!!
0
 
LVL 4

Expert Comment

by:vk33
ID: 9639649
Thanks for the points! AFAIK this should work, no matter if you make changes to the image or not:

Iterator writers = ImageIO.getImageWritersByFormatName("png");
ImageWriter writer = (ImageWriter)writers.next();
writer.setOutput(response.getOutputStream());
writer.write(bi);

Any problems here?
0
 

Author Comment

by:anxx0018
ID: 9646308
Thank you very much, vk33.

Great! The program is working very well.  Thanks a lot !
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
difference between member and local variables. 29 144
maven project in eclipse 11 60
Market Place not working in Eclipse Luna 2 132
thymeleaf natural templating vs JSP 2 165
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question