Transcript Lecture6

Intro to Applets
Applet
 Applets run within the Web browser environment
 Applets bring dynamic interaction and live animation to
an otherwise static HTML page
 Applications and applets share many common
programming features, although they differ slightly in
some respects


Every application must have a main method
Java applets do not need a main method
Applets in Java
 An applet is a program that runs in the
appletviewer (a test utility for applets is
included in J2SDK) or a Web browser such
as Netscape or Internet Explorer.
 The appletviewer (or browser) executes an
applet when an HTML document containing
the applet is opened in the appletviewer (or
browser).
The Applet Class
public class MyApplet extends Applet
{
public void init()
{ ... }
public void start()
{ ... }
other methods
}
The JApplet Class
 To use swing components, it is necessary to
create a Java Applet that extends
javax.swing.Applet

JApplet inherits all the methods from the
Applet class
import java.applet.*;
public class Welcome extends Applet{
}
Browser Calling Applet Methods
reload
enters web page
init
after init
return to the page
start
exit
stop
leave the page
destroy
The init method
 Invoked when the applet is first loaded and again if
the applet is reloaded.
public void init( ) {
}
 Common functions implemented in this method
include loading images, setting up user-interface
components, and getting parameters from the
<applet> tag in the HTML page.
Writing Applets
 Always extends the JApplet class, which is a
subclass of Applet for Swing components.

Override init(), start(), stop(), and destroy()
if necessary. By default, these methods are empty.
 Add your own methods and data if necessary.
 Applets are always embedded in an
HTML page.
To Create an Applet
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Where the awt packages refer to Abstract Windowing
Tools. These packages contain classes of GUI as well as
graphical components that you will need to create your
screens.
The awt package
 To make things “happen” in an applet, we need
a way to place text and/or graphics on the
screen and to receive input from the user.

While Swing does provide means in which to do
this, many browsers do not yet recognize Swing.
 The awt package provides many different
classes and methods to handle screen I/O.
The awt package
Applets sometimes make use of the
paint method of the java.awt.Component
class

public void paint (Graphics g )



called automatically (after init method) to “paint”
the screen
you must supply the instructions for what is to be
painted and where it goes!
Notice the parameter list ‘Graphics g’
The Graphics Class
(found in package java.awt.*;)
 Graphics is a class, g is an instance of that class
 One method of the Graphics class is:
drawString (String str, int x, int y)
The text to be displayed
The horizontal screen location (in pixels)
where the text is to be displayed
The vertical screen location (in pixels)
where the text is to be displayed
A Simple Applet
import java.awt.*;
import javax.swing.*;
public class HelloWorld extends JApplet
{
public void paint(Graphics g)
{
g.drawString("Hello World", 50, 100);
}
}
Flow of Control in JApplet:
The init and paint methods
 public void init ( )
 To initialize variables and references
 automatically called when the applet begins
executing
 guaranteed to be the first method called
 public void paint ( Graphics g )
 called after init( ) and when needed thereafter
 can be forced by using repaint( )
Viewing Applets
<html>
<head>
<title>
</head>
<body>
<applet
code = “Welcome.class”
width= 300
height = 100>
</applet>
</body>
</html>
Applications vs. Applets
 Similarities
 Since they both are subclasses of the Container class,
all the user interface components, layout managers, and
event-handling features are the same for both classes.
 Differences

Applications are invoked by the Java interpreter, and
applets are invoked by the Web browser.

Applets have security restrictions

Web browser creates graphical environment for applets,
GUI applications are placed in a frame.