Advanced Java Programming Lab - Google Docs
Advanced Java Programming Lab - Google Docs
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.
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 {
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:
CODE:
import java.awt.*;
import javax.swing.*;
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
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
● 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.
mouseDragged()in Action:
3.
CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public program_10() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
add(drawingPanel);
setVisible(true);
}
class DrawingPanel extends JPanel implements MouseMotionListener {
public DrawingPanel() {
addMouseMotionListener(this);
}
@Override
super.paintComponent(g);
g.setColor(Color.BLACK);
}
}
@Override
points.add(e.getPoint());
repaint();
}
@Override
}
}
}
}
OUTPUT: