Download presentation
Presentation is loading. Please wait.
Published byArthur Carr Modified over 9 years ago
1
Applets Chapter 17
2
Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web pages, and the results were dazzling. The infusion of Java into the Web was powerful, efficient, portable, and secure The trick was to create a part of a program, called an applet, and to display the applet inside a rectangle on the Web page
3
Java programs are divided into two main categories, applets and applications An application is an ordinary Java program An applet is a kind of Java program that can be run across the Internet
4
Applications are invoked from the static main method by the Java interpreter, and applets are run by the Web browser. The Web browser creates an instance of the applet using the applet’s no-arg constructor and controls and executes the applet through the init, start, stop, and destroy methods. Applets have security restrictions Web browser creates graphical environment for applets, GUI applications are placed in a frame.
5
Applets are not allowed to read from, or write to, the file system of the computer viewing the applets. Applets are not allowed to run any programs on the browser’s computer. Applets are not allowed to establish connections between the user’s computer and another computer except with the server where the applets are stored.
6
When the applet is loaded, the Web browser creates an instance of the applet by invoking the applet’s no-arg constructor. The browser uses the init, start, stop, and destroy methods to control the applet. By default, these methods do nothing. To perform specific functions, they need to be modified in the user's applet so that the browser can call your code properly
7
init() method Executes when Web page containing a JApplet loaded Or when running appletviewer command start() method Executes after init() method Executes again every time applet becomes active after it has been inactive
8
stop() method Invoked when user leaves Web page destroy() method Called when user closes browser or Applet Viewer Releases any resources JApplet might have allocated Every JApplet has the same life cycle outline
10
Always extends the JApplet class, which is a subclass of Applet for Swing components. Override init(), start(), stop(), and destroy() if necessary. By default, these methods are empty. Add your own methods and data if necessary.
11
import java.awt.*; import javax.swing.*; public class FirstApplet extends JApplet { public void init ( ) { getContentPane( ).setBackground(Color.ORANGE); setLayout(new BorderLayout( )); JLabel message = new JLabel("An applet a day keeps the doctor away. "); add(message, BorderLayout.CENTER); }
13
Some of the items included in a Swing GUI are not included in an applet Applets do not contain a main or setVisible method Applets are displayed automatically by a Web page or an applet viewer Applets do not have titles Therefore, they do not use the setTitle method They are normally embedded in an HTML document, and the HTML document can add any desired title
14
Applets do not use the setSize method The HTML document takes care of sizing the applet Applets do not have a close-window button Therefore, they do not have a setDefaultCloseOperation method When the HTML document containing the applet is closed, then the applet is automatically closed
15
An icon is a picture It is typically, but not always, a small picture An icon can be stored in a file of many different standard formats Such as. gif,. tiff, or. jpg The class ImageIcon is used to convert a picture file to a Swing icon Then it can be added as a component to any Container class, such as JApplet The class ImageIcon is in the javax.swing package ImageIcon NameOfImageIcon = new ImageIcon("PictureFileName");
16
The easiest way to display an icon in an applet is to place it in a JLabel The following three lines create a label, create an icon, and then add the icon to the label : JLabel aLabel=new JLabel("Welcome to my applet."); ImageIcon waveIcon = new ImageIcon("hand_waving.gif"); aLabel.setIcon(waveIcon);
17
Java can also play sound files within the Applet class play( ) method of the Applet class Simplest way to retrieve and play a sound Two forms Codebase attribute Indicates the filename of the applet’s main class file getCodeBase( ) method AudioClip aClip = new AudioClip(getCodeBase(), "audio/event.au");
18
An applet class is compiled in the same way as any other Java class However, an applet is run differently from other Java programs The normal way to run an applet is to embed it in an HTML document The applet is then run and viewed through a Web browser
19
HTML is used to be able to run an applet from a web site HTML stands for Hypertext Markup Language Hypertext is text viewed on a browser that contains clickable entries called links or hyperlinks When a link or hyperlink is clicked, the document specified by the link is displayed
20
When you create an applet Write applet in Java Save with. java file extension Compile applet into bytecode using javac command Write HTML document Includes statement to call compiled Java class Load HTML document into a Web browser Or run Applet Viewer program
21
Run applet from within HTML document Three object tag attributes code Name of compiled applet width height
22
Due to security restrictions, applets cannot access local files. How can an applet load resource files for image and audio?
23
The java.net.URL class can be used to identify files (image, audio, text, etc.) on the Internet. In general, a URL (Uniform Resource Locator) is a pointer to a “resource” on the World Wide Web on a local machine or a remote host. A resource can be something as simple as a file or a directory.
24
A URL is the name of an HTML document on the Web URL is an acronym for Uniform Resource Locator URLs often begin with http This is the name of the protocol used to transfer and interpret the HTML document Most browsers will fill in http:// if it is omitted
25
Text can be marked as a hyperlink so that if a user clicks that text, the browser goes to another Web page specified by the link TextToClick The PathToDocument can be a full or relative path name to an HTML file, or a URL to any place on the Web The TextToClick will be displayed and underlined by the browser
26
A picture can also be inserted in an HTML document The PathToPicture can be a full or relative path name to a file with a digitally encoded picture Most commonly used picture-encoding formats are accepted, such as.gif,.tiff, and.jpg
27
Our Java class goals Click here for list of goals
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.