You are on page 1of 28

Java I--Copyright 2000 Tom Hunter

Lecture 4
Introduction to Java Applets

Java I--Copyright 2000 Tom Hunter

Applets Execute in a Browser


There is no main method in a Java Applet.
A Java Applet can only run in a browser.
An Applet is run only when an HTML page requests that
it be executed.

In place of a browser, we use a utility called the

appletviewer
This is a minimal browserit ignores all other HTML
commands except the one used to run an Applet.
Java I--Copyright 2000 Tom Hunter

Applets Execute in a Browser

appletviewer syntax.
These parameters refer to the width
To execute an Applet, do the following:
and height
(in Java
pixels)
of theusing
box javac,
your Applet
Compile
your
Applet
as usual.
will get when it is executed on a web page.
C:\ javac Hello.java
Create an HTML file and name it: Example.html
<HTML>
<APPLET CODE=Hello.class WIDTH=300 HEIGHT=40>
</APPLET>
</HTML>

To run the Applet, you type the following:


C:\ appletviewer Example.html
Java I--Copyright 2000 Tom Hunter

A Simple Applet
Writing A String (A Sentence) using an Applet
Because an Applet gets help from a browser, it contains
much less code.
The most obvious omission is the main method.

Java I--Copyright 2000 Tom Hunter

A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;

Every Java Applet must commence by importing the


class JApplet.
This class does all the heavy lifting for us.
Java I--Copyright 2000 Tom Hunter

A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;

We are also importing class Graphics from the awt


(Abstract Windowing Tools) package so that we can draw
the String on the Applet.
Java I--Copyright 2000 Tom Hunter

