Newbie Help: Java Object to create an image
Hi there.
I'm trying to create a Java class that I can call from Coldfusion that will build a GIF file based on parameters.
The idea is, the Java object opens a "background" gif file and then writes some text onto it and saves the whole thing out as a GIF.
Bearing in mind this class will be used directly by Coldfusion, and so isn't an applet or an application...can anyone tell me what is wrong with this code as I get a null pointer exception on the getGraphics() line:
// Methods available to Coldfusion
public String CreateOverlay( String imageFile, String outPath, String overlayText, int x, int y, String fontFace, int PointSize)
{
try{
Toolkit tk = Toolkit.getDefaultToolkit();
Imageim = tk.getImage(imageFile);
Gif89Encoder enc = new Gif89Encoder();
OutputStream out = new BufferedOutputStream(
new FileOutputStream(outPath)
);
Image newimg = createImage(100,50);
*****Graphics g = newimg.getGraphics();
g.drawImage(im, 0, 0, 100, 50, this);
g.drawLine(0,0,50,50);
enc.addFrame( newimg );
enc.encode(out);
out.close();
}
catch (Exception e) {
System.out.println("Error...");
e.printStackTrace();
}
return "Dont care at the moment";
}
Any help to this troubled newbie would be appreciated.
[1388 byte] By [
ukdevcom] at [2007-9-26 1:16:38]

Hi Kurt,
> You must call "setVisible(true)" on your Frame before
> creating the image.
But, the thing is, this object doesnt have (or need) a GUI. Its just an object that sits on the server and its methods are called to generate an image on disk. Basically what happens is Coldfusion receives information from a user and it loads the java object, calls a method which then generates a new GIF file that is made up of a simple button background and some text (entered by the user)....it then returns the name of this file to Coldfusion and Coldfusion stores it in a database for later retrieval.
In simple terms:
COLDFUSION===========
1. Creates an instance of the JAVA Object
2. Call the generateGif method passing an image filename and some text eg:
newFile=c.GenerateGif( "simplebutton.gif", "Some text for button");
JAVA OBJECT========= (GenerateGif method)
1. Load the GIF simplebutton.gif
2. Create a new blank image
3. Draw the loaded GIF onto the blank one
4. Write the Text ("some text for button") onto the image
5. Take the image and encode it to GIF format
6. Write this GIF to disk
7. Return the filename of the new GIF image just created
8. Finish
As you can see, I dont want or need a GUI.....
Is this possible ?
Thanks in Advance,
Tony Johnson
> You can avoid creating the new image through
> createImage with
>
> Image im = tk.getImage(imageFile);
> Graphics g = im.getGraphics();
> g.drawLine(0,0,50,50);
Hmm, I'm sure I've already tried that, but it gave me a different error saying getGraphics() could only be called on images that have been created with createImage.....
I'll take a look at the API's you suggested.
Thanks.
Tony
You may want to use the other thing, but just for reference, To create an image you want to draw on you must do this:
Canvas c = new Canvas();
Image img = c.createImage(width, height);
Graphics g = img.getGraphics();
And you now have an image you can draw on...
GN