Applet
Applet
Introducing Applets
Applets
– Java programs called from within another application
– Frequently run from a Web page
– Display as rectangular area
– Can respond to user-initiated events
– Behaviors come from Java class named JApplet
Steps to write an applet
– Set up layout for applet
– Create components and add them to applet
Applets
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.Panel
|
+----java.applet.Applet
|
+----javax.swing.JApplet
The simplest possible applet
TrivialApplet.java
import javax.swing.JApplet;
public class TrivialApplet extends JApplet { }
TrivialApplet.html
<applet
code="TrivialApplet.class"
width="150"
height="100">
</applet>
The simplest reasonable applet
import java.awt.*;
import javax.swing.JApplet;
Bytecode:
You save the MyApp.class You can view the web
file with a .html page from a web browser
extension
You write a Web
web page in Web page: Browser
Text Editor
html using an MyApp.html
editor
<html>
<head>
<title>Greetings Applet</title>
</head>
<body>
<applet code="Greetings.class" width=300 height=200 ></applet>
</body>
</html>
Running the Program
In the DOS window type appletviewer Greetings.html
Title
Your greeting
Message
What does it mean?
This line announces that the program These 2 lines tell the computer to include
(class) can be run by anyone (public), is (import) two standard libraries awt
called Greetings and is an Applet. (Abstract Window Toolkit) and applet.
import java.awt.*;
import java.applet.Applet;
import java.applet.*;
import java.awt.*;
An applet does not need to override those methods it does not use.
The following lists the most important methods that are usually
used:
import java.applet.*;
import java.awt.*;
public class AppletName extends Applet
{
public void init(){ . . . }
public void start(){ . . . }
public void stop(){ . . . }
public void destroy(){ . . .}
public void paint(Graphics g ){ . . .}
}
Applet Initialization and Termination
destroy()
public void paint(Graphics g)
For example, the following applet displays the string “Hello World!!”
starting at the point (50,25). Its file name must be HelloApplet.java.
import java.applet.*;
import java.awt.*;
<html>
<body>
The body of the html page… write whatever you like here.
</body>
</html>
Placing an Applet in a Web Page (cont’d)
The parts in green are called attributes. The applet tag has three
mandatory (non-optional) attributes:
– code: the name of the class file of the applet.
– width: the width of the applet, in pixels.
– height: the height of the applet, in pixels.
If the class file is not at the same folder as the HTML page, the
codebase attribute is used to indicate the location of the class file
relative to the directory that has the HTML page.
<applet code=“HelloApplet.class” codebase=“app\” width=600
height=100>
</applet>
Colors
The class Color of java.awt package is used to define Color objects.
All colors can be specified as a mix of three primary colors: red, green,
and blue. A particular color can be specified by three integers, each
between 0 and 255, or by three float values, each between 0.0 and 1.0.
The class Color has some pre-defined colors that are commonly used.
Color RGB Value RGB Value Color RGB Value RGB Value
(float) (integer) (float) (integer)
Color.black 0.0F, 0.0F, 0.0F 0, 0, 0 Color.magen 1.0F, 0.0F, 255, 0, 255
Color.blue 0.0F, 0.0F, 1.0F 0, 0, 255 ta 1.0F
Color.cyan 0.0F, 1.0F, 1.0F 0, 255, 255 Color.orange 1.0F, 0.8F, 255, 200, 0
0.0F
Color.gray 0.5F, 0.5F, 0.5F 128, 128,
128 Color.pink 1.0F, 0.7F, 255, 175,
0.7F 175
Color.darkGra 0.25F, 0.25F, 64, 64, 64
y 0.25F Color.red 1.0F, 0.0F, 255, 0, 0
0.0F
Color.lightGra 0.75F, 0.75F, 192, 192,
y 0.75F 192 Color.white 1.0F, 1.0F, 255, 255,
1.0F 255
Color.green 0.0F, 1.0F, 0.0F 0, 255, 0
Color.yellow 1.0F, 1.0F, 255, 255, 0
0.0F
Colors (cont’d)
For example:
Color c1 = new Color(255, 100, 18);
Color c2 = new Color(0.2F, 0.6F, 0.3F);
An Application An Applet
Runs independently Has to run inside another program,
called execution environment (like a
web browser or an applet viewer)
Can work with command-line (like Almost always works with GUI
what are always doing), or using a
graphical user-interface (GUI)
{More on this in ICS-201}
Program Output
– Comments
• Name of source code and description of applet
5 import java.awt.Graphics; // import class Graphics
8 import javax.swing.JApplet; // import class JApplet
– Method paint
• Lines 13-21 are the definition of paint
• Draws graphics on screen
• void indicates paint returns nothing when finishes task
• Parenthesis define parameter list - where methods receive data
to perform tasks
– Normally, data passed by programmer, as in
JOptionPane.showMessageDialog
• paint gets parameters automatically
– Graphics object used by paint
• Mimic paint's first line
– Body of paint
• Method drawString (of class Graphics)
• Called using Graphics object g and dot operator (.)
• Method name, then parenthesis with arguments
– First argument: String to draw
– Second: x coordinate (in pixels) location
– Third: y coordinate (in pixels) location
– Java coordinate system
• Measured in pixels (picture elements)
• Upper left is (0,0)
Program Output
Program Output
– Method init
• Normally initializes instance variables and applet class
• Guaranteed to be first method called in applet
• First line must always appear as above
– Returns nothing (void), takes no arguments
15 {
– Declare variables
– Two types of variables
• Reference variables (called references)
– Refer to objects (contain location in memory)
• Objects defined in a class definition
• Can contain multiple data and methods
– paint receives a reference called g to a Graphics
object
– Reference used to call methods on the Graphics object
• Primitive data types (called variables)
– Contain one piece of data
• Method JOptionPane.showInputDialog
• Prompts user for input with string
• Enter value in text field, click OK
– If not of correct type, error occurs
– In Chapter 14 learn how to deal with this
• Returns string user inputs
• Assignment statement to string
– Lines 26-27: As above, assigns input to secondNumber
– Assignment statement
• sum an instance variable, can use anywhere in class
– Not defined in init but still used
The font name can be the name of any font available on the particular computer
(such as: TimesRoman, Courier, Helvetica etc.,) or any of the logical names shown
in the table below:
Serif A font with small segments at the end, e.g. Times New
Roman
import java.applet.*;
import java.awt.*;
• If an applet will use several fonts, it is good to create the font objects
in the init() method:
import java.applet.*;
import java.awt.*;
public class FontExamples extends Applet{
private Font f, fb, fi, fbi;
public void init() {
setBackground(Color.yellow);
f = new Font("TimesRoman", Font.PLAIN, 18);
fb = new Font("Courier", Font.BOLD, 20);
fi = new Font("TimesRoman", Font.ITALIC, 18);
fbi = new Font("Helvetica", Font.BOLD + Font.ITALIC, 25);
}
public void paint(Graphics g){
g.setColor(Color.blue);
g.setFont(f);
g.drawString("This is TimesRoman plain font", 10, 25);
//...
}
}
Drawing in an Applet
import java.applet.Applet;
import java.awt.*;
// assume that the drawing area is 150 by 150
public class SquareAndRectangle extends Applet
{
final int areaSide = 150 ;
final int width = 100, height = 50;
public void paint ( Graphics gr )
{
setBackground( Color.green );
gr.setColor( Color.red );
// outline the drawing area
gr.drawRect( 0, 0, areaSide-1, areaSide-1 );
// draw interiour rectange.
gr.drawRect( areaSide/2 - width/2 ,
areaSide/2 - height/2, width, height );
}
}
Java’s coordinate system
(0, 0) (0, 0)(50, 0) (50, 0)
(w-1, h-1)
g.setColor(Color.RED);
g.fillRect(50, 30, 50, 30);
g.setColor(Color.BLACK);
g.drawString("Example JApplet", 20, 80);
}
}
More java.awt.Graphics methods
g.drawLine(x1, y1, x2, y2);
g.drawOval(left, top, width, height);
g.fillOval(left, top, width, height);
g.drawRoundRect(left, top, width, height,
arcWidth, arcHeight);
– arcWidth, arcHeight define the “roundedness” of corners
g.fillRoundRect(left, top, width, height, arcWidth, arcHeight);
g.drawArc( left, top, width, height, startAngle, arcAngle);
– Angles are in degrees
– 0 degrees is the 3 o’clock position
– Positive angles are to the right
g.FillArc( left, top, width, height, startAngle, arcAngle);
Still more Graphics methods
g.drawPolygon(xPoints, yPoints, n);
g.fillPolygon(xPoints, yPoints, n);
– xPoints and yPoints are int arrays of size n
– One way to write an int array is:
new int[] { value1, value2, ..., valueN}
– Example: g.drawPolygon(new int[] { 250, 290, 210 },
new int[] { 210, 290, 290 }, 3);
draws a triangle using the 3 points (250, 210), (290,
290), and (210, 290).
g.drawPolyline(xPoints, yPoints, n);
– A “polyline” is like a polygon, except the first and last points are
not automatically connected
– Hence, there is no “fillPolyline” method
The HTML page
System.out.println(String s)
– Works from appletviewer, not from browsers
– Automatically opens an output window.
showStatus(String s) displays the String in
the applet’s status line.
– Each call overwrites the previous call.
– You have to allow time to read the line!