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

Discussion in 'Java' started by OtisUsenet, Feb 23, 2004.

  1. OtisUsenet

    OtisUsenet Guest

    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!
    OtisUsenet, Feb 23, 2004
    #1
    1. Advertising

  2. OtisUsenet

    ak Guest

    > 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.
    ak, Feb 23, 2004
    #2
    1. Advertising

  3. Re: drawImage(Image, x, y, ImageObserver) in a server-side app (notSwing/AWT)

    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.

    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);
    }
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Molon labe...
    Knute Johnson, Feb 24, 2004
    #3
  4. OtisUsenet

    OtisUsenet Guest

    Thanks!

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

    Thank you for your quick help!

    Knute Johnson <> wrote in message news:<6209382830338985af06a4c2d897556b@news.1usenet.com>...
    > 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.
    >
    > 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);
    > }
    > }
    > }
    OtisUsenet, Feb 24, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    Re: ImageObserver.ABORT

    Roedy Green, Jul 22, 2003, in forum: Java
    Replies:
    1
    Views:
    491
    Charles Thomas
    Jul 22, 2003
  2. Robert Mark Bram
    Replies:
    2
    Views:
    5,938
    Robert Mark Bram
    Dec 11, 2003
  3. Wolfgang
    Replies:
    3
    Views:
    6,069
    Knute Johnson
    Feb 10, 2004
  4. mkrause
    Replies:
    0
    Views:
    658
    mkrause
    May 6, 2005
  5. Replies:
    8
    Views:
    3,087
    Andrew Thompson
    Nov 16, 2006
Loading...

Share This Page