🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.lang.java-games
2 messages
2 total messages Started by Anthony Baraff Sat, 11 Dec 1999 00:00
createImage(int,int) in places other than applets
#40
Author: Anthony Baraff
Date: Sat, 11 Dec 1999 00:00
15 lines
1584 bytes
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
Re: createImage(int,int) in places other than applets
#41
Author: 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 =======<
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads