🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #3299

Re: CreateImage(w,h) in a Java Application?

#3299
From: Martin Hungerford
Date: Thu, 12 Feb 1998 00:00
77 lines
3010 bytes

Andrew Bennett wrote in message <3495A420.71DCDA02@borland.com>...
>
>
>S.T. wrote:
>
>> Hey,
>>
>> I seem to be having trouble using CreateImage(w,h) in a Java
>> application.  It seems when I try the applet version of my program,
>> everything works fine.  When I embed the same applet in a frame in my
>> application, the CreateImage() call returns NULL, and everything else
>> blows up.
>>
>> The line of code I'm working with is similar to:
>>
>> offScreenImage = createImage(width, height);        //this line fails in
>> an application
>> offScreenGraphic = offScreenImage.getGraphics();
>>
>> (Yes, it's double buffering.)
>>
>> Any ideas, or do I need to create an image in an application another
>> way?
>
>java.awt.Component is abstract, so you'll need to subclass the call
>to something.
>
>From Java in a Nutshell 1.1, page 361 "In a standalone application,
>the URL.getContent() method can be used to obtain an ImageProducer
>object, which can then be passed to the Component.createImage()
>method to obtain an Image object."


That didn't work for me. I then got another error (getGraphics() will only
work for images produced by the createImage(w,h) version).

What I'm doing is really clunky, but it works. I have a reference to class
imageCreator (specified below).  When I need an image I intansiate this
class with the width and height of the desired image. I then add that object
to my top level Frame, resize the Frame and repaint the Frame. I can then
call ImageCreator.getImage() successfully! Having done that I remove the
CreateImage object from the frame (See what I mean about Clunky!)

The only reason I go to all this trouble is because it works. If some-one
else has a simpler workaround for this bug I'd be DELIGHTED.

Martin Hungerford,  jongleur@netcon.net.au


public class ImageCreator extends Canvas {
  Image drawImg;
  Frame f;
  private int myX,myY;

  public ImageCreator(Frame parent, int x, int y){
    setSize(1, 2);
    myX=x;
    myY=y;
    f = parent;
  }

  public Image getImage(){
    numOfImages++;
    int j=0;
    Dimension size = f.getSize();
    if (drawImg == null){
      Graphics g = this.getGraphics();
      update(g);
    }
    return drawImg;
  }

  public void paint(Graphics g){update(g);}
  public void update(Graphics g)  drawImg = createImage(myX,myY);
}
Technical Details
Message-ID: <6bvlv9$gm24@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <34942E67.EE92E8A2@whc.net> <3495A420.71DCDA02@borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,114f8832447813b5
X-Google-Attributes: gid1089d0,public
From: "Martin Hungerford" <jongleur@netcon.net.au>
Subject: Re: CreateImage(w,h) in a Java Application?
Date: 1998/02/12
Message-ID: <6bvlv9$gm24@forums.borland.com>
X-Deja-AN: 324534855
Content-Transfer-Encoding: 7bit
References: <34942E67.EE92E8A2@whc.net> <3495A420.71DCDA02@borland.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Mimeole: Produced By Microsoft MimeOLE V4.71.1712.3
Organization: None
Newsgroups: borland.public.jbuilder.java.api