Advertisements

OtisUsenet wrote:
> Hello,
>
> I am trying to compose some images....
> First I create a BufferedImage to serve as the background.
> Then I load an Image from the disk, and try drawing it on the
> BufferedImage.
>
> Unfortunately, I am having problems getting my image to render using
> drawImage(Image, int, int, ImageObserver) method in
> java.awt.Graphics2D.
>
> I am doing this in a server-side component, and not in an AWT or Swing
> app.
>
> My code:
>
> BufferedImage image = new BufferedImage(100, 100,
> BufferedImage.TYPE_INT_ARGB);
> Graphics2D g2d = image.createGraphics();
>
> // load image from disk (this returns Image that is != null)
> Image topImage = Toolkit.getDefaultToolkit().createImage("/tmp/myimage.png");
>
> g2d.drawImage(topImage, 10, 10, Color.red, null);
>
> g2d.dispose();
>
> When I save this image to a disk the topImage is not shown.
> If, instead, I write some text over my BufferedImate, like this:
>
> g2d.drawString("Hello World", 10, 10);
>
> This works.
>
> I suspect that g2d.drawImage(topImage, 10, 10, Color.red, null); does
> not work because the last parameter (ImageObserver interface) is null.
>
> But what implementation of ImageObserver interface should I be using
> in a server-side app?
>
> All examples I have seen deal with images in AWT and Swing components,
> which all inherit Component, and thus can use 'this' for the last
> parameter, since Component implements ImageObserver.
>
> Any help would be much appreciated.
> Thank you!

You don't need the ImageObserver for this to work but I did find an
interesting problem when doing some tests. See the program below. It
works fine if the BufferedImage uses TYPE_INT_RGB but if you use
TYPE_INT_ARGB the image is black. I would guess that the alpha is zero
but I don't know why. I don't think it should be. Any way, try this
program.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!