0% found this document useful (0 votes)
7 views5 pages

Java Applet Programming Guide

Uploaded by

jshehei
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Java Applet Programming Guide

Uploaded by

jshehei
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Applet Programming

📘 Notes on Applet Programming – I


1. What is an Applet?
An Applet is a small Java program that is usually embedded in a webpage.

Runs inside a browser (or special tool like appletviewer ).

Adds interactivity: play music/video, take user input, show images, login
forms, etc.

Unlike C, C++, Pascal, Java has built-in support for applets.

👉 Think of it as a mini window (GUI) inside a web page.


2. Structure of an Applet Window
Title / Status bar: Shows messages or status.

View area: Displays text, images, videos, menus, forms, etc.

Essentially, it provides a Graphical User Interface (GUI).

3. Steps to Write and Run an Applet

Java Applet Programming 1


1. Write Java Code

Import required packages:

import [Link].*;
import [Link].*;

Extend Applet class.

Example:

public class HelloWorld extends Applet {


public void paint(Graphics g) {
[Link]("Hello World", 150, 150);
}
}

2. Compile the program

javac [Link] → creates [Link]

3. Embed in HTML

<applet code="[Link]" width="300" height="300"></applet>

4. Run

Use appletviewer [Link]

(Modern browsers don’t support applets due to security reasons).

4. General Structure of Applet Code


1. Import Section – Import packages ( [Link] , [Link] , etc.)

2. Class Declaration – Must be public class MyApplet extends Applet

Java Applet Programming 2


3. Variable Declaration (optional)

4. Method Declaration – Overriding applet lifecycle methods.

5. Applet Lifecycle Methods


These methods are inherited from the Applet class and can be overridden:

1. init() → Initializes the applet (e.g., set size, read input).

2. start() → Called after init() , begins execution.

3. stop() → Stops the applet when the user leaves the page.

4. paint(Graphics g) → Used to display/draw things (text, shapes, images).

5. destroy() → Removes the applet from memory.

6. Using init() for Initialization


Can resize applet or take input from HTML.

Example: reading rectangle dimensions from <param> in HTML:

<applet code="[Link]" width="300" height="300">


<param name="x" value="20">
<param name="y" value="40">
<param name="w" value="100">
<param name="h" value="50">
</applet>

In Java:

public void init() {


int x = [Link](getParameter("x"));
int y = [Link](getParameter("y"));
int w = [Link](getParameter("w"));
int h = [Link](getParameter("h"));
// later used in paint()

Java Applet Programming 3


}

7. Difference: Application vs Applet


Feature Application Applet

Standalone ( main method, run Embedded in HTML, run with


Execution
with java ) appletviewer

Main method Required ( public static void main ) Not present

Cannot use standard I/O directly (must


Input/Output Can use keyboard, files, console
use HTML/event handling)

Full features of Java (files,


Flexibility Restricted (sandboxed for security)
threads, networking, etc.)

Purpose Solving problems, creating apps GUI, interactivity inside webpages

8. Limitations of Applets
Cannot directly read/write files or use keyboard input.

Restricted to certain packages ( applet , awt , swing ).

Cannot access local system resources freely (security reasons).

Need HTML or event handling for inputs.

4. Passing Parameters to Applet


HTML <param> tag is used inside <applet> to pass values.

Example:

<applet code="[Link]" width="400" height="200">


<param name="fontname" value="Courier">
<param name="fontsize" value="14">

Java Applet Programming 4


</applet>

In Java, use getParameter() method:

public void start() {


String font = getParameter("fontname");
int size = [Link](getParameter("fontsize"));
}

Java Applet Programming 5

You might also like