Professional Documents
Culture Documents
Lecture 4
Introduction to Java Applets
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
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>
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.
A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;
A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;
A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;
public class WelcomeApplet extends JApplet
{
A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;
public class WelcomeApplet extends JApplet
{
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 )
{
}
}
A Simple Applet
// A First Applet
import javax.swing.JApplet;
import java.awt.Graphics;
public class WelcomeApplet extends JApplet
{
public void paint( Graphics g )
{
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 );
}
}
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 );
}
}
Note: When you resize the Applet, you trigger the paint
method to fire.
Java I--Copyright 2000 Tom Hunter
Object:
A balloon
Attributes:
Color: red
Diameter: 2 inch
Behaviors:
Rises
Inflates
Deflates
Pops
Java I--Copyright 2000 Tom Hunter
calculate_pay
Draw A Line
The Graphics class that drew the String on our previous
Applet has many methods at our disposal.
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);
}
}
Draw A Line
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:
}
public void paint( Graphics g )
{
}
}