1 / 27

Java Programming

Java Programming. Applets Chapter 17. Applets.

amos
Download Presentation

Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming Applets Chapter 17

  2. Applets • 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. Applets • Java programs are divided into two main categories, applets and applications • An applicationis an ordinary Java program • An applet is a kind of Java program that can be run across the Internet

  4. Applets • 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. Security Restrictions • 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 wherethe applets are stored.

  6. The JApplet Class • 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. The JApplet Class • 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. The JApplet Class • 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

  9. The JApplet Class

  10. Writing Applets • Always extends theJAppletclass, which is a subclass of Applet for Swing components. • Overrideinit(), start(), stop(), anddestroy() if necessary. By default, these methods are empty. • Add your own methods and data if necessary.

  11. Applet Example 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); } }

  12. Applet Example

  13. Applets • 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 • 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. Icons • 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 ImageIconNameOfImageIcon = new ImageIcon("PictureFileName");

  16. Icons • 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: JLabelaLabel=new JLabel("Welcome to my applet."); ImageIconwaveIcon = new ImageIcon("hand_waving.gif"); aLabel.setIcon(waveIcon);

  17. Sound Files • Java can also play sound files within the Appletclass • 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 AudioClipaClip = new AudioClip(getCodeBase(), "audio/event.au");

  18. Running an Applet • 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 • 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. HTML • 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. HTML • Run applet from within HTML document <HTML> <object code = "AClass.class" width = 300 height = 200> </object> </HTML> • Three object tag attributes • code • Name of compiled applet • width • height

  22. Locating Resources • Due to security restrictions, applets cannot access local files. How can an applet load resource files for image and audio?

  23. Locating Resources • 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. URL • 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. Hyperlinks • 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 <a href="PathToDocument"> TextToClick </a> • The PathToDocumentcan 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. Inserting a Picture • A picture can also be inserted in an HTML document <img src="PathToPicture"> • The PathToPicturecan 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. HTML Code <p> Our Java class goals </p> <imgsrc = "smiley.gif"> <a href = "http://goals/"> Click here for list of goals </a>

More Related