Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Create an Image within a Servlet

0 views
Skip to first unread message

Steve M

unread,
Oct 14, 1999, 3:00:00 AM10/14/99
to
I want to create an Image in a servlet running on Linux Apache JServ. I
don't have and don't want an XServer running on the machine, so I can't use
Frame.createImage. My image data is coming from a MemoryImageSource.

SO, given a MemoryImageSource, how do I create an Image without touching a
Frame? Using the default toolkit doesn't seem to help at all. I'm sure this
all has to do with ImageProducers and consumers, and I'll be trying to find
info and examples how to use them, but if anyone can quickly post an example
or offer alternatives or suggestions, that would be extremely helpful.

Thanks in advance.


Luke Webber

unread,
Oct 15, 1999, 3:00:00 AM10/15/99
to
AFAIK you should be free to use java.awt.Image. Just instantiate it and use
getGraphics() to get a graphics context for it.

Luke

Steve M wrote in message <7u5iip$3ja$1...@autumn.news.rcn.net>...

Steve M

unread,
Oct 15, 1999, 3:00:00 AM10/15/99
to
No, Image is abstract. I'd have to create a BufferedImage, but while I do
know the width and height of the image I'm creating, I do not know its type.

What's happening is an Image is created by an applet (just using
Applet.createImage(width, height)). Stuff gets drawn into the image in the
applet, and then I use a PixelGrabber to get the Image pixels into an int
array. I then send the width, height and pixels down to the Servlet, which
uses the pixel array as a MemoryImageSource, and that's my ImageProducer.

So: when I call Applet.createImage, is that returning a BufferedImage? If
so, then I could cast the result of Applet.createImage to BufferedImage,
call BufferedImage.getType, and then pass that to the Servlet as well as the
Image's width, height and pixels, and then the Servlet could instantiate a
BufferedImage using the (width, height, type) constructor.

Barring that, a) is the type of Image returned by Applet.createImage
platform-independent, and if so b) what is it?

(btw I'm not going to be drawing into the Image so I don't need to do a
getGraphics() on it. I'm converting the Image to JPEG and writing it out on
the server.)

Luke Webber <lu...@webber.com.au> wrote in message
news:7u5plh$1o9s$1...@arachne.labyrinth.net.au...

Dimitri I. Rakitine

unread,
Oct 26, 1999, 3:00:00 AM10/26/99
to
You can run 'dummy' Xserver - it is very useful in cases like this.
I might be wrong (somebody will correct me), but in 1.1 you cannot
instantiate awt objects without corresponding native peers. Running
virtual framebuffer X server will definitely help.

Dimitri

Steve M <nos...@mapson.com> wrote:
: I want to create an Image in a servlet running on Linux Apache JServ. I

jean_t...@my-deja.com

unread,
Oct 26, 1999, 3:00:00 AM10/26/99
to
Hi Steve,
I have the same problem. Except that I have no idea about Xservers :(.

I have tried the following:
----------------------------------------------------
I would like to output from my servlet an image in format GIF that I
will dynamically generate.

I have acquired a class GifEncoder from ACME. It outputs in a
OutputStream an Image. Great stuff!

Now my problem is how can I generate an Image from my servlet?
I have tried the following:

class myServlet...
{
...
private void OutGIFString(String sOut, OutputStream out) throws
IOException
{
int w = 200;
int h = 50;
int size = width * height;
int pixels[] = new int[size];
for (int i = 0; i < size; i++) {
pixels[i] = Color.black.getRGB();
}

MemoryImageSource source = new MemoryImageSource(width, height,
pixels, 0, width);
Toolkit tlk = Toolkit.getDefaultToolkit();
Image Img = tlk.createImage(source);
Graphics g = Img.getGraphics();

g.setColor(Color.red);
g.setFont(new Font("TimesRoman", Font.BOLD, 12));
g.drawString(sOutput, 10, 50);

GifEncoder GIFEnc = new GifEncoder( Img, out );
}
...

----------------------------
When I run this application I get:

java.lang.IllegalAccessError: getGraphics() only valid for images
created with createImage(w, h)
at sun.awt.windows.WImage.getGraphics(WImage.java:32)
at ImageServer.ImageServer.OutputGIFString(ImageServer.java,
Compiled Code)
at ImageServer.ImageServer.handleRequest(ImageServer.java,
Compiled Code)
at ImageServer.ImageServer.doGet(ImageServer.java:73)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:715)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at
com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:140)
at
com.sun.web.core.InvokerServlet.service(InvokerServlet.java:169)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at
com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:140)
at com.sun.web.core.Context.handleRequest(Context.java:375)
at
com.sun.web.server.ConnectionHandler.run(ConnectionHandler.java:135)
-------------------------
How can I create the Image?
createImage(w, h)is a method from Component, which is an abstract class.
From an Applet it would be simple to create the image but how can I
create it from a Servlet?
Any help would be greatly appreciated. Thanks...
Jean
----------------------------------------------------

In article <7v3cb1$b38$2...@nntp6.atl.mindspring.net>,


Sent via Deja.com http://www.deja.com/
Before you buy.

0 new messages