🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.java-games
Article #41

Re: createImage(int,int) in places other than applets

#41
From: Jim Sculley
Date: Sat, 11 Dec 1999 00:00
39 lines
2532 bytes
Anthony Baraff wrote:
> 
> I'm writing a game in Java and I wanted to carry out double buffering for
> the animation in the game.  I have used createImage while working with
> applets it always worked just fine.  The same method exists for the
> java.swing components.  I'm trying to use a JPanel as the backdrop for the
> animation.  I can call the createImage successfully but it always returns a
> null reference. So when I attempt to get a graphics object from the Image
> the program fails.  I create the Image in the constructor of the JPanel that
> I am extending.  Does anyone know what to do?  Is this a bug in the API or
> am I doing something wrong.  I get no exception from the createImage() call.
> It just always creates a null reference.  Any help would be greatly
> appreciated.
> 
> Thanks,
> 
> Anthony Baraff

The createImage method will return 'null' if you have not done one of
two things:

a) the Component whose createImage() method is called must be visible

b) The native OS peer for the Component whose createImage() method is
called must exist.

I believe item 'a' is just a subset of item 'b' and calling setVisible()
creates the peer (I haven't examined the source code).  In any case the
peer existing is what is really important, since your JPanel doesn't
really exist in a general sense, the peer is doing all the work.

You can eliminate your problem by calling setVisible() before
createImage() for you JPanel, or if you can't have your JPanel visible
before calling createImage() you can call addNotify() which will create
the peer.

Jim S.
-- 
>======= http://www.abraxis.com/niceguy =======<
Technical Details
Message-ID: <3852C66E.832251A7@abraxis.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <#7e$sQBR$GA.274@cpmsnbbsa05>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,78a38cb06d5481ac
X-Google-Attributes: gid111d45,public
X-Google-Thread: 10fd6d,78a38cb06d5481ac
X-Google-Attributes: gid10fd6d,public
X-Google-Thread: 1033cf,78a38cb06d5481ac
X-Google-Attributes: gid1033cf,public
From: Jim Sculley <niceguy@abraxis.com>
Subject: Re: createImage(int,int) in places other than applets
Date: 1999/12/11
Message-ID: <3852C66E.832251A7@abraxis.com>
X-Deja-AN: 559494064
Content-Transfer-Encoding: 7bit
References: <#7e$sQBR$GA.274@cpmsnbbsa05>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
X-Trace: newsfeed.slurp.net 944948545 216.47.65.132 (Sat, 11 Dec 1999 15:42:25 CDT)
MIME-Version: 1.0
NNTP-Posting-Date: Sat, 11 Dec 1999 15:42:25 CDT
Newsgroups: alt.comp.lang.java-games,borland.public.jbuilder.java.swing,comp.lang.java.gui