Professional Documents
Culture Documents
3.1 Introduction
• Applet
– Program that runs in
• appletviewer (test utility for applets)
• Web browser (IE, Communicator)
– Executes when HTML (Hypertext Markup Language)
document containing applet is opened and downloaded
– Applications run in command windows
• Notes
– Mimic several features of Chapter 2 to reinforce them
– Focus on fundamental programming concepts first
• Explanations will come later
3.2.1 The TicTacToe Applet
• Running applets
– In command prompt, change to demo subdirectory of applet
cd c:\jdk1.3\demo\applets
cd appletDirectoryName
– There will be an HTML file used to execute applet
– Type appletviewer example1.html
• appletviewer loads the html file specified as its
command-line argument
• From the HTML file, determines which applet to load (more
section 3.3)
– Applet will run, Reload and Quit commands under
Applet menu
3.2.1 The TicTacToe Applet
• You start as player "X"
3.2.2 The DrawTest Applet
3.2.3 The Java2D Applet
• Demonstrates 2D drawing capabilities built into Java2
Program Output
– Comments
• Name of source code and description of applet
5 import java.awt.Graphics; // import class Graphics
8 import javax.swing.JApplet; // import class JApplet
– Method paint
• Lines 13-21 are the definition of paint
• Draws graphics on screen
• void indicates paint returns nothing when finishes task
• Parenthesis define parameter list - where methods receive data
to perform tasks
– Normally, data passed by programmer, as in
JOptionPane.showMessageDialog
• paint gets parameters automatically
– Graphics object used by paint
• Mimic paint's first line
– Body of paint
• Method drawString (of class Graphics)
• Called using Graphics object g and dot operator (.)
• Method name, then parenthesis with arguments
– First argument: String to draw
– Second: x coordinate (in pixels) location
– Third: y coordinate (in pixels) location
– Java coordinate system
• Measured in pixels (picture elements)
• Upper left is (0,0)
Program Output
Program Output
– Method init
• Normally initializes instance variables and applet class
• Guaranteed to be first method called in applet
• First line must always appear as above
– Returns nothing (void), takes no arguments
15 {
– Declare variables
– Two types of variables
• Reference variables (called references)
– Refer to objects (contain location in memory)
• Objects defined in a class definition
• Can contain multiple data and methods
– paint receives a reference called g to a Graphics
object
– Reference used to call methods on the Graphics object
• Primitive data types (called variables)
– Contain one piece of data
• Method JOptionPane.showInputDialog
• Prompts user for input with string
• Enter value in text field, click OK
– If not of correct type, error occurs
– In Chapter 14 learn how to deal with this
• Returns string user inputs
• Assignment statement to string
– Lines 26-27: As above, assigns input to secondNumber
– Assignment statement
• sum an instance variable, can use anywhere in class
– Not defined in init but still used
3.6 Viewing Applets in a Web Browser
applet’s upperleft corner HTML file loaded into browser
status bar
Now, lets review packages needed for
applets
6.7 Java API Packages
• Packages
– Classes grouped into categories of related classes
– Promotes software reuse
– import statements specify classes used in Java programs
• e.g., import javax.swing.JApplet;
java.awt The Java Abstract Windowing Toolkit Package.
This package contains the classes and interfaces required to create and
manipulate graphical user interfaces in Java 1.0 and 1.1. In Java 2, these classes
can still be used, but the Swing GUI components of the javax.swing
packages are often used instead.
java.awt.event The Java Abstract Windowing Toolkit Event Package.
This package contains classes and interfaces that enable event handling for GUI
components in both the java.awt and javax.swing packages.
java.io The Java Input/Output Package.
This package contains classes that enable programs to input and output data
(see ChapterÊ
16, Files and Streams).
java.lang The Java Language Package.
This package contains classes and interfaces required by many Java programs
(many are discussed throughout this text) and is automatically imported by the
compiler into all programs.
public void
start()
This method is called after the init method completes execution. In addition, if the browser user visits another Web site and
later returns to the HTML page on which the applet resides, method start is called again. The method performs any tasks
that must be completed when the applet is loaded for the first time and that must be performed every time the HTML page on
which the applet resides is revisited. Typical actions performed here include starting an animation (see Chapter 19) and
starting other threads of execution (see Chapter 16).
public void
paint(
Graphics g )
This drawing method is called after the init method completes execution and the start method has started. It is also called
every time the applet needs to be repainted. For example, if the user covers the applet with another open window on the screen
and later uncovers the applet, the paint method is called. Typical actions performed here involve drawing with the
Graphics object g that is passed to the paint method by the applet container.
public void
stop()
This method is called when the applet should stop executing—normally, when the user of the browser leaves the HTML page
on which the applet resides. The method performs any tasks that are required to suspend the applet’s execution. Typical
actions performed here are to stop execution of animations and threads.
public void
destroy()
This method is called when the applet is being removed from memory—normally, when the user of the browser exits the
browsing session (i.e., closes all browser windows). The method performs any tasks that are required to destroy resources
allocated to the applet.
Fig. 6.11 JApplet methods that the applet container calls during an applet’s execution.
Variable vs. objects
A Reference
firstNumber is a
00000000 00000000 07045607
pointer !
7045607
{This is the actual place in memory where the String Object stores everything it needs to
accomplish its task as a String Object. Inevitably, it would be a lot more than a simple primitive...
More examples
import java.applet.*;
import java.awt.*;
public class DrawingLines extends Applet {
int width, height;
public void init()
{ width = getSize().width;
height = getSize().height;
setBackground( Color.black );
}
public void paint( Graphics g )
{
g.setColor( Color.green );
for ( int i = 0; i < 10; ++i )
{
g.drawLine( width, height, i * width / 10, 0 );
} }}
Example from
http://www.dgp.toronto.edu/~mjmcguff/learn/java/01drawingLines/
import java.applet.*;
import java.awt.*;
public class DrawingLines extends
Applet {
int width, height;
public void init()
{ width = getSize().width;
height = getSize().height;
setBackground( Color.black );
}
public void paint( Graphics g )
{
g.setColor( Color.green );
for ( int i = 0; i < 10; ++i )
{
g.drawLine( width, height, i *
width / 10, 0 );
} }}
More examples
• http://www.dgp.toronto.edu/~mjmcguff/learn/java/
02-drawingOtherStuff/