Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > drawImage(Image, x, y, ImageObserver) in a server-side app (not Swing/AWT)

Reply
Thread Tools

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

 
 
OtisUsenet
Guest
Posts: n/a
 
      02-23-2004
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!
 
Reply With Quote
 
 
 
 
ak
Guest
Posts: n/a
 
      02-23-2004
> 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.


 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      02-24-2004
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...
 
Reply With Quote
 
OtisUsenet
Guest
Posts: n/a
 
      02-24-2004
Thanks!

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

Thank you for your quick help!

Knute Johnson <(E-Mail Removed)> wrote in message news:<6209382830338985af06a4c2d897556b@news.1usene t.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);
> }
> }
> }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
the easiest way to convert a .net windows app to web app? tom ASP .Net 4 10-24-2008 05:57 PM
Web App OR WinForm App - advice on choosing the UI for an admin utility RJ ASP .Net 1 08-22-2004 05:08 AM
1 server, 2 web sites 1 asp.net app each, build one, other -> Server App Unavailable Dave Diehl ASP .Net 4 08-11-2004 12:52 PM
win app and web app mimi ASP .Net 1 07-29-2004 03:19 AM
Asp.net app, Unable to load dll!!, while same dll loads properly in Win form app NGM ASP .Net 0 11-06-2003 10:34 AM



Advertisments