Java Applet
Java Applet
APPLET
Introduction
• Applet are small applications that are accessed on an Internet
server, transported over the Internet, automatically installed,
and run as part of a web document.
• After an applet arrives on the client, it has limited access to
resources so that it can produce a graphical user interface and
run various computations without introducing the risk of
viruses or breaching data integrity.
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet{
public void paint(Graphics g){
g.drawString(“A simple Applet”,20,20);
}
}
• After you enter the source code for SimpleApplet, compile in the
same way that you have been compiling programs.
• However, running SimpleApplet involves a different process. In
fact, there are two ways in which you can run an applet:
– Executing the applet within a Java-compatible web browser.
– Using an applet viewer, such as the standard tool, appletviewer.
• An applet viewer executes your applet in a window. This is
generally the fastest and easiest way to test your applet.
• One way to execute an applet in a web browser is to write a short
HTML text file that contains a tag that loads the applet.
• At the time of this writing, Oracle recommends using the APPLET
tag for this purpose.
String fontName;
int fontSize;
float leading;
boolean active;
//Initialize the string to be displayed
public void start() {
fontName = getParameter("fontName");
fontSize =Integer.parseInt(getParameter("fontSize"));
leading = Float.parseFloat(getParameter("leading"));
active = Boolean.parseBoolean(getParameter("accountEnabled"));
}
//Display parameters
public void paint(Graphics g) {
g.drawString("Font name:" + fontName, 0, 10);
g.drawString("Font size:" + fontSize, 0, 26);
g.drawString("Leading:" + leading, 0, 42);
g.drawString("Account Active:" + active, 0, 58);
}
}
Practices
import java.awt.*;
import java.applet.*;
/*
<applet code="Sample" width=300 height=100>
</applet>
*/
public class Sample extends Applet{
String msg;
//set the foregorund and background colors
public void init(){
setBackground(Color.cyan);
setForeground(Color.red);
msg="Inside init()--";
}
public void start(){
msg="Inside start()--";
}
public void paint(Graphics g){
msg="Inside paint()";
g.drawString(msg,10,30);
}
}
Using the Status Window
import java.awt.*;
import java.applet.*;
/*
<applet code="StatusWindow" width=300 height=100>
</applet>
*/
public class StatusWindow extends Applet{
public void init(){
setBackground(Color.cyan);
}
//Display msg in applet window
public void paint(Graphics g){
g.drawString("This is in the applet window",10,30);
showStatus("This is shown in the status window");
}
}
Graphics
Drawing Lines
• Lines are drawn by means of the drawLine( ) method, shown
here:
– void drawLine(int startX, int startY, int endX, int endY )
• drawLine( ) displays a line in the current drawing color that
begins at startX, startY and ends at endX, endY.
Drawing Rectangles
• The drawRect( ) and fillRect( ) methods display an outlined and
filled rectangle, respectively. They are shown here:
– void drawRect(int x, int y, int width, int height)
– void fillRect(int x, int y, int width, int height)
– The upper-left corner of the rectangle is at x, y. The
dimensions of the rectangle are specified by width and
height.
• To draw a rounded rectangle, use drawRoundRect( ) or
fillRoundRect( ), both shown here:
– void drawRoundRect(int x,int y,int width,int height,int
xDiam,int yDiam)
– void fillRoundRect(int x,int y,int width,int height,int
xDiam,int yDiam)
Drawing Ellipses and Circles
• To draw an ellipse, use drawOval( ). To fill an ellipse, use
fillOval( ). These methods are shown here:
– void drawOval(int x, int y, int width, int height)
– void fillOval(int x, int y, int width, int height)
Drawing Arcs
• Arcs can be drawn with drawArc( ) and fillArc( ), shown here:
– void drawArc(int x,int y,int width,int height,int
startAngle,int sweepAngle)
– void fillArc(int x,int y,int width,int height,int startAngle,int
sweepAngle)
Graphics Demo
import java.applet.*;
import java.awt.*;
/*
<applet code="GraphicsDemo.class" width=350 height=400>
</applet>
*/
public class GraphicsDemo extends Applet{
public void paint(Graphics g){
//draw lines
g.drawLine(0,0,100,90);
g.drawLine(0,90,100,10);
g.drawLine(40,25,250,80);
//Draw Rectangles
g.drawRect(10,150,60,50);
g.fillRect(100,150,60,50);
g.drawRoundRect(190,150,60,50,15,15);
g.fillRoundRect(280,150,60,50,30,40);
//Draw Arcs
g.drawArc(10,350,70,70,0,180);
g.fillArc(60,350,70,70,0,35);
}
}
GUI in Applet
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Calc extends Applet implements ActionListener {
Panel p1, p2;
Label l1, l2, l3;
TextField t1, t2, t3;
Button b1, b2, b3, b4;
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p1.add(l3);
p1.add(t3);
b1 = new Button("add");
b2 = new Button("mul");
b3 = new Button("sub");
b4 = new Button("div");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
if (ae.getActionCommand() == "add") {
c = a + b;
t3.setText("" + c);
}
if (ae.getActionCommand() == "mul") {
c = a * b;
t3.setText("" + c);
}
if (ae.getActionCommand() == "sub") {
c = a - b;
t3.setText("" + c);
}
if (ae.getActionCommand() == "div") {
double z = (double)a/ b;
t3.setText("" + z);
}