🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #17934

Re: Doubble buffering in a canvas.

#17934
From: Hando
Date: Thu, 24 Sep 1998 00:00
93 lines
4036 bytes
From the Java programmers FAQ http://www.afu.com/javafaq.html

A peer component needs to exist for your component before you can get
its image. This is done by the method addNotify() (surely one of the
most poorly named methods in all Java -- it doesn't mean "add a Notify".
It means "Notify that the Component has been added to a Container". It
tells the system, "you need to create the peer for this Component now").
addNotify will be called for you when you add your component to a
container. Javasoft recommends against explicit calling (and, presumably
overriding also) of addNotify() by applications programmers - this is
especially important in these days of Lightweight components that do not
have peers. If you have any code that requires peer resources, move it
into a thread that is started from a conditional branch of the paint()
or update() method. 
     A common reason for seeming to require peer resources in a
constructor, or alternatively in the getPreferredSize() method, (which
is also usually before the peer is created) is to measure the area
required for your window, in terms of font and image sizes. Font sizes
may be obtained by calling
Toolkit.getDefaultToolkit().getFontMetrics(somefont). This does not
require a peer. Image sizes may be obtained by waiting for the relevant
Image to load from the ImageProducer by using an ImageObserver, or a
MediaTracker (see 8.15), also without requiring a peer. See 15.4 for
more discussion of this problem. 
     If you override addNotify(), don't forget to call super.addNotify()
in your overriding version. 


Cheers,

Stuart.

John P. McGrath wrote:
> 
> Martin:
> 
> The problem is that the createImage(int,int) method will return
> null if used before the Component has been associated with a
> Toolkit.  This is not documented in the Component class docs, but
> obviously, it should be.
> 
> If you think about it, after you create the off-screen image, you
> then get a Graphics object for that image.  You could then do a
> number of operations with that Graphics object, such as get a
> FontMetrics object for a particular font.  Since this depends on
> the graphics output device, it clearly needs that information.
> 
> To solve your problem, create the Image and the Graphics object
> in the addNotify() method:
> 
>     public void addNotify() {
>         super.addNotify();
>         imagen = this.createImage(400, 400);
>         g = imagen.getGraphics();
>     }
> 
> --
> Regards,
> 
> John P. McGrath
> <mcgrath@enter.net>
> <jpm@nscs.fast.net>
> 
> Martin Fuente <sistemas@gbreuer.com.ar> wrote:
> 
> > I have a NullPointerException error when the program executes the "g =
> > imagen.getGraphics()" sentence
> > in the constructor. This is not an applet. It's an application.
> > I tried this code alone with an Init method and occured the same error.
> > Can anybody help me with this?
> > thank you very much.
> >
> >
> > import java.io.*;
> > import java.awt.*;
> > import java.awt.Graphics;
> >
> > public class MarcaGeneralCanvas extends Canvas {
> >   int fila, width, height;
> >   Image imagen;
> >   Graphics g;
> >
> >   public MarcaGeneralCanvas(Marca marca) {
> >      imagen = this.createImage(400, 400);
> >      g = imagen.getGraphics();
> > .......
> >    }
> >
> >
> >   public void paint (Graphics g2) {
> >      g2.drawImage(Imagen,.....)
> >   .. ...
> >   }
Technical Details
Message-ID: <3609A85C.E6B7F98E@tig.com.au>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <6ttqkl$ao64@forums.borland.com> <MPG.106ea95b22eab1c59896b3@forums.inprise.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,a7d917cc0ce6dd31
X-Google-Attributes: gidf74b3,public
From: Hando <hando@tig.com.au>
Subject: Re: Doubble buffering in a canvas.
Date: 1998/09/24
Message-ID: <3609A85C.E6B7F98E@tig.com.au>
X-Deja-AN: 394218614
Content-Transfer-Encoding: 7bit
References: <6ttqkl$ao64@forums.borland.com> <MPG.106ea95b22eab1c59896b3@forums.inprise.com>
Content-Type: text/plain; charset=us-ascii
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language