Click to See Complete Forum and Search --> : What does Frame.show() do to make createImage(w,h) work?


maciej
January 12th, 2001, 05:13 PM
Hello,
this is a bit tricky. I created a temp Frame for the sole purpose of being able to call createImage(w,h). I need to create my Image this way because need to be able to obtain Graphics object from the Image. And as far as I know this is the only way to call getGraphics() on an Image object.

The problem is that I must call Frame.show() in order for my createImage(w,h) to work. If I don't, createImage() returns NULL.

My question is does anyone know what does .show() method do that Frame can create Image(s) and can it be duplicated without calling .show()?

Any idea is greatly appreciated! Thanks!
--maciej

poochi
January 12th, 2001, 08:47 PM
I have to start it with the Component.CreateImage() method.
Here is that function. Look at the code carefully


public Image createImage(int width, int height) {
ComponentPeer peer = this.peer;
// peer is instanceof FramePeer. So it goes to else condition
if (peer instanceof java.awt.peer.LightweightPeer) {
return parent.createImage(width, height);
} else {
// if the peer is null, you wont get any Image Object but "null"
return (peer != null) ? peer.createImage(width, height) : null;
}
}




All native components are attached to a peer. ( JApplet, JFrame , JWindow & awt components are native components)
This peer will be created when you try to show that Container/Component or adding that Component
or container to it's parent Container. Just creating the instance will not create the Peer.
In your createImage() code, It checks whether the peer exists are not. If it's null, it wont
create an Image Object. It returns null. So, you need peer to create an Image.

What Frame.show() method do ?? Here is the Window.show() method ( Frame dont have show method.
So, Window.show() will be called )


public void show() {
Container parent = this.parent;
// No parent. So the condtion will not be satisfied.
if (parent != null && parent.getPeer() == null) {
parent.addNotify();
}
// Peer is null. addNotify() will be called.
if (peer == null) {
// This is the guy who creates FramePeer and assign it to your Frame's
// peer member variable
addNotify();
}

......
......

}





Frame dont have parent. So, it executes to the next line.
if (peer == null) {
addNotify();
}

This addNotify() guy creates the FramePeer object and set it to your Frame object. Now
you got the peer. Your createImage(int,int) method will work after this.

Poochi...

poochi
January 12th, 2001, 08:54 PM
> can it be duplicated without calling .show()?

instead of calling .show() you can call "addNotify()"
Or you can create a Hidden control and add it to your container. then call
hiddenComponent.createImage(x,y).