0% found this document useful (0 votes)
25 views

APPLET

Uploaded by

itsadarshraj17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

APPLET

Uploaded by

itsadarshraj17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

APPLET

An applet is a small program that runs in a web browser or applet viewer. Applets
extend the java.applet.Applet class or javax.swing.
JApplet (for Swing-based applets). They were initially designed for creating dynamic
content for web pages but have been deprecated in modern Java versions.

Key Features

1. No main() Method: Unlike regular Java applications, applets are not standalone
programs. The browser or applet viewer invokes lifecycle methods to control
their execution.
2. Predefined Lifecycle: The applet lifecycle involves specific methods (init(),
start(), stop(), destroy()), which you override to customize behavior.
3. Graphical User Interface (GUI): Applets are capable of creating graphical
interfaces using AWT or Swing.
4. Security Restrictions: Applets run in a sandbox environment and cannot access
local system resources (e.g., files) for security reasons.

Applet Lifecycle in Detail

1. init():
o Called once during the initialization phase.
o Used for applet setup (e.g., loading resources, initializing variables).
2. start():
o Called every time the applet becomes active (e.g., when the web page
containing the applet is loaded or refreshed).
o Used to start animations or threads.
3. paint(Graphics g):
o Called whenever the applet needs to redraw itself.
o Used for rendering graphics or text.
4. stop():
o Called when the applet becomes inactive (e.g., user navigates away from
the page).
o Used to stop animations or release resources.
5. destroy():
o Called once when the applet is being removed from memory.
o Used for cleanup tasks.
APPLET PROGRAMME:-
import java.applet.Applet;
import java.awt.Graphics;

public class MyApplet extends Applet


{
// Called when the applet is first loaded
public void init()
{
System.out.println("Applet initialized");
}

// Called when the applet starts or resumes


public void start()
{
System.out.println("Applet started");
}

// Called to perform drawing


public void paint(Graphics g)
{
g.drawString("Hello, Applet!", 50, 50); // Draw text on applet
}
// Called when the applet stops
public void stop()
{
System.out.println("Applet stopped");
}

// Called when the applet is destroyed


public void destroy()
{
System.out.println("Applet destroyed");
}
}

Key Limitations of Applets

1. Browser Support: Modern browsers no longer support applets.


2. Deprecated in Java: Removed from Java 11 onwards.
3. Security Restrictions: Limited access to resources, making it unsuitable for
complex applications.

Modern Alternatives

 JavaFX: Used for building rich client applications.


 HTML5, CSS3, and JavaScript: Widely adopted for interactive web
applications.
MVC Architecture Overview

MVC (Model-View-Controller) is a design pattern used to separate an application into three


main components: Model, View, and Controller. This separation helps organize code,
making it more maintainable and scalable by decoupling the user interface (UI) from the
business logic and data management.

1. Model:

 Represents the data and business logic of the application.


 It directly manages the data, logic, and rules of the application.
 The Model is responsible for fetching, updating, and deleting data. It does not depend on
the user interface.
 It may notify the View when data changes (in event-driven syst ems).

Example:

In a banking application, the Model would represent the bank account, handling operations
like deposit, withdrawal, and balance calculation.

2. View:

 Displays the data to the user.


 The View is the UI part of the application (e.g., forms, buttons, tables).
 It listens for changes in the Model and updates the UI to reflect changes.
 The View should not contain business logic.

Example:

In the banking application, the View would show the balance, display buttons for
deposit/withdrawal, and show error messages.

3. Controller:

 Acts as the intermediary between the Model and the View.


 The Controller accepts user input, processes it (with possible updates to the Model), and
updates the View.
 The Controller responds to actions triggered by the View (such as button clicks) and modifies
the Model or updates the View accordingly.

Example:

In the banking application, the Controller would manage events like button clicks (Deposit or
Withdraw) and call the appropriate Model methods to update the account balance.
AWT: window fundamentals
AWT is a set of APIs provided by Java to create graphical user interfaces (GUIs).

AWT is part of Java’s standard library, and it provides basic UI components (like buttons, text fields,
and labels), event handling, and window management.

1. Creating a Frame in AWT

A Frame is a top-level window that can contain other components like buttons, text fields,
etc. It is used to represent the main window of the application.

In AWT, you create a frame using the Frame class, which is a subclass of Window.

