drawImage(Image, x, y, ImageObserver) in a server-side app (not Swing/AWT)

O

OtisUsenet

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!
 
A

ak

I suspect that g2d.drawImage(topImage, 10, 10, Color.red, null); does
not work because the last parameter (ImageObserver interface) is null.
wrong, this is legal.

your image is created but not loaded.
use MediaTracker or better (simpler) put your topImage in ImageIcon.

____________

http://reader.imagero.com the best java image reader.
 
K

Knute Johnson

OtisUsenet said:
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.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class test2 {
public static void main(String[] args) {
try {
BufferedImage bi =
new BufferedImage(320,240,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
BufferedImage bi2 = ImageIO.read(new File("photo.jpg"));
g2d.drawImage(bi2,10,10,Color.red,null);
g2d.dispose();
ImageIO.write(bi,"JPG",new File("test2.jpg"));
} catch (Exception e) {
System.out.println(e);
}
}
}
 
O

OtisUsenet

Thanks!

The javax.imageio.ImageIO class was a discovery for me, and it worked!

Thank you for your quick help!

Knute Johnson said:
OtisUsenet said:
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.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class test2 {
public static void main(String[] args) {
try {
BufferedImage bi =
new BufferedImage(320,240,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
BufferedImage bi2 = ImageIO.read(new File("photo.jpg"));
g2d.drawImage(bi2,10,10,Color.red,null);
g2d.dispose();
ImageIO.write(bi,"JPG",new File("test2.jpg"));
} catch (Exception e) {
System.out.println(e);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top