Just a few picky points "for the record"...
| import java.applet.AudioClip;
|
| public class Class_Sound extends JApplet{
|
| AudioClip clip;
|
| public Class_Sound(){
| String filename=getParameter("bouchon-champagne.wav");
| clip=getAudioClip(getDocumentBase(),filename);
|
|
| }
[...]
| I get an error with getParameter. It seems that Java can't find the file.
/* wrong! use the 'init()' method for applets, they
do not have a 'constructor' */
[...]
No you don't! You never _call_ getParameter,
so it cannot be Java having a problem with it!
The truth is halfway in the middle. Yes, applets do have constructors.
The base class Applet has a single constructor that takes no arguments
(see the API documentation), and subclasses of Applet must have a
constructor that takes no arguments as well. (Though an Applet subclass
is welcome to provide additional constructors, they will not be used by
a normal applet container.)
This is very important at a VM level because certain kinds of simple
initialization code (for example, inline field initializaters) are moved
to the constructor by the compiler. It's less important at the language
level, but it is worth understanding that applets follow the same basic
lifecycle as any other Java object, and aren't really "magical" in any
way. There are some kinds of initialization that can be done in the
applet's constructor, and some kinds that cannot. (And incidentally,
the same restrictions that apply to code in the constructor also apply
to inline field initializers.)
The problem is that the setup of an applet by the web browser is a two-
stage process. First, the applet object itself is created, and then the
applet is linked to the web browser (via a bridge object that implements
the AppletStub interface). Since the constructor is run as the applet
object is being created, any code that is written in the applet's
constructor may not interact with the web browser in any way. So, it's
fine to use the constructor to initialize an array of the first 200
fibinacci (sp?) numbers, but it's not fine to use the constructor to get
parameters from the applet's PARAM tags.
So in the end, yes Michael is calling getParameter. Furthermore, he is
calling getParameter before it has the information it needs to complete
the task. That is most likely causing an exception to be thrown (though
the API docs don't really specify what happens in this case).
Of course, there are other problems as well. The code most likely
demonstrates a confusion about what the getParameter method does; and
declares two methods called Play() and Stop() that are never called and
that grossly violate esteblished and near-universal Java identifier
naming conventions. Incidentally, Stop() is a very close misspelling of
the actual lifecycle method called stop(), and I wonder if these two
methods are intended to be start() and stop() instead of the current
Play() and Stop().
Debugging is another matter again,
but your example shows you have not
put 'println' calls everywhere, the first step.
(Just a side-comment; I agree that println is a great means of
demystifying what is happening when the behavior of code gets a little
strange. However, I certainly prefer that those statements are removed
before posting... unless, of course, they are part of demonstrating the
problem itself.)
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation