Transcript Applet

Applets
Java API
Applet Examples

Java Programs:



Application programs
Applet programs
Applets are designed to run on Web pages. Here
are a couple of applet, from JSDK examples.
(These can be found in j2sdk1.4/demo/applets.)





Clock
Molecule 1
Molecule 2
Molecule 3
Tic-Tac-Toe
Applet Restrictions

Applets have restrictions for security reasons.
 Cannot read or write files on the user's computer,
to prevent damaging files and spreading viruses.
 Cannot run any program on the user's system.
Otherwise, the local system could be destroyed or
modified.
 Cannot connect the user's computer to any other
computer, except to the server where the applets
are hosted. Without this restriction, an applet may
connect the user's computer to another computer
without the user's knowledge.
<applet> Tag in HTML Page
<html>
<head>
<title>Applet Demo</title>
</head>
<body>
<h1>An Applet Demo</h1>
<applet code="Sample.class“
codebase=“code/applets”
width="200" height="200>
</applet>
</body>
</html>
Applet Class


You create an applet by extending
java.applet.Applet class. Applet has no main(),
since the program is executed automatically by the
browser.
Applet contains several methods, which can be
overridden by the programmer.
 public void init()
 public void stop()
 public void destroy()
 public void paint()
Applet Class (cont.)

public void init() -- called when the applet is
loaded, as the enclosing Web page is opened.
This method might include creating objects that
the applet needs--e.g., creating an interface,
setting fonts and loading images, and reading
applet parameters from the web page. Like a
constructor in application.
 public void stop()--called when the user leaves
the page containing the applet. The method may
be overridden by a code, for example, that
would stop any processing to conserve
resources. In such a case, a corresponding code
must be included in the start() method to restart
the processing.
Applet Class (cont.)


public void destroy()--called when the browser
exits normally. The stop() method is always called
before the this method in called.
 public void paint(Graphcs g)--called whenever the
applet displays something on the screen, such as
text, line, color, or image. The method is called
automatically whenever the window containing an
applet is redrawn, for example, after visiting a
different page.
Ordinarily, the methods init() and the paint() are
overridden. Others can be left alone, in which case,
nothing specific will be done when they are invoked
by the browser.
Example: Applet Welcome

Image of an applet interface
Welcome.java Source Code
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class Welcome extends Applet {
String msg = "Welcome to the world “ +
“of Applets.";
public void paint(Graphics g) {
setBackground(Color.cyan);
g.drawOval(5, 50, 200, 40);
g.drawString(msg, 15, 75);
g.drawLine(5, 100, 205, 100);
}
}
Welcome.java Source Code

Note:
Only paint(Graphics g) method needs to be
over-ridden.
 Graphics g is the handle to the graphics
environment for drawing figures and texts in
the applet.
 setBackground(Color.cyan) sets background
color of the applet—think of it as a frame

Welcome.html
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome Message</h1>
<hr>
<applet code="Welcome.class“
codebase=“subfolder/”
width=220 height=120>
</applet>
</body>
</html>
View Applet
//optional
Interactive Applet Welcome2




Applet Welcome2 prompts the user to
click a button, causing an input panel to
pop up and to display a welcome message
to a text field.
It incorporates event detection and event
handler in the same way that you have
already seen with application programs.
Run Welcome2
Code Welcome2.java
Welcome2 (cont.)

In the source code, note:
 Interface has three objects, but on btnStart is
associated with an event.
 In init() method:
 Objects are created
 They are arranged in a panel
 Button is associated with event listener
 Panel is inserted into the applet
 In actionPerformed() method:
 yourName = JOptionPane.showInputDialog
pops
Applet with Parameters


In Welcome example the background
color (cyan) was hard-coded in the applet
code. It would be more convenient if the
color can be changed during its use in the
Web page.
The color information can be received by
the applet as a parameter from the Web
page.
Applet with Parameters (cont.)
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Applet with Parameter</h1>
<applet code="Welcome3.class"
width=300 height=100>
<param name="bgcolor" value="green">
</applet>
</body>
</html>
Applet with Parameters (cont.)


Using the same Welcome3 applet:
 Web Page 1
 Web Page 2
 Web page 3
Welcome3.java Source Code
Applet with Paramers (cont.)

Note:

String color = getParameter("bgcolor");
captures the value of parameter “bgcolor” from
the HTML page.

if (color.equals("green"))
setBackground(Color.green);
else if . . .
Not color == “green”, because String
variable is a reference.
Applet to Display Graph


Next example uses an applet (Bargraph)
to display a bar graph.
Values of bar heights are captured from
the Web page.
Applet to Display a Graph (cont.)
<html>
<head> <title>Graph Test</title></head><body>
<applet code="BarGraph.class"
width=300 height=200>
<param name="title" value="Quiz Scores">
<param name="num0" value="85">
<param name="num1" value="90">
<param name="num2" value="73">
<param name="num3" value="100">
<param name="num4" value="82">
<param name="num5" value="68">
</applet>
</body>
</html>
Applet to Display a Graph (cont.)


barGraph.HTML
BarGraph.java Source Code
BAR_WIDTH, MAX_HEIGHT, & GAP are
constants that specify the bars and the spacing
between them.
 title = getParameter("title");
captures the value of the “title” parameter.
 value = getParameter("num" + count);
In the HTML page, parameters are named
“num0”, “num1”, etc., so that the applet code can
use a loop to form those names.

Applet to Display a Graph (cont.)

Values of parameters are stored in array
numList[].
 In the paint() method,
 g.setColor(Color.BLUE);
Once a color is set, it remains the same for
subsequent drawings until it is changed.
 Both Color.BLUE & Color.blue are OK.
 fillRectangle() uses the values in numList[]
to draw rectangles.
With Different Parameters
<html>
<head> <title>Graph Test</title></head><body>
<applet code="BarGraph.class"
width=300 height=200>
<param name="title" value="Quiz Scores">
<param name="num0" value=“20">
<param name="num1" value=“40">
<param name="num2" value=“60">
<param name="num3" value=“80">
<param name="num4" value=“100">
<param name="num5" value=“120">
</applet>
</body>
Run barGraph2.html
</html>
Converting Application to Applet


To show the relationship between an
application and an applet, the next
example shows how to convert a previous
application—ConverDistance.java—to an
applet.
Application ConvertDistance.java Source
Code
Converting Application to Applet




Extend Applet, instead of Frame
class DistanceConverter extends Applet
implements ActionListener {
instead of
class DistanceConverter extends JFrame
implements ActionListener {
Applet has no constructor. Method init() takes its
place.
Most of code in constructor is appropriate in init(),
exept for super(), setSize(),
setDefaultCloseOperation() , and setVisitble(true) .
Applet ConvertDistance.java Source Code