Applet Programming
Applet Programming
Applet Programming
Introduction:
• Java programs are divided into two main categories, applets and
applications.
• An application is an ordinary Java program.
• An applet is a kind of Java program that can be run across the Internet.
• Applets are small Java programs that are embedded in Web pages.
• They can be transported over the Internet from one computer (web
server) to another (client computers).
• They transform web into rich media and support the delivery of
applications via the Internet.
Applet Programming
Introduction:
❖Advantages:
• There are many advantages:
➢It works at client side so less response time.
➢Secured
➢It can be executed by browsers running under many platforms, including Linux,
Windows, Mac Os etc.
❖Drawback
• Plug-in is required at client browser to execute applet.
Applet Programming
import java.applet.Applet;
import java.awt.*;
}
Applet Programming
➢public void init(): is used to initialized the Applet. It is invoked only once.
➢public void start(): is invoked after the init() method or browser is maximized. It
is used to start the Applet.
➢public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized.
➢public void destroy(): is used to destroy the Applet. It is invoked only once.
Applet Programming
publicclass HelloAppletMsgextendsApplet{
String msg;
publicvoid init()
{
msg = getParameter("Greetings");
if( msg == null)
msg = "Hello";
}
publicvoid paint(Graphicsg) {
g.drawString (msg,10, 100);
} This is name of parameter specified in
} PARAM tag ; This method returns the
value of paramter .
Applet Programming
What happen if we don’t pass parameter?
See HelloAppletMsg1.html getParameter() returns null..
<HTML> Some default value may be used.
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Communicating Applet on the Web!</h1>
<APPLET CODE="HelloAppletMsg.class" width=500 height=400>
</APPLET>
</body>
</HTML>
Applet Programming
Displaying Numeric Values
//SumNums.java
import java.applet.Applet;
import java.awt.*;
public class SumNums extends Applet {
public void paint(Graphics g) {
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
String str = "Sum: "+String.valueOf(sum);
g.drawString (str,100, 125); SumNums.html
} <HTML>
} < HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Sum of Numbers</h1>
<APPLET CODE="SumNums.class" width=500 height=400>
</APPLET> </body>
</HTML>
Applet Programming
Interactive Applet
• Applets work in a graphical environment. Therefore, applets treats
inputs as text strings.
• We need to create an area on the screen in which use can type and
edit input items.
• We can do this using TextField class of the applet package.
• When data is entered, an event is generated. This can be used to
refresh the applet output based on input values.
Applet Programming
//SumNumsInteractive..java
Interactive Applet
import java.applet.Applet;
import java.awt.*; sum = num1 + num2;
public class SumNumsInteractive extends Applet { String str = "THE SUM IS: "+String.valueOf(sum);
TextField text1, text2; g.drawString (str,100, 125);
public void init() }
{ public boolean action(Event ev, Object obj)
text1 = new TextField(10); {
text2 = new TextField(10); repaint();
text1.setText("0"); return true;
text2.setText("0"); }
add(text1); }
add(text2);
}
public void paint(Graphics g) {
int num1 = 0;
int num2 = 0;
int sum;
String s1, s2, s3;
g.drawString("Input a number in each box ", 10, 50);
try {
s1 = text1.getText();
num1 = Integer.parseInt(s1);
s2 = text2.getText();
num2 = Integer.parseInt(s2);
}
catch(Exception e1)
{}
Applet Programming
Summary
• Applets are designed to operate in Internet and Web environment.
• They enable the delivery of applications via the Web.
• In this presentation we learned:
✓ How do applets differ from applications?
✓ Life cycles of applets
✓ How to design applets?
✓ How to execute applets?
✓ How to provide interactive inputs?