🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Doubble buffering in a canvas.

#17911
From: John P. McGrath
Date: Sun, 20 Sep 1998 00:00
60 lines
2152 bytes
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: <MPG.106ea95b22eab1c59896b3@forums.inprise.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <6ttqkl$ao64@forums.borland.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: mcgrath@enter.net (John P. McGrath)
Subject: Re: Doubble buffering in a canvas.
Date: 1998/09/20
Message-ID: <MPG.106ea95b22eab1c59896b3@forums.inprise.com>
X-Deja-AN: 392954236
References: <6ttqkl$ao64@forums.borland.com>
Organization: Software Consulting Services
Newsgroups: borland.public.jbuilder.java.language