+ All Categories
Home > Documents > Applets Chapter 17. Java’s big splash onto the scene came in the mid 90’s. The people at Sun...

Applets Chapter 17. Java’s big splash onto the scene came in the mid 90’s. The people at Sun...

Date post: 29-Dec-2015
Category:
Upload: arthur-carr
View: 214 times
Download: 0 times
Share this document with a friend
Popular Tags:
27
Java Programming Applets Chapter 17
Transcript
Page 1: Applets Chapter 17.  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.

Java ProgrammingApplets

Chapter 17

Page 2: Applets Chapter 17.  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.

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

Page 3: Applets Chapter 17.  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.

Applets

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

Page 4: Applets Chapter 17.  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.

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.

Applets

Page 5: Applets Chapter 17.  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.

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.

Security Restrictions

Page 6: Applets Chapter 17.  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.

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

The JApplet Class

Page 7: Applets Chapter 17.  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.

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

Page 8: Applets Chapter 17.  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.

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

The JApplet Class

Page 9: Applets Chapter 17.  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.

The JApplet Class

Page 10: Applets Chapter 17.  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.

Writing Applets

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.

Page 11: Applets Chapter 17.  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.

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); }

}

Page 12: Applets Chapter 17.  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.

Applet Example

Page 13: Applets Chapter 17.  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.

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

Page 14: Applets Chapter 17.  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.

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

Page 15: Applets Chapter 17.  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.

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 packageImageIcon NameOfImageIcon = new

ImageIcon("PictureFileName");

Page 16: Applets Chapter 17.  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.

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:

JLabel aLabel=new JLabel("Welcome to my applet.");ImageIcon waveIcon = new ImageIcon("hand_waving.gif");aLabel.setIcon(waveIcon);

Page 17: Applets Chapter 17.  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.

Sound Files

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");

Page 18: Applets Chapter 17.  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.

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

Page 19: Applets Chapter 17.  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.

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

Page 20: Applets Chapter 17.  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.

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

Page 21: Applets Chapter 17.  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.

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

Page 22: Applets Chapter 17.  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.

Locating Resources

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

Page 23: Applets Chapter 17.  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.

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.

Page 24: Applets Chapter 17.  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.

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

Page 25: Applets Chapter 17.  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.

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 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

Page 26: Applets Chapter 17.  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.

Inserting a Picture

A picture can also be inserted in an HTML document <img src="PathToPicture">

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

Page 27: Applets Chapter 17.  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.

HTML Code

<p>Our Java class goals</p><img src = "smiley.gif"><a href = "http://goals/">Click here for list of goals</a>


Recommended