anxx0018
asked on
draw graphics through Servlet!!!
Does anybody know if servlet supports java.awt.* and javax.swing.*?
The following sentences are not working because of exception!
Toolkit.getDefaultToolkit( ).getImage ("map800gr id.gif");
g.drawLine(0, 0,800,800);
Urgent! Does anybody know why?????? Thanks a lot!
The following sentences are not working because of exception!
Toolkit.getDefaultToolkit(
g.drawLine(0, 0,800,800);
Urgent! Does anybody know why?????? Thanks a lot!
>> Toolkit.getDefaultToolkit( ).getImage ("map800gr id.gif");
if you already have a gif file, why don't sent it to browser directly?
if you already have a gif file, why don't sent it to browser directly?
ASKER
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!!!!
What do you think is a good way for it?
Thanks a lot!!!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!
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!
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!
ASKER
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.BufferedIma ge;
public class Try extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
File f= new File("/home/anqian/Java/Gr aphics/map 400grid.gi f");
BufferedImage bi = ImageIO.read(f);
Iterator writers = ImageIO.getImageWritersByF ormatName( "gif");
ImageWriter writer = (ImageWriter)writers.next( );
writer.setOutput(response. getOutputS tream());
writer.write(bi);
}
}
The error is:
java.util.NoSuchElementExc eption
at javax.imageio.spi.FilterIt erator.nex t(ServiceR egistry.ja va:804)
at javax.imageio.ImageIO$Imag eWriterIte rator.next (ImageIO.j ava:814)
at Try.doGet(Try.java:18)
Sorry for bother you again! Thanks a lot!
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.BufferedIma
public class Try extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
File f= new File("/home/anqian/Java/Gr
BufferedImage bi = ImageIO.read(f);
Iterator writers = ImageIO.getImageWritersByF
ImageWriter writer = (ImageWriter)writers.next(
writer.setOutput(response.
writer.write(bi);
}
}
The error is:
java.util.NoSuchElementExc
at javax.imageio.spi.FilterIt
at javax.imageio.ImageIO$Imag
at Try.doGet(Try.java:18)
Sorry for bother you again! Thanks a lot!
ASKER
One more question:
Is java.awt.Graphics2D standard java imaging API ? Thanks a lot!!!
Is java.awt.Graphics2D standard java imaging API ? Thanks a lot!!!
ASKER
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((BufferedIma ge)image," png",out);
Thanks a lot!!!!!
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((BufferedIma
Thanks a lot!!!!!
Thanks for the points! AFAIK this should work, no matter if you make changes to the image or not:
Iterator writers = ImageIO.getImageWritersByF ormatName( "png");
ImageWriter writer = (ImageWriter)writers.next( );
writer.setOutput(response. getOutputS tream());
writer.write(bi);
Any problems here?
Iterator writers = ImageIO.getImageWritersByF
ImageWriter writer = (ImageWriter)writers.next(
writer.setOutput(response.
writer.write(bi);
Any problems here?
ASKER
Thank you very much, vk33.
Great! The program is working very well. Thanks a lot !
Great! The program is working very well. Thanks a lot !
The answer is NO.
servlet is running at server side doesn't have access to client window. applet is designed for this purpose.