Re: How do I put pictures in my Frame?
Flo wrote:
Hey Guys,
very basic question from a beginner..
A good group for beginners is comp.lang.java.help.
I need to know how to include a picture in my frame. This script
(http://www.dpunkt.de/java/Programmieren_mit_Java/Applets_und_Applikationen/10.html
/ just look at the script, you don't need to understand what it says)
What if it says 'any student found to be seeking
external help will be charged with academic
misconduct'. ;-)
..works perfectly for an Applet, ..
It is extremely unfortunate that most examples of images
and audio clips rely on those idiotic* applet methods.
(* Idiotic in the sense that, they were not really much more
appropriate to 'tag on' to the Applet class, than the
Frame or Window class - if one, it should have been all)
...but when I try to use the methods for my
Frame, the compiler always says something like "Cannot Resolve Symbol"
and it points to the g in "getCodeBase(), "
That means the class you are using, does not have
any such 'getCodeBase()' method. Check things like
that in the JavaDocs*, for best results.
What can I do to use pictures in Frames?
Look first to.. (* e.g. of JavaDocs)
<http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Toolkit.html#getImage(java.lang.String)>
, then the more useful form that accepts an URL, and their
<http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Toolkit.html#createImage(byte[])>
...variants.
For the replacement to getCodeBase(), you can either..
1) put the image on the project classpath, and call
Object.getClass().getResource("theImageName")
2) Get a File object pointing to the image and call File.toURL()
...to get an URL.
Note that it is best to have your own copy of the JavaDocs
to browse locally - they can be downloaded.
HTH
Andrew T.