Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applets The objectives of this chapter are: To describe applets and their purpose To discuss embedding applets in HTML pages.

Similar presentations


Presentation on theme: "Applets The objectives of this chapter are: To describe applets and their purpose To discuss embedding applets in HTML pages."— Presentation transcript:

1 Applets The objectives of this chapter are: To describe applets and their purpose To discuss embedding applets in HTML pages

2 An applet is a subclass of Panel It is a container which can hold GUI components It has a graphics context which can be used to draw images An applet embedded within an HTML page Applets are defined using the tag Its size and location are defined within the tag The browser contains a Java Virtual Machine which executes the applet The applet.class file is downloaded, through the net, into the Virtual machine. Unfortunately, most browsers have a very old version of the JVM The standard is either Java 1.1.4 or 1.1.5 Sun has released a Java 1.2 plugin which can be used instead What is an applet

3 Java applications are executed from the command line A Java VM must be installed The VM is given the name of a class which contains a main() method The main method instantiates the objects necessary to start the application Applets are executed by a browser The browser either contains a VM or loads the Java plugin The programmer must implement a class which is a subclass of applet There is no main method. Instead, the applet contains an init() method which is invoked by the Browser when the applet starts Applications and Applets

4 The HTML applet tag contains the following parameters: <Appletcode="name of.class file" codebase="URL where code is loaded from" name="applet identifier" align="LEFT|RIGHT|CENTER width="size in pixels" height="size in pixels“> HTML and Applets

5 Example HTML file Sample Applet This text will display if the browser does not support applets

6 Sample Applet import java.applet.*; import java.awt.*; public class Sample extends Applet implements ActionListener { private Button okButton = new Button("OK"); private Button cancelButton = new Button("Cancel"); public void init() { okButton.addActionListener(this); cancelButton.addActionListener(this); add(okButton); add(cancelButton); } public void actionPerformed(ActionEvent x) { //... }... }

7 Passing Parameters To Applets public class Menu extends Applet { public void init() { String menuName = getParameter("MenuName"); String theFont = getParameter("Font"); // do something with parameters }

8 Java applets execute within a Sandbox Applets cannot access the local file system Applets cannot connect to systems other than the server from which they were downloaded Applets cannot listen for inbound network connections Applets cannot spawn processes or load local jar files Applets cannot terminate the Java VM Applets cannot change the security policy Signed applets can be granted more access rights These rights are controlled by the SecurityManager Applet Security

9 Unfortunately, Applets are not heavily used Browsers support is limited Browsers often contain outdated or buggy Java virtual machines Each browser has its own compatibility issues. This usually means that the programmer has to implement workarounds for the various browsers Microsoft has not been very cooperative in terms of ensuring that IE correctly implements applets. Even the Java plugin has issues under IE Because the AWT and Swing are not highly regarded, use of Java in the client is minimal. Browser Issues


Download ppt "Applets The objectives of this chapter are: To describe applets and their purpose To discuss embedding applets in HTML pages."

Similar presentations


Ads by Google