Title: Title Slid
1Title Slid
CSS 444
Java Programming Applets
By Ralph B. Bisland, Jr.
2Applets
- An applet is a Java program designed to be run by
a Java-enabled Web browser (i.e., it has the JVM
embedded in the browser). - A call to the applet is embedded in the HTML
script file. - When a Web page is loaded that contains a
reference to an applet, the browser downloads the
applet from the Web server and executed the
applet on the client machine.
3Restrictions To Applets
- Applets are considered untrusted code.
- Things applets can not do
- Access the local file system on your computer
- Perform networking operations
- Use system facilities
- Use certain AWT facilities
- Access certain system properties
- Access certain classes and packages
- Create threads or access threads or thread groups
outside of the thread group in which the applet
is running.
4Accessing An Applet
- Applets are accessed through the HTML ltAPPLETgt
tag. - Minimum Syntax ltAPPLET codeapplet-file-name
widthpixel-width lengthpixel-length
gt lt/APPLETgt - The applet file must be the class file and it
must have the .class extension. - Length and width define the initial window size
for the applet output.
5Accessing An Applet
- The CODE command assumes that the applet .class
file is in the same subdirectory as the HTML
file. - If the .class files are located somewhere else
you must use the CODEBASE option to the applet to
direct the JVM to the .class files. - The format of the CODEBASE option is
CODEBASEdirectory-name. - This option is inserted into the APPLET tag.
6Example1
ltHTMLgt ltTITLEgtA Sample Appletlt/TITLEgt ltBODYgt ltAPPL
ET codeHelloWorld.class HEIGHT300
WIDTH300gt lt/APPLETgt lt/BODYgt lt/HTMLgt
7Example2
ltHTMLgt ltTITLEgtA Sample Appletlt/TITLEgt ltBODYgt ltAPPL
ET codeHelloWorld.class codebaseMyLibrary
HEIGHT300 WIDTH300gt lt/APPLETgt lt/BODYgt lt/HTMLgt
8JARs And Applets
- The use of JAR files can significantly reduce the
download time for applets (particularly if image
files or sound files are involved.) - To refer to code, images, sounds, etc.
encapsulated in a jar file, use the archives
parameter in the APPLET tag. - The valus of the archives parameter must be
enclosed in quotes. - It may contain several directories, separated by
commas.
9JAR Applet Example
ltHTMLgt ltTITLEgtA Sample Appletlt/TITLEgt ltBODYgt ltAPPL
ET codeHelloWorld.class codebaseMyLibrary
archivesclass1.jar, extras/class2.jar
HEIGHT300 WIDTH300gt lt/APPLETgt lt/BODYgt lt/HTMLgt
10More Applet Info
- To create an applet, you must create a class that
extends the applet class. - The applet class is located in the java.applet
package. - The applet class extends the Panel class.
11Applet I/O
- Since Java has restrictions to reading and
writing text data, applet I/O usually involves
some type GUI interface. - You may use Abstract Windowing Toolkit (AWT) or
Swing (JFC Swing) to utilize a GUI interface to
applets. - The only exception to this is the use of the
paint method to perform basic output.
12The Applet Class
public class Applet extends Panel //
Constructor public Applet () // Methods
public void destroy() public AppletContext
getAppletContext() public String
getAppletInfo() public AudioClip getAudioClip
(URL url) public AudioClip getAudioClip (URL
url, String name) public URL getCodeBase()
public URL getDocumentBase() public Image
getImage (URL url) public Image getImage (URL
url, String name) public Locale getLocale()
13The Applet Class (ctd)
public String getParameter(String name) public
String getParameterInfo() public void
init() public boolean isActive() public void
play(URL url) public void play (URL url, String
name) public void resize (Dimension d) public
void resize (int width, int height) public
final void setStub(AppletStub stub) public void
showStatus(String msg) public void start()
public void stop()
14Creating An Applet
- Applets can not have a main method.
- To create an applet, you must create a subclass
of Applet and override some or all of the
following methods, - init()
- start()
- stop()
- destroy()
- paint() - from java.awt.Component
15Basic Applet Methods
- init() Called after the constructor is called,
when the applet first starts. - start() Called when the browser opens the
applets window. - stop() Called when the browser changes to a new
HTML page making the applet temporarily hidden. - destroy() Called when the applet exits, and
reverses any action taken by init() freeing any
resources the applet is holding.
16Basic Applet Methods
- paint() Comes from java.awt.Component. Used to
draw the applet on the screen. - Any or all of these methods may be overridden.
- You do not have to explicitly call these methods,
they are automatically called for you. - The applet class inherits from the class Panel
which inherits from the Container class which
inherits from the Component class which inherits
from the Object class.
17A Basic Applet
import java.awt. import java.applet. public
class HelloWorld extends Applet public void
paint (Graphics g) g.drawString(Hello
World, 250, 150) To compile the applet
use the javac call from the JDK. javac
HelloWorld.java
18Applet Notes
- The drawString method was overridden.
- The applet output began 150 pixels from the top
of the applet window. - The applet output began 250 pixels from the left
side of the applet window. - When compiled, the applet bytecode was stored in
a file called HelloWorld.class. - The method has a parameter of type Graphics.
19Another Applet
import java.awt. import java.applet. public
class HelloWorld2 extends Applet public void
init() setBackground(Color.yellow)
public void paint (Graphics g) Font font
new Font (Serif, Font.BOLD, 72)
g.setFont(font) g.setColor(Color.blue)
g.drawString(Hello World, 0, 150)
20Testing Applets
- The JDK comes with a program to test our applets.
- The program is called appletviewer.
- To run appletviewer from a UNIX-based computer,
X-term must be installed. - To run appletviewer, enter the command
appletviewer followed by the HTML file containing
the reference to the Java applet. - Example appletviewer first.html
21A Shortcut
Place the HTML code in comments, then submit the
file to the appletviewer as appletviewer
java_plus_html.java / ltHTMLgt ltTITLEgtA Sample
Appletlt/TITLEgt ltBODYgt ltAPPLET codejava_plus_html.
class HEIGTH50 WIDTH180gt lt/APPLETgt lt/BODYgt lt/HTM
Lgt / import java.awt. import
java.applet. public class java_plus_html
extends Applet public void paint (Graphics
g) g.drawString("Lassez les bon temps
rouler", 0, 30)
22Passing Parameters To Applets
- Same concept as passing arguments to an
application via the command line. - Syntax ltAPPLET Code ...gt ltPARAMETER
NAMEparm-name1 VALUE value1gt ltPARAMETER
NAMEparm-name2 VALUE value2gt . .
. lt/APPLETgt - The parameter value is always treated as a string
and must be enclosed in double quotes.
23Example
ltHTMLgt ltBODYgt ltAPPLET codefirst.class width900
height300gt ltPARAM NAMEsize VALUE72gt ltPARAM
NAMEfont VALUESerifgt ltPARAM NAMEcolor
VALUEyellowgt ltPARAM NAMEbackground
VALUEbluegt ltPARAM NAMEmessage VALUEGuardez
des loogt lt/APPLETgt lt/BODYgt lt/HTMLgt
24Processing Parameters
- To get the value of a parameter use the
getParameter method. - Example of getting the font point size int
sizeParameter Integer.parseInt
(getParameter(size) - The getParameter method always returns the value
as a string. - Example of getting the font type. String
fontParameter getParameter (font) - You may define your own method in an applet.
25Example
// Applet to display a message in a window
the // font style, font size, background, and //
foreground colors and the message text are //
input as parameters to the applet import
java.awt import java.applet. public class
Demo_Parms extends Applet int sizeParameter
String fontParameter String colorParameter
String backgroundParameter String
messageParameter
26Example (ctd)
private Color convertColorString (String color)
if (color.equals(red)) return
Color.red else if (color.equals(orange))
return Color.orange else if (color.equals(yell
ow)) return Color.yellow else if
(color.equals(green)) return Color.green
else if (color.equals(blue)) return
Color.blue else if (color.equals(cyan))
return Color.cyan else if (color.equals(magent
a)) return Color.magenta else
return Color.black public void
init sizeParameter new Integer(getParameter(
size)).intValue() fontParameter
getParameter(font) colorParameter
getParameter(color) backgroundParameter
getParameter (background) messageParameter
getParameter (message)
27Example (ctd)
public void paint (Graphics g) Font font
new Font (fontParameter, font.BOLD,
sizeParameter) g.setFont(font)
g.setColor(convertColorString(colorParameter))
setBackground(convertColorString(backgroundParame
ter)) g.drawString (messageParameter, 75,
150)
28Connecting To A Website
- The java.net package provides a powerful set of
features that allow applets and applications to
connect to and communicate with other web sites
on the Internet. - The class URL allows the data referred to by the
URL to be downloaded. - One of the constructors from the URL class has
the signature public URL (String spec)
throws MalformedURLException
29Connecting To A Website (ctd)
- To access the homepage for the appropriate Web
site use the showDocument() method from the
abstract interface AppletContext. - The getAppletContext() method (from the class
Applet) returns an AppletContext object that is
used to invoke the showDocument() method.
30Example
import java.awt. import java.applet. import
java.net. public class connect_URL extends
applet public void getIt String newURL
http//www.cs.usm.edu/bisland
URL site new URL (newURL)
getAppletContext().showDocument(site)
31Multimedia Applets
- Java applets have the capability of displaying
images, playing movie file and playing sound
bytes. - In order to perform these multimedia operations
through applets, your computer must have the
capability of implementing them.
32URLs
- Two methods are used to connect to the URLs where
the multimedia files are located - getDocumentBase() Returns a URL object that
describes the current browser page. - getCodeBase() Returns a URL object that
describes the source of the applet itself. Often
this is the current page.
33Sound
- Java 1.2 supports the following sound formats
- AU Audio file
- AIFF Apple Interface File Format
- WAVE Windows Audio Virtual Environment
- MIDI Musical Instrument Digital Interface
- Playing sounds is as simple as loading a sound
file and calling the methods to play it. - Sounds may be played in either applets or
applications.
34Sound (ctd)
- To load an audio clip use the getAudioClip
method - Format getAudioClip (URL Base, String
target) - Fetches a sound from the file named in the target
located at the URL specified by the base. The
returned value is an instance of the class
AudioClip.
35Sound (ctd)
- There are three methods used with audio clips
- play()Plays the audio clip one time.
- stop() Stops the audio clip.
- loop() Plays the audio clip continuously until
the stop method is executed.
36Example
import java.applet.Applet import
java.awt.Graphics public class PlayIt extends
Applet public void paint (Graphics g)
g.drawString (Theyre playing my song,
25, 25) play (getDocumentBase(),
mysong.au)
37Loading Audio Clips
- You can treat audio clips like images. You can
load them and play them later. - To load an audio clip use the getAudioClip
method Audioclip sound soundgetAudioC
lip(getDocumentBase(), mysong.au) - Once the audio clip is loaded use one of the
three methods play, stop, or loop to do whatever
you wish with the clip.
38Example
import java.awt.Graphics import
java.applet. public class Music extends applet
AudioClip sound public void init ()
soundgetAudioclip(getdocumentBase(),
mysong.au) public void paint (Graphics g)
g.drawString(Audio Test, 25, 25) public
void start () sound.start() public void
stop () sound,stop()
39Images
- To display an image two things must be done
- 1. Inform the program of the name of the graphics
file and to allocate space in memory for the
image. This is done via the getImage method. - 2. Load the image from the file into memory and
display the image on the screen. This is done via
the drawImage method.
40The getImage Method
- The call to getImage is getImage (URL base,
String target) - Where URL base, is either getDocumentbase() or
getCodeBase() and target is the name of the image
file.
41The drawImage Method
- The call to drawImage is drawImage(Image,
x-coord, y-coord, image-observer) - Where
- Image is the image object to be drawn.
- X-coord is the x-coordinate of the image.
- Y-coord is the y-coordinate of the image.
- Image-observer is a class that is notified if the
image changes. (Probably this class)
42Example
import java.awt. import java.applet.Applet publ
ic class Picture extends Applet Image
image public void init () image
getImage (getDocumentBase(),
mypicture.gif) public void paint
(Graphics g) g.drawImage (image, 20, 20,
100, 100, this)