Messages of subject
Using Color with PJA in a Headless environment |
David N
Location : Australia
Member since : Jun 1, 2006
Messages : 2
|
Jun 6, 2006 at 3:16 AM
Hi,
I wrote a piece of code to generate thumbnails of images using the Java.awt but I found out that I need to run it in a headless environment. I switched to using the getDefaultToolkit() in the PJA package.
Using:
image = PJAToolkit.getDefaultToolkit().getImage(sourceFile.getPath());
rather than:
image = Toolkit.getDefaultToolkit().getImage(sourceFile.getPath());
Which works great.
But I need to be able to set the background color for the image in case it is a GIF with transparency. I could do that using Java.awt when I wrote the image:
graphics2D.drawImage(image, 0, 0, newWidth, newHeight, bgcolor, null);
Where bgcolor is a Color object. But Color does not work in a headless environment.
Does anyone have any ideas about what I might be able to do? Is there anything in the PJA Toolkit that might help me?
Thanks in advance for any help,
David
|
Manu
Location : Paris / France
Member since : Apr 29, 2003
Messages : 394
|
Jun 6, 2006 at 6:58 PM
Retrieve a Graphics instance bound to a PJAImage object.
Then, use its fillRect and drawImage methods...
If you use the classes cited in PJA Toolkit FAQ "What methods can be called in a servlet with no problem of Toolkit access for end users ?", your code should look like this :
// PJAGraphicsManager replaces safely Toolkit in headless environments
PJAGraphicsManager manager = PJAGraphicsManager.getDefaultGraphicsManager();
Image image = manager.getImage(sourceFile.getPath());
// This is the easiest way to load an image synchronously
// you may also use a standard ImageObserver (see PJA examples)
((PJAImage)image).sync();
// Create an empty image at same size
Image imageWithBackground =
manager.createImage(image.getWidth(null), image.getHeight(null));
// Get its Graphics object
Graphics g = imageWithBackground.getGraphics();
// Change current color safely (java.awt.Color needs X11 libs until JDK 1.4 included)
((PJAGraphicsExtension)g).setColor (red, green, blue);
g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
g.drawImage(image, 0, 0, null);
// Do whatever you want with imageWithBackground
--- Manu (moderator/modérateur)
|
David N
Location : Australia
Member since : Jun 1, 2006
Messages : 2
|
Jun 7, 2006 at 2:28 AM
Hi,
Thanks for that, mate.
I'll have another play with it and see how it goes.
Cheers
David
|