Module 6
Basics of applet programming Applet life cycle.
Applet Programming in Java
Applet programming is a technique in Java that allows developers to create small
applications that run inside a web browser or an applet viewer. Java applets are
embedded in web pages and executed using the Java Virtual Machine (JVM). They
provide interactive features, graphics, and animations.
Life Cycle of an Applet
A Java applet follows a specific lifecycle managed by the browser or applet
viewer. The key methods in an applet’s lifecycle are:
1. init() – Initializes the applet. It is called once when the applet is first loaded.
2. start() – Starts or restarts the applet when the web page is opened or
revisited.
3. paint(Graphics g) – Handles rendering and graphics, drawing content in the
applet window.
4. stop() – Stops the applet when the user leaves the web page.
5. destroy() – Cleans up resources before the applet is removed from
memory.
Basic Applet Example
import [Link].*;
import [Link].*;
/* <applet code="[Link]" width="300" height="200"></applet> */
public class AppletDemo extends Applet {
// Initializes the applet
public void init() {
[Link]("Applet Initialized");
}
// Starts the applet
public void start() {
[Link]("Applet Started");
}
// Paint method to display text
public void paint(Graphics g) {
[Link]("Hello, Java Applet!", 50, 100);
}
// Stops the applet
public void stop() {
[Link]("Applet Stopped");
}
// Destroys the applet
public void destroy() {
[Link]("Applet Destroyed");
}
}
Advantages of Applets
Platform-independent (runs on any OS with JVM).
Can be embedded in web pages.
Provides graphical user interface (GUI) capabilities.
Secure execution within a restricted environment (sandbox).
Disadvantages of Applets
Requires JVM installation.
Modern browsers have dropped support for applets.
Limited access to system resources.
Alternatives
With the decline of applets, technologies like JavaScript, HTML5, and JavaFX have largely
replaced them for interactive web applications.
WAP in java using an applet to show how to draw “Line,Arc,Oval, Rectangle and String.”
import [Link].*;
import [Link].*;
/* <applet code="[Link]" width="400" height="300"></applet> */
public class DrawShapesApplet extends Applet {
public void paint(Graphics g) {
// Drawing a Line
[Link](20, 30, 200, 30);
// Drawing an Arc
[Link](50, 50, 100, 100, 0, 180);
// Drawing an Oval
[Link](200, 50, 100, 50);
// Drawing a Rectangle
[Link](50, 150, 100, 50);
// Drawing a String
[Link]("Hello, Applet Graphics!", 150, 200);
WAP in java to show how to color boundaries and complete area of specific objects designed
by applet.
Import [Link].*;
import [Link].*;
import [Link].*;
/* <applet code="[Link]" width="400" height="300"></applet> */
public class ColoredShapesApplet extends Applet {
public void paint(Graphics g) {
// Set color for boundaries and draw a rectangle
[Link]([Link]);
[Link](50, 50, 100, 50);
// Set color for filling and fill a rectangle
[Link]([Link]);
[Link](200, 50, 100, 50);
// Set color for an oval boundary and draw it
[Link]([Link]);
[Link](50, 150, 100, 50);
// Set color for filling and fill an oval
[Link]([Link]);
[Link](200, 150, 100, 50);
// Set color for text
[Link]([Link]);
[Link]("Colored Shapes in Applet", 100, 250);
WAP in java Design an appl program to design different shapes.
Import java util.*;
import [Link].*;
import [Link].*;
/* <applet code="[Link]" width="500" height="400"></applet> */
public class ShapesApplet extends Applet {
public void paint(Graphics g) {
// Drawing a rectangle
[Link]([Link]);
[Link](50, 50, 100, 50);
// Drawing a filled rectangle
[Link]([Link]);
[Link](200, 50, 100, 50);
// Drawing an oval
[Link]([Link]);
[Link](50, 150, 100, 50);
// Drawing a filled oval
[Link]([Link]);
[Link](200, 150, 100, 50);
// Drawing an arc
[Link]([Link]);
[Link](50, 250, 100, 100, 0, 180);
// Drawing a filled arc
[Link]([Link]);
[Link](200, 250, 100, 100, 0, 180);
// Drawing a polygon (triangle)
int[] xPoints = {350, 400, 450};
int[] yPoints = {200, 100, 200};
[Link]([Link]);
[Link](xPoints, yPoints, 3);
// Drawing a filled polygon (pentagon)
int[] xPentagon = {350, 400, 450, 425, 375};
int[] yPentagon = {300, 250, 300, 350, 350};
[Link]([Link]);
[Link](xPentagon, yPentagon, 5);
}
Difference between application & applet programming.
Application Programming Applet Programming
A standalone Java program that runs on A small Java program that runs inside a web
the operating system. browser or an applet viewer.
Executed using the Java Virtual Machine Runs inside a web browser or an applet viewer.
(JVM) via java command.
Does not require a main() method; follows an
Requires a main() method to start applet lifecycle (init(), start(), paint(), stop(),
execution. destroy()).
Uses Swing, AWT, JavaFX, or command- Uses AWT for graphics and user interaction.
line interface.
Has full access to system resources (file Runs in a sandbox with restricted access to system
system, network, etc.). resources for security reasons.
Used for general-purpose computing, Used for interactive web-based applications
enterprise applications, and backend (though now outdated).
services.
Banking software, data processing Small web-based games, animations, and
applications, mobile apps, and games. embedded tools in HTML pages.
Applications are independent programs that run on the JVM.
Applets are small Java programs designed to run in browsers (though modern browsers no
longer support them).
Server/client communication
Server-client communication refers to the exchange of data between a server (which provides services)
and a client (which requests services). It is widely used in web applications, network programming, and
cloud computing.
Example: Java Socket Programming (Client-Server Communication)
import [Link].*;
import [Link].*;
// Server Program
class Server {
public static void main(String args[]) throws IOException {
ServerSocket server = new ServerSocket(5000);
[Link]("Server is waiting for client...");
Socket socket = [Link]();
DataInputStream dis = new DataInputStream([Link]());
String message = [Link]();
[Link]("Client says: " + message);
[Link]();
[Link]();
[Link]();
// Client Program
class Client {
public static void main(String args[]) throws IOException {
Socket socket = new Socket("localhost", 5000);
DataOutputStream dos = new DataOutputStream([Link]());
[Link]("Hello, Server!");
[Link]();
[Link]();
Hardware communication
Hardware communication involves interfacing software with physical hardware like sensors,
actuators, or embedded systems. This is commonly seen in IoT, robotics, and industrial
automation.
Example: Java Serial Port Communication (With External Hardware)
import [Link].*;
public class SerialCommunicationExample {
public static void main(String[] args) {
SerialPort port = [Link]("COM3"); // Change COM port as needed
[Link](9600);
if ([Link]()) {
[Link]("Port opened successfully!");
String message = "Hello Hardware!";
[Link]([Link](), [Link]());
} else {
[Link]("Failed to open port!");
}
[Link]();
}
}
Enterprise communication
Enterprise communication focuses on data exchange between large-scale applications in
business environments. Common protocols include REST APIs, SOAP, JMS (Java Messaging
Service), and WebSockets.
Example: Java REST API Using Spring Boot (Enterprise Communication)
import [Link].*;
import [Link];
import [Link];
@SpringBootApplication
@RestController
public class EnterpriseCommunication {
public static void main(String[] args) {
[Link]([Link], args);
}
@GetMapping(\"/message\")
public String getMessage() {
return \"Hello, Enterprise Communication!\";
Comparison of Communication Types
Type Used For Example Technologies
Server/Client Web applications, chat apps TCP/IP, HTTP, WebSockets
Hardware IoT, automation, robotics Serial Communication, Bluetooth, MQTT
Enterprise Large-scale applications REST API, SOAP, JMS