PROGRAM: 8
AIM:Write a Java program to demonstrate applet programming.
VERVIEW:Java applets were small applications writtenin the Java programming language
O
that could be embedded in web pages and run within a web browser. They were originally used
to add interactivity, multimedia, and dynamic content to websites. Java applets were popular in
the late 1990s and early 2000s but have largely been replaced by newer web technologies due to
security concerns and changes in browser support.
Purpose:
● J ava applets were designed to beplatform-independent,meaning they could run on any
operating system that had aJava Virtual Machine (JVM)installed.
● They were often used forinteractive web applications,games, charts, scientific
simulations,andmultimedia presentationson websites.
How They Worked:
<applet>tag (in older browsers) or
● Applets were embedded inHTML pagesusing the
object>tag.
<
The browser would download the applet and run it inside a special"applet viewer"or
●
applet containerprovided by the JVM.
● Applets hadrestricted access to system resourcesfor security reasons, operating in a
"sandbox" environment.
End of Java Applets:
A
● s of2015, Oracle officiallydeprecated Java appletsand theNPAPI plugin.
● By2017, most browsers stopped supporting them entirely.
● Java applets are now consideredobsoletein favorof modern web development standards.
CODE:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class program_8 {
public static void main(String[] args) {
JFrame frame = new JFrame("Sudhanwa's Applet Application");
JLabel label = new JLabel("Hello, World!", SwingConstants.CENTER);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
OUTPUT:
PROGRAM: 9
AIM:Write an applet for event handling which printsa message when Clicked on a button.
OVERVIEW:
vent handling was an essential aspect of Java applets, enabling user interaction with the applet.
E
Java’s event-handling model allows applets to respond to various actions, such as mouse clicks,
keyboard inputs, and other user interactions.
J ava applets followed the listener-based model, where an event (e.g., a mouse click) triggered a
method in a listener object that responded to that event.
Event-Driven Programming:
● J ava applets areevent-driven, meaning they do notrun continuously in a loop but
insteadrespond to user actionssuch as:
○ Mouse movements, clicks, and drags
○ Keyboard presses
○ System-generated events(e.g., window resizing)
● When an event occurs, theappropriate event handlermethodis called automatically.
Event Sources:
T
● heevent sourceis the object that generates an event.
● Examples of event sources in Java applets:
○ Buttons (
JButton )
○ Text fields (
JTextField
)
○ Applet’s graphical area (
Canvas
)
Event Listeners:
● T o handle events, applets must register an event listener, which is a Java interface
defining methods for handling specific events.
● Examples of event listeners:
○
MouseListener→Handles mouse events
○ KeyListener→Handles keyboard events
How event listeners work:
●
○ They listen for a specific event (e.g., button click).
○ When the event occurs, they call the corresponding handler method automatically.
Event Handling Process:
1. Registering Listeners:
○ The applet must register the appropriate listener(s) with the event source.
○ Example: Registering a MouseListenerto respond tomouse clicks on a
button.
2. Event Occurrence:
○ When an event occurs (e.g., the user clicks a button), the event source triggers the
event.
3. Listener Response:
○ The registered listener detects the event and invokes the corresponding
event-handling method.
4. Event Handling Method:
○ The applet implements the method(s) from the listener interface (e.g.,
mouseClicked()for mouse events).
○ The method performs the desired action such as:
■ Drawing a shape
■ Changing text
■ Displaying a message
CODE:
import java.awt.*;
import javax.swing.*;
public class SwingApplet {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Applet Programming Demonstration");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JButton button = new JButton("Click Me!");
button.addActionListener(e ->
J OptionPane.showMessageDialog(frame, "Hello, Applet!", "Message",
JOptionPane.INFORMATION_MESSAGE)
);
frame.add(button);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
OUTPUT:
PROGRAM: 10
AIM:Implement painting using MouseDragged() methodof MouseMotionlistener in Applet.
OVERVIEW:
J ava applets (though deprecated in modern browsers) were used in the past for creating graphical
user interface (GUI) applications embedded in web pages. They were commonly used for
interactive applications like games, drawing tools, and simulations.
I n this context, we will focus on creating a simple painting applet that allows users to draw on
the screen using mouse dragging.
mouseDragged()
Key Concepts in Applet Painting Using
1. Applet Basics:
● A n applet is a special Java program designed to be embedded in a web page and run
inside a browser.
● Due to security and compatibility issues, applets are now mostly obsolete.
● In an applet, methods likepaint(Graphics g)or update(Graphics g)are
used for drawing tasks.
MouseMotionListenerInterface:
2.
MouseMotionListenerinterface provides methodsto track mouse movements. The
he
T
two key methods are:
● mouseMoved(MouseEvent e)→ Triggered when the mousemoveswithout being
dragged.
● mouseDragged(MouseEvent e)→ Triggered when the mousemoves while the
mouse button is held down.
mouseDragged()in Action:
3.
● W mouseDragged()method is called
hen a userclicks and drags the mouse, the
continuously, providing thecurrent mouse position.
● By capturing thesemouse coordinatesand updatingthe graphics, we cansimulate a
drawing or painting effect.
4. Graphics Update:
● T he mouseDragged()method invokesdrawing operationsusing the
Graphics
object.
● The graphics can beupdated dynamicallyto draw:
○ Lines
○ Shapes
○ Custom graphics
paint(Graphics
● Typically, thedrawing happens inside the g)method, using
g.drawLine()
g.fillOval()
, , etc.
CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public class program10 extends JFrame {
private final List<Point> points = new ArrayList<>();
public program_10() {
setTitle("Applet Paint App");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
DrawingPanel drawingPanel = new DrawingPanel();
add(drawingPanel);
setVisible(true);
}
class DrawingPanel extends JPanel implements MouseMotionListener {
public DrawingPanel() {
addMouseMotionListener(this);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
for (Point p : points) {
g.fillRect(p.x, p.y, 4, 4);
}
}
@Override
public void mouseDragged(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(program10::new);
}
}
OUTPUT: