Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.

Similar presentations


Presentation on theme: "Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here."— Presentation transcript:

1 Introduction to Applets Chapter 21

2 Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here To View Cannon World Applet

3 Security Issues Applets are loaded from remote computer and executed locally. Since an “untrusted” stranger wrote the applet, applets are restricted. Applets CANNOT: –run any local executable program –read or write to local computer’s file system –Communicate with any computer except originating server –Access many facts about the local computer, except name and version of OS

4 Applets vs. Applications Much of the code for Applets is similar to Applications Differences: –Applets are created by subclassing Applet, instead of Frame –Applets begin execution with the method init, instead of main –Applets are halted and restarted as the Web browser moves to a new page and returns

5 Applet class Applet is a subclass of Panel, thus it inherits all of Panel’s graphical component attributes. Applets have four methods available for overriding by clients: –init() called only once when the applet is first loaded (use like the constructor) –start() called to begin execution each time the Web- page containing the applet is exposed –stop() called each time the Web-page containing the applet is hidden –Destroy() called when the applet is about to be terminated (should free any resource being used)

6 CannonWorld as an Applet import java.applet.*; import java.awt.*; import java.awt.event.*; public class CannonWorld extends Applet // was Frame { public static final int FrameWidth = 600; public static final int FrameHeight = 400; private int angle; private String message; private CannonBall cannonBall; private Scrollbar slider; // public CannonWorld() { public void init() { // changed the layout manager setLayout(new BorderLayout()); setSize ( FrameWidth, FrameHeight ); //setTitle( "Cannon Game" ); angle = 45; message = "Angle: " + angle; …

7 Http and Applets Cannon World Applet Demo CS II Web- page to Display an Applet

8 Parameters in tag <APPLET CODEBASE="http://www.cs.uni.edu" CODE="CannonWorldApplet" ALIGN=LEFT WIDTH=600 HEIGHT=400> You do not have a Java enabled browser In the Java Applet, getParameter(“name1”) can be used to access the parameter string.

9 Loading Resources from the Server getImage(URL) – retrieve the image specified by the URL (jpeg and gif) getAudioClip(URL) – return audioClip object, then play it (or play(URL) short cut) getCodeBase() returns URL for codebase appletContext.showDocument(URL) instructs the Web browser to display the a new page from the URL

10 URL objects Constructed from a string or previous URL and a string URL have an openStream() method which returns an inputStream object, so you can read from a URL just like reading from a file.


Download ppt "Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here."

Similar presentations


Ads by Google