Creating a frame in AWT (Abstract Window Toolkit) is simple. The Frame class in AWT is
used to create a basic window for a graphical user interface (GUI).

Basic Steps to Create a Frame:

1. Create a subclass of Frame.


2. Add components to the frame.
3. Make the frame visible.

import java.awt.Frame;

public class SimpleAWTFrame

public static void main(String[] args)

// Step 1: Create a Frame

Frame frame = new Frame("My First Frame");

// Step 2: Set the size of the Frame

frame.setSize(400, 300); // 400 pixels wide and 300 pixels high

// Step 3: Make the Frame visible

frame.setVisible(true);
// Step 4: Close the Frame when the close button is clicked

frame.addWindowListener(new java.awt.event.WindowAdapter()

public void windowClosing(java.awt.event.WindowEvent e)

System.exit(0); // Close the program

});

What Each Line Does:

1. import java.awt.Frame;
o This imports the Frame class from the java.awt package.
2. Frame frame = new Frame("My First Frame");
o Creates a new frame window with the title "My First Frame."
3. frame.setSize(400, 300);
o Sets the width and height of the frame to 400x300 pixels.
4. frame.setVisible(true);
o Makes the frame visible on the screen. Frames are not visible by
default.
5. frame.addWindowListener(...)
o Adds a listener to detect the event when the user clicks the close
button (the "X" in the top-right corner).
6. System.exit(0);
o Ends the program when the window is closed.

1. new java.awt.event.WindowAdapter()

 Purpose: Creates an anonymous inner class that extends the WindowAdapter


class.
 Why use WindowAdapter?
o The WindowListener interface has multiple methods (like
windowOpened, windowClosing, etc.).
o If you implement WindowListener, you must provide implementations
for all its methods, even if you need only one (e.g., windowClosing).
o To simplify this, WindowAdapter provides default (empty)
implementations of all WindowListener methods, and you can override
only the ones you need.

public void windowClosing(java.awt.event.WindowEvent e)

 Purpose: This method is called automatically when the window is being closed
(e.g., when the user clicks the close button (X)).
 What it does: Inside this method, you specify what should happen when the
close button is clicked. In most cases, you'll call System.exit(0) to terminate the
program.
2. Adding Components to a Frame

AWT components like buttons, text fields, and labels can be added to a frame using the
add() method.

import java.awt.*;

public class FrameWithButton

public static void main(String[] args)

// Create a frame with the title "Frame with Button"

Frame frame = new Frame("Frame with Button");

// Create a button

Button button = new Button("Click Me");

// Add the button to the frame

frame.add(button);

// Set the size of the frame

frame.setSize(400, 300);

// Set the frame to close when clicked

frame.addWindowListener(new java.awt.event.WindowAdapter() {

public void windowClosing(java.awt.event.WindowEvent we) {


System.exit(0);

});

// Make the frame visible

frame.setVisible(true);

}
Write a program in JAVA to implement GUI in AWT
import java.awt.*;
import java.awt.event.*;

public class SimpleAWT {


public static void main(String[] args) {
// Create a Frame
Frame frame = new Frame("Simple AWT Example");

// Create a Label, TextField, and Button


Label label = new Label("Enter your name:");
TextField textField = new TextField(20);
Button button = new Button("Submit");

// Add components to the frame


frame.add(label);
frame.add(textField);
frame.add(button);

// Set layout and frame size


frame.setLayout(new FlowLayout());
frame.setSize(300, 200);
// Add button click event
button.addActionListener(e -> {
String name = textField.getText();
frame.setTitle("Hello, " + name);
});

// Close the frame when "X" is clicked


frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

// Make the frame visible


frame.setVisible(true);
}
}

EXPLANATION OF FEW TERMS:-


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Action when button is clicked
System.out.println("Button clicked!");
}
});

What It Does

1. button.addActionListener(...)
o Attaches an event listener to the button.
o It listens for click events on the button.
2. new ActionListener() { ... }
o Creates an anonymous inner class that implements the
ActionListener interface.
o The ActionListener interface has a single method:
actionPerformed(ActionEvent e).
3. public void actionPerformed(ActionEvent e)
o This method is executed whenever the button is clicked.
o Parameter ActionEvent e: Contains details about the event, such
as which button was clicked.
4. System.out.println("Button clicked!");
o The action to be performed when the button is clicked.
o In this case, it simply prints "Button clicked!" to the console.

You might also like