Chapter 3
Introduction to Java Applets
appletviewer
This is a “minimal browser”—it ignores all other HTML
commands except the one used to run an Applet.
appletviewer syntax.
These parameters refer to the width
• To execute an Applet, do the following:
and height
Compile (in Java
your pixels) of theusing
Applet box javac,
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>
• Take special
Subclass note ofclass)
(derived the “extends” keyword.
WelcomeApplet
• extends means that our WelcomeApplet is
building on top of another class, JApplet !
• We’re 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;
}
}
Overriding a method
• This process is central to inheritance.
• Although we received a perfectly good method from the
Superclass, we decided to create our own version of
it—with the exact same name.
• Since we used the same name, our version of the
method takes over or overrides the method.
• We haven’t affected the original copy of the method—it
still exists—we just improved upon it in our own special
Subclass.
Instance
public classvariables:
AdditionApplet extends JApplet
{
• Declared outside
double sum; of any
// instance method
variable
•{ Automatically initialized
public void init()
This asterisk allows you to
• Visible in all methods of the class
import all the classes in a package.
Local variables: Notice,
( But only thisthe classessum
variable is
at this
• Declared inside a method outside
directory, of any
not any method.
sub-directories. )
sum
Also,is from
an instance
that variable.
wildcard
•} Not automatically initialized—a syntax error if you
Numeric
package, instance variables
the compiler willare
try to
public void use Graphics
paint( them with
g ) out first
automatically putting a
initialized value
only bring in those classes that to in.
zero,
{
• Vanish after the method returns
youchar toused
whatever
instance
actually in thecalled
variables are it.
program.
}
} automatically initialized to spaces
and boolean are automatically
w initialized Java
toI--Copyright
false.© 2000 Tom Hunter
// Add two floating point numbers
import javax.swing.*;
import java.awt.Graphics;
}
}
where
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...