Documente Academic
Documente Profesional
Documente Cultură
Ralph Morelli
Trinity College
Hartford, CT
published by Prentice
Hall
Java, Java, Java
Object Oriented Problem Solving
Chapter 4 Applets:
Programming for the
World Wide Web
Objectives
• Be able to design and implement a Java
applet.
• Understand Java's event handling model.
• Be able to handle button clicks in your
programs.
• Have a better appreciation of inheritance and
polymorphism.
• Know how to design a simple Graphical
User Interface (GUI).
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Outline
• Introduction
• The java.applet.Applet Class
• Class Inheritance
• Applet Subclasses
• A Simple Applet
• Event-Driven Programming
• Case Study: The CyberPetApplet
• Object-Oriented Design: Inheritance and
Polymorphism
• From the Java Library: Image
• In the Laboratory: CyberPetApplet
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Introduction
• An applet is a Java program embedded
within a Web page and run by a Web
browser.
• An applet has a graphical user interface
(GUI ).
• Java applet programming is event-driven
programming.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
The java.applet.Applet Class
Applet’s public methods help define an
interface between the applet and the browser .
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Objects, Assignments, and Types
• An instance of a class can be validly
assigned to variables whose type is any of
its superclasses.
Panel p = new Panel();
Container cont = p; // Valid: A Panel is a Container
Component comp = p; // Valid: A Panel is a Component
Object o = p; // Valid: A Panel is an Object
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
The Java Class Hierarchy
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Inheritance: Specializing a Class
• Inheritance allows us to specialize a class.
Subclasses inherit
getCreditCardNumber()
and add their own
special methods.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Specialization: A Square is a Rectangle
• A Square is a Rectangle whose length equals width.
To construct a Square,
you specify its side.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
A Square is an Extension of a Rectangle
public class Rectangle The Square() constructor
{
protected double length; calls the Rectangle()
protected double width; constructor, passing it side
public Rectangle (double l, double w)
as a value for both length
{ and width.
length = l;
width = w; public class Square extends Rectangle
} // Rectangle() {
public Square (double side)
public double calculateArea() {
{ //Call the superconstructor
return length * width; super(side, side);
} // calculateArea() }
} // Rectangle } // Square
Output
The inherited calculateArea() Produced
method can be used just as if
it were defined in Square.
square’s area is 100.0
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
An Applet Example
• SimpleApplet contains a single button.
• When the button is clicked, its label is
toggled from “The machine is off” to the
“The machine is on”
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Applets
• Java applets are made by first creating a
subclass of java.applet.Applet.
• And then instantiating the subclass.
public class SimpleApplet extends Applet { ... } // Define subclass
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
A Simple Applet
• SimpleApplet inherits from Applet and
implements the ActionListener interface.
These methods are inherited
by SimpleApplet
An interface is an abstract
class with no variables.
Implement: This
actionPerformed() is
implemented according to the
definition inherited from
ActionListener interface
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Interacting Objects
• A Web browser creates a SimpleApplet named
applet1 and tells it to initialize itself. The applet
creates a Button named toggle and adds it to itself.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Overriding the Applet.init() Method
• The init() method initializes the applet. It is
where applet execution begins. It may be
overridden in the applet subclass.
public void init () Create a new Button.
{
toggle = new Button ("The machine is off");
toggle.addActionListener(this);
add(toggle);
} // init() This applet will listen
for button clicks.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Polymorphism and Extensibility
• Polymorphism = many (poly) forms (morph)
• A polymorphic method -- init() -- has
different behavior for different objects.
• Overriding Applet.init() creates a method
that performs appropriate initializations for
your applet.
• Extensibility: Applet functionality (init())
defined in the superclass (Applet) is
extended to the subclass (SimpleApplet).
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
The Polymorphic eat() Method
• Polymorphism: The eat() method has different
behavior for CyberDogs and CyberCats.
CyberPet p1 = new CyberDog();
p1.eat(); // Eats dog food
p1 = new CyberCat();
p1.eat(); // Eats cat food
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
The Polymorphic init() Method
• Polymorphism: The init() method has different
behavior for AppletA and AppletB.
public class AppletA extends Applet {
public void init() {
System.out.println(“AppletA”);
}
}
public class AppletB extends Applet {
public void init() {
System.out.println(“AppletB”);
}
}
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Event-Driven Programming
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
The Java Event Hierarchy
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Creating an ActionListener
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Handling Action Events
1. Applet subclass implements the
ActionListener interface
import java.applet.*;
import java.awt.*;
import java.awt.event.*; 2. Declare a Button instance.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Responding to a Click Event
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Running an (Embedded) Applet
• A summary of what happens from the browser’s (or
appletviewer’s) perspective.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Case Study: The CyberPetApplet
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
CyberPetApplet’s GUI Interface
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
CyberPetApplet: Design Specification
Uses
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Defining CyberPetApplet
Step 1. Define a subclass of
java.applet.Applet that implements the
ActionListener interface.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Defining CyberPetApplet (cont.)
Step 2. Declare the instance variables.
public class CyberPetApplet extends Applet implements ActionListener {
// Declare instance variables.
private CyberPet pet1; // The CyberPet
private Label nameLabel; // Label
private TextField stateField; // TextField
private Button eatButton, sleepButton; // Buttons
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Defining CyberPetApplet (cont.)
Step 3. Define the init() method.
public void init()
{ // Instantiate the instance variables
pet1 = new CyberPet("Socrates"); // The CyberPet
nameLabel = new Label("Hi! My name is " + pet1.getName() +
" and currently I am : ");
stateField = new TextField(12); // TextField
eatButton = new Button("Eat!"); // Buttons
eatButton.addActionListener(this); // Assign the listeners
sleepButton = new Button("Sleep!");
sleepButton.addActionListener(this);
stateField.setEditable(false);
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Default Layout: FlowLayout
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Interacting Objects
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Defining CyberPetApplet (cont.)
Step 4. Implement the
actionPerformed() method.
public class CyberPetApplet extends Applet implements ActionListener {
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
The CyberPetApplet Program
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Object-Oriented Design: ToggleButton
• A ToggleButton isa Button that toggles its own
label.
Whenever it is clicked,
it toggles between
label1 and label2.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Design Spec: The ToggleButton Class
• Design Specification
– Class Name: ToggleButton
– Role: To be a button that toggles between two
labels
– Information (instance variables)
• Label1: Primary label (initial value of label)
• Label2: Secondary label
– Actions (public methods)
• ToggleButton: constructor initializes the two
labels
• actionPerformed(): Toggles between the two
labels
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Class Definition: ToggleButton
import java.awt.*;
import java.awt.event.*; Inheritance: A ToggleButton is a Button
public class ToggleButton extends Button implements ActionListener {
private String label1; // Two Labels to toggle between
private String label2;
Invoke Button’s constructor to set initial label
public ToggleButton (String l1, String l2) { // Constructor method
super(l1); // Use l1 as the default label
label1 = l1;
label2 = l2; A ToggleButton acts as its own ActionListener
addActionListener(this);
}
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Using a ToggleButton
import java.applet.*;
import java.awt.*; This applet uses a ToggleButton.
import java.awt.event.*;
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Interacting Objects
• The ToggleButton has two ActionListeners,
itself and the applet.
The JVM calls
both
ActionListeners
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Java Library: Using Images in an Applet
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Using the java.awt.Image Class
Declare a reference
variable for each image.
import java.applet.*;
import java.awt.*;
Applet.getImage() loads the
public class Lights extends Applet images from GIF files.
{
private Image lightOn, lightOff; // Declare two Image variables
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
In the Laboratory: CyberPetApplet
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
In the Laboratory: CyberPetApplet
Problem Statement
Extend the CyberPet and CyberPetApplet by
adding a third state to the pet simulation -- for
example, thinking. The applet GUI should
continue to display the CyberPet’s name and
state. An image can be used to depict the state.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Technical Terms
• abstract class
• abstract interface
• abstract method
• applet
• Application Programming Interface (API)
• event-driven programming
• Graphical User Interface (GUI)
• import declaration
• inheritance
• inheritance hierarchy
• interface
• polymorphic method
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Summary Of Important Points
• An applet is an embedded program that runs
within the context of a Web browser.
• The Java Application Programming Interface
(API) is a set of predefined classes that can be
used to write programs.
• A Graphical User Interface (GUI) enables the
user to interact with a program via graphical
elements such as windows, button, menus and so
on. Java's GUI components are defined in the
Abstract Windowing Toolkit (AWT) package.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Summary of Important Points (cont)
• The import statement is used to import definitions
of predefined classes into a Java program.
• The extend keyword is used to define a class's
pedigree -- its place in the Java class hierarchy.
• A class that extends another class is said to be a
subclass of that class which is its superclass.
• A subclass inherits the public and protected
methods and fields (variables) of its superclasses.
• Methods defined in a class’s superclasses can be
overridden in the subclass by defining a method
with the same signature -- same name, return type,
and same number and type of parameters.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Summary of Important Points (cont)
• A method that behaves differently for different
objects is said to be polymorphic.
• The Applet.init() method is an example, since it is
redefined in all of Applet subclasses.
• Applets are event-driven: they react to certain
events.
• An Event object records specific information about
a particular event.
• Clicking on a Button in an applet generates an
ACTION_EVENT which should be handled by an
actionPerformed() method.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets
Summary of Important Points (cont)
• A Label is a GUI component that displays a single
line of uneditable text on the applet. A Button is a
labeled GUI component that is used to trigger
some kind of action when clicked. A TextField is
a GUI component that displays a single line of
editable text. The setText() and getText() methods
can be used to set a Button's label or a TextField's
text.
• The default layout pattern for Java applets is the
FlowLayout.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 4: Applets