A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;
public class WelcomeApplet extends JApplet
{

All code in an Applet sits in the wrapper of a class.


Likewise, the class name starts with a Capital
The public keyword enables the browser to create an
instance of this classwhat we have to do by ourselves
in an Application.

Java I--Copyright 2000 Tom Hunter

A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;
public class WelcomeApplet extends JApplet
{

This is Inheritancea very special principleand


there are special terms to describe the relationship.
}

Superclass (base class)

JApplet

Take special
note ofclass)
the extends
keyword.
Subclass
(derived
WelcomeApplet
extends means that our WelcomeApplet is
building on top of another class, JApplet !
Were taking everything it has, and adding to it !
Java I--Copyright 2000 Tom Hunter

A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;
public class WelcomeApplet extends JApplet
{
public void paint( Graphics g )
{
}
}

Now we have added a method called paint.


Notice its Access Modifier is public, so this method
can be called by any object outside of this class.
The return value is void.
Java I--Copyright 2000 Tom Hunter

A Simple Applet

// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;
public class WelcomeApplet extends JApplet
{
public void paint( Graphics g )
{

Dont get nervous. When you created an integer


}
variable
( int x; char m; ) you were doing
}
the same thing, Creating a instance of a type.
Graphics g is creating an example or
instance of the
Graphics
and naming
thatexpects
instance
Apparently,
paintclass
method
of JApplet
to g.
be
passed an object of type Graphics.
We are instantiating (creating) a Graphics class object
called g right there, inside the parenthesis of paint.
Java I--Copyright 2000 Tom Hunter

A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;
public class WelcomeApplet extends JApplet
{
public void paint( Graphics g )
{
g.drawString( Welcome to Java!, 25, 25 );
}
}

Finally, with this statement, we use the Graphics class


object g, and call the method drawString that all
Graphics class objects have.
drawString expects a String, plus two coordinates
that say where to place the bottom-left corner of the text.
Java I--Copyright 2000 Tom Hunter

A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;
public class WelcomeApplet extends JApplet
{
public void paint( Graphics g )
{
g.drawString( Welcome to Java!, 25, 25 );
}
}

Class JApplet already has a method called paint.


Class JApplets paint method is empty. It does nothing.
Although we did inherit the do-nothing method paint
from JApplet, ours will do something.
We are overriding the method paint that we inherited.
Java I--Copyright 2000 Tom Hunter

Applets Execute in a Browser

You might have to resize the Applet. You set the


dimensions of this box in your Example.html file.

Note: When you resize the Applet, you trigger the paint
method to fire.
Java I--Copyright 2000 Tom Hunter

More About Applets


In an Application, the method main is guaranteed to be
called by the operating system.

In an Applet, which has no main method, three other


methods are guaranteed to be called by the operating
system:
init()
start()
paint()
( In that order )
Java I--Copyright 2000 Tom Hunter

More About Applets


init() start() paint()
Since we did not override init()and start(),
the default versions of these methods were executed.
What did they do? Nothing!
Only paint() did something because we overrode it
and made it do something useful.

Finally, anytime you resize an Applet (meaning drag the


bottom right corner to make it bigger or smaller), then the
method paint() is automatically called again.
Java I--Copyright 2000 Tom Hunter

Thinking About Objects


Attributes and Behaviors: All Objects have them.
An Object in the real world has:
Attributes--its qualities, and its
Behaviors--what it does.

Object:
A balloon
Attributes:
Color: red
Diameter: 2 inch
Behaviors:
Rises
Inflates
Deflates
Pops
Java I--Copyright 2000 Tom Hunter

Thinking About Objects


Java is based on the unit of a class.
A class is an object, it encapsulates the attributes
and behaviors into one self-contained unit.
The attributes (internal data variables) and behaviors
(methods that have an effect on those internal data
variables) are combined into a unit called an object.

Java encapsulates data (attributes) and


methods (behavior)
into a unit called an
Object.
Java I--Copyright 2000 Tom Hunter

Thinking About Objects


An Employee is an Object
A Generic Employee has attributes:
name
address
phone_number
social_security_number
A Generic Employee has a method:

calculate_pay

Java I--Copyright 2000 Tom Hunter

Draw A Line
The Graphics class that drew the String on our previous
Applet has many methods at our disposal.

To the list of API classes we must know, we must now


add Graphics and JApplet.

Java I--Copyright 2000 Tom Hunter

Draw A Line
// Display Text and Lines
import javax.swing.JApplet;
import java.awt.Graphics;
public class WelcomeLines extends JApplet
{
public void paint( Graphics g )
{
g.drawLine( 15, 10, 210, 10 );
g.drawLine( 15, 30, 210, 30 );
g.drawString( Welcome to Java!, 25,25);
}
}

This will put lines above and below the sentence.


The 4 arguments are the beginning and end points of the
line.
Java I--Copyright 2000 Tom Hunter

Draw A Line

This is the output.

Java I--Copyright 2000 Tom Hunter

Applet Example: Addition


This will produce the same result as we achieved with
the Addition Application, only this time as an Applet.
The goal is to add two floating-point numbers.

Java I--Copyright 2000 Tom Hunter

// Add two floating point numbers


import javax.swing.*;
import java.awt.Graphics;

Two Kinds of Java Variables:


Instance
public classvariables:
AdditionApplet extends
{

JApplet

// instance
variable
Declared outside
of any
method
public void init()
{ Automatically initialized
This asterisk allows you to
Visible in all methods
of
the
class
import all the classes in a package.
double sum;

Notice,
thisthe
variable
is
( But only
classessum
at this
outside
of any
method.
directory,
not any
sub-directories.
)
Declared inside a method
sum
is from
an instance
variable.
Also,
that
wildcard
} Not automatically initializeda syntax error if you
Numeric
instance
variables
package,
the compiler
willare
try to
use Graphics
them with
out
first
putting
a
value
in.
public void
paint(
g
)
automatically
initialized
to
zero,
only bring in those classes that
{
Vanish after the method
returns
toused
whatever
instance
variables
are it.
youchar
actually
in thecalled
program.
}
automatically initialized to spaces
and boolean are automatically
initialized Java
toI--Copyright
false. 2000 Tom Hunter
w

Local variables:

// Add two floating point numbers


import javax.swing.*;
import java.awt.Graphics;
public class AdditionApplet extends JApplet
{
double sum;
// instance variable
public void init()
{
String firstNumber, secondNumber; // local variables
double num1, num2;
// local variables

}
public void paint( Graphics g )
{
}
}

Java I--Copyright 2000 Tom Hunter

// Add two floating point numbers


import javax.swing.*;
import java.awt.Graphics;
public class AdditionApplet extends JApplet
{
double sum;
// instance variable
public void init()
{
String firstNumber, secondNumber;
double num1, num2;
firstNumber = JOptionPane.showInputDialog(First Num );
secondNumber = JOptionPane.showInputDialog(Second Num );
num1 = Double.parseDouble( firstNumber );
num2 = Double.parseDouble( secondNumber );
sum = num1 + num2;
}
public void paint(Since
Graphics
g )and
num1
{
}
}

num2 are doubles, we use


the Type Wrapper class that can
convert a String into a double.
( There are Type Wrapper classes for all
the primitive data types. )
Java I--Copyright 2000 Tom Hunter

// Add two floating point numbers


import javax.swing.*;
import java.awt.Graphics;
public class AdditionApplet extends JApplet
{
double sum;
// instance variable
public void init()
{
String firstNumber, secondNumber;
double num1, num2;
firstNumber = JOptionPane.showInputDialog(First Num );
secondNumber = JOptionPane.showInputDialog(Second Num );
num1 = Double.parseDouble( firstNumber );
num2 = Double.parseDouble( secondNumber );
sum = num1 + num2;
}
public void paint( Graphics g )
{
g.drawRect( 15, 10, 270, 20 );
g.drawString( The Sum is + sum, 25, 25 );
}

In drawRect, the parameters are the coordinates


for the top left-hand corner, the width and the height.
Java I--Copyright 2000 Tom Hunter

Applet Example: Addition Output

Java I--Copyright 2000 Tom Hunter

You might also like