You are on page 1of 5

IT1712

Applets
JApplet
• An applet is a program that is called within another application, usually a Web browser.
• A Java applet and a Java application are similar in several ways:
o You save both with a .java file extension.
o You compile both into bytecode using the javac command, and the bytecode is stored in a
file class with a .class file extension.
o Both can contain any number of methods you define and any number of variables and
constants. Both can contain decisions, loops, arrays, and other language elements.
o Both can contain GUI elements and event listeners.
• A Java applet is different from a Java application in several ways:
o Applets descend from the JApplet class.
o Applets run from another application. The java command is not used to run an applet.
o Applets do not contain a main() method.
o A default close operation is not set for a JApplet.
o Applets cannot delete, read, or create files on the user’s system.
o Applets cannot run any other program on the user’s system.
• You can create a JApplet using only the init() method.
• The init() method is the first method called in any applet. It is used to perform initialization tasks,
such as setting variables to initial values or placing applet components on the screen.
• Sample code for a JApplet:

• Applets are viewed using either one of these two:


o Web Browser (through any of the following)
▪ Type the complete path of the HTML document that you created in the address bar
of a Web browser.
▪ Double-click the HTML document if the program set to open it is a Web browser.
▪ Right-click the HTML document and open it with your chosen Web browser.
o Applet Viewer – a program that comes with JDK that provides a convenient environment for
testing applets
▪ Type the appletviewer command at the command line, followed by the full HTML
filename, including the file extension.
▪ Use the built-in applet viewer of your chosen Java IDE.
• To run a Java applet, you only need to create an HTML document.
• To run an applet from an HTML document, <object> and </object> tag pair is added. You can also
use <applet> and </applet> but using object is recommended because it includes new and future
media types.
• Sample HTML code to run a JApplet:

10 Handout 1 *Property of STI


Page 1 of 5
IT1712

JApplet Components
• Components that are typically placed in a JFrame can also be placed in a JApplet.
• Invoking the validate() method after adding one or more components to an applet ensures that
the components draw themselves on the screen.
• Sample code for a JApplet with various components:

Initial Output: After typing name and clicking the button:

10 Handout 1 *Property of STI


Page 2 of 5
IT1712

JApplet Life Cycle


• The init() method executes when a Web page containing a JApplet is loaded in the browser, or
when you use the Applet Viewer.
• The start() method executes after the init() method, and it executes again every time the applet
becomes active after it has been inactive.
• The paint() method is always called after the init() and start() methods execute. It is also called if
an applet needs repainting.
• The stop() method is invoked when a user leaves a Web page (by minimizing a window or visiting a
different Web page).
• The destroy() method is called when the user closes the browser or the Applet Viewer.

Images
• Multimedia describes the use of sound, images, graphics, and video in computer programs.
• Multimedia tools provided by Java:
o Java 2D or Java 3D Application Programming Interface (API) – for creating 3D graphics
applications
o Java Media Framework (JMF) API – for adding audio and video media to an application
o Java Sound – for playing, recording, and modifying audio files
o Java Advanced Imaging API – for image-manipulation
o Java Speech API – for enabling speech commands and producing speech output
• Image formats supported by Java:
o Portable Network Graphics (PNG)
o Joint Photographic Experts Group (JPEG)
o Graphics Interchange Format (GIF)
• The Image class loads images that have been stored in one of the allowed formats. It is included in
the java.awt package.
• Image objects are created using the getImage() method. It loads an Image into the named Image in
the applet.
• One version of the getImage() method can take up to two (2) arguments: a location where the
image is stored and its filename.
Sample statement:

• The getCodeBase() method returns the Uniform Resource Locator (URL) where the code is located.
• The paint() method can be used to display Image object images.
Sample statement:

• The drawImage() method is a Graphics method that uses these four (4) arguments:
o A reference to the Image object in which the image is stored
o The x coordinate where the image appears on the applet
o The y coordinate where the image appears on the applet
o The this reference to implement the ImageObserver object (This is usually the object on
which the image appears.)
10 Handout 1 *Property of STI
Page 3 of 5
IT1712

• An overloaded version of drawImage() can be used to output a scaled image with six (6) arguments:
o First: A reference to the Image object in which the image is stored
o Second and third: The x and y coordinates where the image appears on the applet
o Fourth and fifth: The width and height of the scaled object
o Sixth: The this reference to implement the ImageObserver object
Sample statement:

Sample program:

Output:

• The ImageIcon class is used to create images in applications and applets.


Example:

• The paintIcon() method can be used to display ImageIcon images with four (4) arguments:
o First: A reference to the Component on which the image appears
o Second: A reference to the Graphics object used to render the image
o Third and fourth: The x and y coordinates for the upper-left corner of the image
Example:

10 Handout 1 *Property of STI


Page 4 of 5
IT1712

Sound
• Sound formats supported by Java: Windows Wave File (WAV), Music and Instrument Digital Interface
(MIDI or MID), and Sun Microsystems Audio (AU)
• The play() method of the Applet class is used to retrieve and play sound as soon as possible once it
is called. It takes one of two (2) forms:
o With one (1) argument: A URL object that loads and plays an audio clip when both the URL
object and the audio clip are stored at the same URL
o With two (2) arguments: The first argument is a URL object and the second one is a folder path
name that loads and plays the audio file. The first argument is often a call to a
getCodeBase() method or getDocumentBase() method to retrieve the URL object; the
second one is the name of the audio clip within the folder path that is stored at that URL.
• Calling the getCodeBase() method in an applet, you get a URL object that represents the folder in
which the applet’s class file is stored.
• To start or stop the sound, or to play a sound more than once, you must load the sound into an
AudioClip object using the applet’s newAudioClip() method.
• The getAudioClip() takes one (1) or two (2) arguments:
o With one (1) argument: A URL that identifies the sound file
o With two (2) arguments: A URL that identifies the sound file and a folder path referenced
needed for locating the file
• You can use the play() method to call and play the sound, the stop() method to terminate the
playback, and the loop() method to repeatedly play the sound.
• Sample program that adds sound to a JApplet:

References:
Baesens, B., Backiel, A. & Broucke, S. (2015). Beginning java programming: The object-oriented approach. Indiana: John
Wiley & Sons, Inc.
Farrell, J. (2014). Java programming, 7th Edition. Boston: Course Technology, Cengage Learning
Savitch, W. (2014). Java: An introduction to problem solving and programming, 7th Edition. California: Pearson Education,
Inc.

10 Handout 1 *Property of STI


Page 5 of 5

You might also like