I was trying to set up a simple application that would read in a
picture, and put it on the screen. I admit to being a Java newbie, but
I've done something similar before.
I didn't want a boarder, or header - just the picture. The image was
going to be the same size as the screen.
I created the canvas, read in the GIF image, tried to get the graphics
context of the canvas, and it wouldn't be anything but null.
Researching - found that the graphics image would be null until the
component was visible. Couldn't get it visible.
Researching farther - found Window described as "a top-level window with
no borders and no menubar....A window must have either a frame, dialog,
or another window defined as its owner when it's constructed."
Window is called "top-level" but must have an owner - does that mean it
must be inside another component? Is that the problem with Canvas?
Frame, which is a subclass of Window, also says it is a top-level
window. In my previous work, I've put the canvas in a frame or applet.
So, it looks like I have to have a Frame with a title bar whether I want
it or not. Is there another way to do this?
Thanks!
--
Tom A.
"No doubt many that post deserve to be flamed. And many that are flamed
didn't deserve it. Can you give them the latter? Then don't be so
quick to deal out the former!" - not Gandalf.
Deja mail is gone. Look for me at raugost at yahoo . com
> Hi all,
>
> I was trying to set up a simple application that would read in a
> picture, and put it on the screen. I admit to being a Java newbie, but
> I've done something similar before.
>
> I didn't want a boarder, or header - just the picture. The image was
> going to be the same size as the screen.
> I created the canvas, read in the GIF image, tried to get the graphics
> context of the canvas, and it wouldn't be anything but null.
>
> Researching - found that the graphics image would be null until the
> component was visible. Couldn't get it visible.
>
> Researching farther - found Window described as "a top-level window with
> no borders and no menubar....A window must have either a frame, dialog,
> or another window defined as its owner when it's constructed."
>
> Window is called "top-level" but must have an owner - does that mean it
> must be inside another component? Is that the problem with Canvas?
>
> Frame, which is a subclass of Window, also says it is a top-level
> window. In my previous work, I've put the canvas in a frame or applet.
>
> So, it looks like I have to have a Frame with a title bar whether I want
> it or not. Is there another way to do this?
A JWindow will stand alone. Extend JWindow and Canvas or JPanel and use the
constructor for the Canvas to load your image and then draw it with
Canvas.paint(). You can even get it to auto size if you add the
getPreferredSize() to Canvas.
knute...
How? Are you using a MediaTracker? You should.
> tried to get the graphics
> context of the canvas,
Bad, bad, bad. Never ever use getGraphics(). Read about the AWT and
Swing painting model
http://java.sun.com/products/jfc/tsc/articles/painting/index.html
> Researching - found that the graphics image would be null until the
> component was visible.
Yep, and that's the reason why getGraphics() is not a good idea. I wish
Sun would remove it from the API.
> Researching farther - found Window described as "a top-level window with
> no borders and no menubar....A window must have either a frame, dialog,
> or another window defined as its owner when it's constructed."
>
> Window is called "top-level" but must have an owner - does that mean it
> must be inside another component?
No, it is supposed to have a "parent" which controls certain aspects of
it. Window is not really meant to be used for actual programms, it is
a superclass for the real windows like Frame and Dialog. But you can
use it if you e.g. create an invisible Frame as its owner.
> Is that the problem with Canvas?
There is non, it is just not a top-level window, so it can not be
displayed on the screen without beeing in a window, like a Frame or a
Dialog.
> So, it looks like I have to have a Frame with a title bar whether I want
> it or not. Is there another way to do this?
No, use a Window with an invisible Frame. Or use Java 1.4 with full
screen mode. Or use Swing.
/Thomas
Don't do that. Don't mix heavyweight and leightweight components. Either use
JFrame/JWindow/JPanel/paintComponent(), or Frame/Window/Canvas/paint().
/Thomas
Thank you, all, for your information. In the AWT world, Canvas appears
to be there to put drawings on. But there does not appear to be a
similar JCanvas. What does Swing provide for that?
A JPanel will work just fine.
knute...
Thomas Weidenfeller wrote:
>
> "Tom A." <tar...@my-deja.com> writes:
> > I didn't want a boarder, or header - just the picture. The image was
> > going to be the same size as the screen.
> > I created the canvas, read in the GIF image,
>
> How? Are you using a MediaTracker? You should.
current = Toolkit.getDefaultToolkit().createImage
("d:\\Picture\\FOUNTAIN2.GIF");
> > tried to get the graphics
> > context of the canvas,
>
> Bad, bad, bad. Never ever use getGraphics(). Read about the AWT and
> Swing painting model
>
> http://java.sun.com/products/jfc/tsc/articles/painting/index.html
Looking this over, it looks very good. Thanks for the pointer.
> > Researching - found that the graphics image would be null until the
> > component was visible.
>
> Yep, and that's the reason why getGraphics() is not a good idea. I wish
> Sun would remove it from the API.
But it looks like drawImage is the only way I've seen to get the image
on a screen; and it's a method of Graphics, so how am I supposed do
drawImage without doing a getGraphics()?
> > Researching farther - found Window described as "a top-level window with
> > no borders and no menubar....A window must have either a frame, dialog,
> > or another window defined as its owner when it's constructed."
> >
> > Window is called "top-level" but must have an owner - does that mean it
> > must be inside another component?
>
> No, it is supposed to have a "parent" which controls certain aspects of
> it. Window is not really meant to be used for actual programms, it is
> a superclass for the real windows like Frame and Dialog. But you can
> use it if you e.g. create an invisible Frame as its owner.
>
> > Is that the problem with Canvas?
>
> There is non, it is just not a top-level window, so it can not be
> displayed on the screen without beeing in a window, like a Frame or a
> Dialog.
>
> > So, it looks like I have to have a Frame with a title bar whether I want
> > it or not. Is there another way to do this?
>
> No, use a Window with an invisible Frame. Or use Java 1.4 with full
> screen mode. Or use Swing.
According to that web page, Swing's lightweight components need to be in
a heavyweight compontent anyway. So somewhere at the top there has to
be a Frame.
>
> /Thomas
Thanks for your help.
--
Tom A.
Anya: Sensible! You think it’s sensible for me to go down
into that pit of cotton-top hell, and let them hippity-hop
all over my vulnerable flesh? - Buffy the Vampire Slayer
You have not read the suggested article:
http://java.sun.com/products/jfc/tsc/articles/painting/index.html
You will get a Graphics object when you need it. Don't call us, we will
call you.
> According to that web page, Swing's lightweight components need to be in
> a heavyweight compontent anyway. So somewhere at the top there has to
> be a Frame.
A JFrame, not a Frame.
/Thomas