Introduction
To Java - MFC 158 G
Week
4 Lecture notes
Chapter 3 - Introduction to Java
Applets- MFC 158 (1 of 3)
I.
What's the
best way to proceed?
A.
Discuss Conventional parts of structured programming
1.
Environment, IO routines, control structures(while,if,etc.),
arrays
B.
Discuss basic OO concepts as needed until Chapter 8
II. Basic Object Oriented concepts
A. Objects - people, animals, plants, cars, etc.
1.
Objects humans perceive - beaches, not sand; houses, not
bricks and mortar; stereo system, not microchips.
2.
Can be generalized to have attributes - size, color, weight, shape, frequency, etc.
3.
Can be generalized to have behaviors - baby cries,
engine runs, radio plays.
4.
Different objects can exhibit similar behaviors -cars, trucks, wagons
B. Object Oriented Programming (OOP)
1.
Models real-world objects using digital structures
2.
OOP encapsulates
attributes and behavior into objects
a)
Use interfaces to
access a class, but inner workings
are hidden
3.
Information hiding - drive a car, but not concerned with
mechanics
4.
Objects communicate
via message passing - argument list,
return values
C. Classes
1.
The blueprint of an object
a)
Can create many houses from a single blueprint
b)
Can create many objects from a single class
2.
Contain data components - instance variables
3.
Contain function components - methods
4.
An instance of a variable/class - int total_amount, à class robot1
D. Reusability
1.
To develop similar classes, we can inherit qualities from
other classes and derive a new class (with additional attributes and behaviors)
2.
Avoid 'reinventing the wheel' - use existing classes - if
you can find them.
3.
Efficient programming should utilize reuse as much as
possible.
E. Object Oriented design intentions
1.
Goal of efficiency - with reuse and encapsulation
III. Applets
A.
runs from a Web browser / appletviewer, (not the command
window, like an app)
B.
not run directly,
referenced from an HTML document
C.
Based upon inheritance - from javax.swing.JApplet
D.
Phases of execution (appletviewer/browser) - initàstartàpaint -
(details on Pg 247)
1.
init() - executed
once at startup (init variables, load sounds/images, etc.)
2.
start() - called
after init() completes and each time the browser returns to the HTML where the
applet resides (after browsing another page).
Used for starting animations or other threads of execution.
3.
paint() - called
after init() completes and the start() method gets called. Gets called each time the applet needs to
get refreshed (after being covered, etc.)
4.
stop() - called when
the applet should stop executing - when the user has left the page that the
HTML resides (stop execution of animations, threads, etc.)
5.
destroy() - called when
user exits browser session. Performs
tasks required to destroy resources allocated to the applet.
Chapter 3 - Introduction to Java
Applets- MFC 158 (2 of 3)
//
Fig. 3.6: WelcomeApplet.java //
A first applet in Java import
javax.swing.JApplet; // class Japplet
is located in package javax.swing import
java.awt.Graphics; // class
Graphics is located in package java.awt public
class WelcomeApplet extends JApplet {
public void paint( Graphics g ) { g.drawString( "Welcome to Java
Programming!", 25, 25 ); } } C)
Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. |
-
Japplet class provides the basic functionality
of an applet (new ver of class from java.applet)
-
Classes
are almost never created entirely from scratch
-
Wouldn't
want to create an applet from scratch - requires over 200 methods to do so
-
Class
WelcomeApplet inherits all
functionality from Japplet by using extends
-
The
keyword public is required for any
class that inherits from JApplet
-
Since
default behavior of method paint
does nothing, we create our own, overriding default
-
Graphics class provides the
ability of drawing lines, rectangles, ovals, strings of characters
-
Method
paint’s parameter list requires the
use of a graphics object to function
-
The
graphics object is used by method paint to
draw graphics (rectangle, text, etc.)
-
Method
drawstring is called using the object g – followed by .drawString (drawLine,
drawRect).
-
drawString
takes 3 arguments (text, X-position in pixels and Y-position in pixels - upper
left is (0,0) )
Required
HTML for executing WelcomeApplet.class
<html> <applet
code="WelcomeApplet.class" width=300 height=30> </applet> </html> |
-
all
html should start with the tags html and end with /html
-
applet
code = ???.class
-
width
and height
-
the
upper left corner of the coordinate (0,0)
-
The
width is 300 pixels and the height is 30
-
It
may be necessary to modify this size based upon your application
//
Fig. 3.12: AdditionApplet.java //
Adding two floating-point numbers import
java.awt.Graphics; // import class
Graphics import
javax.swing.*; // import package
javax.swing public
class AdditionApplet extends JApplet { double sum; // sum of the values entered by the user public void init() { String firstNumber, // first string entered by user secondNumber; // second string entered by user double number1, // first number to add number2; // second number to add // read in first number from user firstNumber = JOptionPane.showInputDialog( "Enter first floating-point
value" ); // read in second number from user secondNumber = JOptionPane.showInputDialog( "Enter second
floating-point value" ); // convert numbers from type String to
type double number1 = Double.parseDouble(
firstNumber ); number2 = Double.parseDouble(
secondNumber ); // add the numbers sum = number1 + number2; } public void paint( Graphics g ) { // draw the results with g.drawString g.drawRect( 15, 10, 270, 20 ); g.drawString( "The sum is "
+ sum, 25, 25 ); } } |