Outline
3.1 Introduction
3.2 Sample Applets from the Java 2 Software Development Kit
3.2.1 The TicTacToe Applet
3.2.2 The DrawTest Applet
3.2.3 The Java2D Applet
3.3 A Simple Java Applet: Drawing a String
3.3.1 Compiling and Executing WelcomeApplet
3.4 Two More Simple Applets: Drawing Strings and Lines
3.5 Another Java Applet: Adding Floating-Point Numbers
3.6 Viewing Applets in a Web Browser
3.6.1 Viewing Applets in Netscape Navigator 6
3.6.2 Viewing Applets in Other Browsers Using the Java Plug-In
3.7 Java Applet Internet and World Wide Web Resources
3.8 (Optional Case Study) Thinking About Objects: Identifying the Classes in a Problem Statement
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 Sample Applets from the Java 2 Software Development Kit
Sample Applets
Provided in Java 2 Software Development Kit (J2SDK)
Source code included (.java files)
Study and mimic source code to learn new features
All programmers begin by mimicking existing programs
Located in demo directory of J2SDK install
Can download demos and J2SDK from
java.sun.com/products/jdk/1.3/
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.3 A Simple Java Applet: Drawing a String
Now, create applets of our own
Take a while before we can write applets like in the demos
Cover many of same techniques
Upcoming program
Create an applet to display
"Welcome to Java Programming!"
Show applet and HTML file, then discuss them line by line
1 // Fig. 3.6: WelcomeApplet.java
2 // A first applet in Java.
3
4 // Java core packages
5 import java.awt.Graphics; // import class Graphics
6
7 // Java extension packages
8 import javax.swing.JApplet; // import class JApplet
9
10 public class WelcomeApplet extends JApplet {
11
12 // draw text on applet’s background
13 public void paint( Graphics g )
14 {
15 // call inherited version of method paint
16 super.paint( g );
17
18 // draw a String at x-coordinate 25 and y-coordinate 25
19 g.drawString( "Welcome to Java Programming!", 25, 25 );
20
21 } // end method paint
22
23 } // end class WelcomeApplet
3.3 A Simple Java Applet: Drawing a String
Comments
Name of source code and description of applet
Import predefined classes grouped into packages
import statements tell compiler where to locate classes used
When you create applets, import the JApplet class (package javax.swing)
import the Graphics class (package java.awt) to draw graphics
Can draw lines, rectangles ovals, strings of characters
import specifies directory structure
3.3 A Simple Java Applet: Drawing a String
Applets have at least one class definition (like applications)
Rarely create classes from scratch
Use pieces of existing class definitions
Inheritance - create new classes from old ones (ch. 15)
Begins class definition for class WelcomeApplet
Keyword class then class name
extends followed by class name
Indicates class to inherit from (JApplet)
JApplet : superclass (base class)
WelcomeApplet : subclass (derived class)
WelcomeApplet now has methods and data of JApplet
3.3 A Simple Java Applet: Drawing a String
Class JApplet defined for us
Someone else defined "what it means to be an applet"
Applets require over 200 methods!
extends JApplet
Inherit methods, do not have to define them all
Do not need to know every detail of class JApplet
3.3 A Simple Java Applet: Drawing a String
Class WelcomeApplet is a blueprint
appletviewer or browser creates an object of class WelcomeApplet
Keyword public required
File can only have one public class
public class name must be file name
3.3 A Simple Java Applet: Drawing a String
Class WelcomeApplet is a blueprint
appletviewer or browser creates an object of class WelcomeApplet
Keyword public required
File can only have one public class
public class name must be file name
3.3 A Simple Java Applet: Drawing a String
Our class inherits method paint from JApplet
By default, paint has empty body
Override (redefine) paint in our class
Methods paint, init, and start
Guaranteed to be called automatically
Our applet gets "free" version of these by inheriting from JApplet
Free versions have empty body (do nothing)
Every applet does not need all three methods
Override the ones you need
Applet container “draws itself” by calling method paint
3.3 A Simple Java Applet: Drawing a String
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
3.3 A Simple Java Applet: Drawing a String
Calls version of method paint from superclass JApplet
Should be first statement in every applet’s paint method
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)
3.3.1 Compiling and Executing WelcomeApplet
Running the applet
Compile
javac WelcomeApplet.java
If no errors, bytecodes stored in WelcomeApplet.class
Create an HTML file
Loads the applet into appletviewer or a browser
Ends in .htm or .html
To execute an applet
Create an HTML file indicating which applet the browser (or appletviewer) should load and execute
3.3.1 Compiling and Executing WelcomeApplet
2
4
Simple HTML file (WelcomeApplet.html)
Usually in same directory as .class file
Remember, .class file created after compilation
HTML codes (tags)
Usually come in pairs
Begin with < and end with >
Lines 1 and 4 - begin and end the HTML tags
Line 2 - begins
No comments:
Post a Comment