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

Off Screen Images - Creating, Saving

0 views
Skip to first unread message

Newnham, Leonard

unread,
Feb 8, 1999, 3:00:00 AM2/8/99
to
Hi,
There probably is a simple answer to this, but it's beating me.

I am developing a web application for propeller design
(http://helios.bre.co.uk/ccit/people/newnhaml/prop/) where the user
interacts with the program through html forms. Once a prop has been
designed I want to plot some graphs and then display them on the web
page returned to the user.

All I want to do is create an off screen image, draw to it then JPG
encode it and save as a file. I'm using jdk1.2beta3.

This is the first time I have tried to use any image manipulation in
Java and I seem to be doing something very wrong. Any suggestions would
be much appreciated. The code is below.

It fails at the drawLine() line. Also, I'm not at all sure about the
ImageObserver argument of drawImage() - but it doesn't even get that
far!

Thanks,

Leonard
--
Leonard Newnham tel: +44 (0)1923 664117
Centre for Construction IT fax: +44 (0)1923 664689
Building Research Establishment email: newn...@bre.co.uk
Garston www: http://www.bre.co.uk/ccit
Watford WD2 7JR UK
===========================
Code:
//set up buffer and graphics
Container Can=new Container();
BufferedImage offscreenImage= new
BufferedImage(100,100,BufferedImage.TYPE_BINARY);
Graphics2D doubleBuffer = offscreenImage.createGraphics();

//draw line
doubleBuffer.drawLine(10,10,90,90);

//save in image

doubleBuffer.drawImage(offscreenImage,100,100,(ImageObserver)Can);

//save image as JPEG
try {
FileOutputStream os=new
FileOutputStream("TestImage.jpg");
DataOutputStream ds=new DataOutputStream(os);
JPEGImageEncoder J=new JPEGImageEncoder((BufferedImage
offscreenImage,ds);
J.encode();
} catch (IOException e) {
out.println(e.getMessage());
}
==========================

Output: Note fails when drawLine is attempted to be drawn.

java.lang.ClassCastException: sun.awt.image.BytePackedRaster
at
sun.java2d.loops.ByteDiscreteRenderer$DrawLineByteIndexed.DrawLine(ByteD
iscreteRenderer.java:156)
at
sun.java2d.loops.RasterOutputManager.drawLine(RasterOutputManager.java:1
894)
at
sun.awt.image.BufferedImageGraphics2D.drawLine(BufferedImageGraphics2D.j
ava:500)
at sun.java2d.pipe.ValidatePipe.drawLine(ValidatePipe.java:35)
at sun.java2d.SunGraphics2D.drawLine(SunGraphics2D.java:1376)
at DesPropWeb.Prop(DesPropWeb.java:104)
at DesPropWeb.doPost(DesPropWeb.java:1896)
[...]


0 new messages