Applets: Using The Status Window
Applets: Using The Status Window
• The applet tag can be used to start an applet from both an HTML document and from
an applet viewer. An applet viewer will execute each applet tag that it finds in a
separate window, while web browser will allow many applets on a single page.
• The syntax for a full form of the applet tag is shown below:
• <Applet
• [codebase = codebaseURL]
• Code = appletfile
• [Alt = alternate text]
• [Name = apllet instance name]
• Width = pixels Height = pixels
• [Align = alignment]
• [vspace = pixels] [Hspace = pixels]
• >
• [<param name = AttributeName value = AttributeValue>]
• [<param name = AttributeName value = AttributeValue>]
• ….
• [HTML displayed in the absence of java]
• </applet>
Passing Parameters To
Applets:
• getDocumentBase() and getCodeBase():
• You will create applets that will need to explicitly load
media and text.
• Java will allow the applet to load data from the directory
holding the HTML file that started the applet (the
document base) and the directory from which the
applet’s class file was loaded (the code base).
• These directories are returned as URL objects by
getDocumentBase() and getCodebase(). They can be
concatenated with a string that names the file you want
to load.
• The following applet illustrates these methods :
• // display code and document bases :
• Import java.awt.*;
• Import java.applet.*;
• Import java.net.*;
• /*
• <applet code = “Bases” width = 300 height = 50>
• </applet>
• */
• Public class bases extends applet
• {
• // display code and document bases
• Public void paint(Graphics g)
• {
• String msg;
• URL url = getCodeBase(); // get code base
• Msg = “Code Base” + url.toString()
• g.drawString(msg,10,20);
• url = getDocumentBase(); //get doc base
• msg = “Document Base:” + url.toString();
• g.showString(msg,10,40);
• }
• }
AppletContext and
ShowDocument()-
• AppletContext is an interface that lets you get
information from the applet’s execution environment.
The context of the currently executing applet is
obtained by a call to the getAppletContext() method
defined by applet.
• There are two showDocument() methods. The
method shoDocument(URL) displays the document
at the specified URL. The method
showDocument(URL,string) displays the specified
document at the specified location within the browser
window
• /* using an applet context getCodebase() and showDocument() to display an HTML file */
• Import java.awt.*;
• Import java.applet.*;
• Import java.net.*;
• /*
• <applet code = “ACDemo” width = 300 height = 50>
• </applet>
• */
• Public class ACDemo extends Applet
• {
• Public void start()
• {
• appletContext ac = getAppletContext();
• URL url = getCodeBase(); // get url of this applet
• Try
• {
• Ac.showDocument(new URL (url + “test.html”));
• }
• Catch(malformedURLException e)
• {
• showStatus(“URL not found”);
• }
• }
• }
The Audiochip Interface:
– The audiochip interface defines these
methods.
• Play() – play a clip from the beginning
• Stop() – stop playing the clip
• Loop() – play the loop continuously
• After you have loaded an audio clip using
getAudioClip() you can use these methods
to play it.
Output to the console:
– Output to an applet’s window must be accomplished through
GUI-based methods such as drawstring() , it is still possible to
use console output in your applet- is still possible to use console
output in your applet – especially for debugging purpose.
• In an applet , when you call a method such as
system.out.printly() the output is not sent to your applet’s
window.
• Instead it appears either in the console session in which
you launched the applet viewer or in the java console
that is available in some browsers.
• Use of console output for purposes other then debugging
is discouraged, since it violates the design principles of
the graphical interface most users will expect.