Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Advanced Java Programming Lab - Google Docs

The document outlines Java applet programming, detailing its historical context, usage for interactive web applications, and eventual deprecation due to security issues. It includes examples of Java code for creating applets, demonstrating event handling and painting using mouse events. The document emphasizes the transition from applets to modern web technologies and provides sample code for basic applet functionality.

Uploaded by

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

Advanced Java Programming Lab - Google Docs

The document outlines Java applet programming, detailing its historical context, usage for interactive web applications, and eventual deprecation due to security issues. It includes examples of Java code for creating applets, demonstrating event handling and painting using mouse events. The document emphasizes the transition from applets to modern web technologies and provides sample code for basic applet functionality.

Uploaded by

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

‭PROGRAM: 8‬

‭AIM:‬‭Write a Java program to demonstrate applet programming.‬

‭ VERVIEW:‬‭Java applets were small applications written‬‭in 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 be‬‭platform-independent‬‭,‬‭meaning they could run on any‬
‭operating system that had a‬‭Java Virtual Machine (JVM)‬‭installed.‬
‭●‬ ‭They were often used for‬‭interactive web applications,‬‭games, charts, scientific‬
‭simulations,‬‭and‬‭multimedia presentations‬‭on websites.‬

‭How They Worked:‬

<applet>‬‭tag (in older browsers) or‬


‭●‬ ‭Applets were embedded in‬‭HTML pages‬‭using the‬‭
‭object>‬‭tag.‬
<
‭ ‬ ‭The browser would download the applet and run it inside a special‬‭"applet viewer"‬‭or‬

‭applet container‬‭provided by the JVM.‬
‭●‬ ‭Applets had‬‭restricted access to system resources‬‭for security reasons, operating in a‬
‭"sandbox" environment‬‭.‬

‭End of Java Applets:‬

‭‬ A
● ‭ s of‬‭2015‬‭, Oracle officially‬‭deprecated Java applets‬‭and the‬‭NPAPI plugin‬‭.‬
‭●‬ ‭By‬‭2017‬‭, most browsers stopped supporting them entirely.‬
‭●‬ ‭Java applets are now considered‬‭obsolete‬‭in favor‬‭of 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 prints‬‭a 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 are‬‭event-driven‬‭, meaning they do not‬‭run continuously in a loop but‬
‭instead‬‭respond to user actions‬‭such as:‬
‭○‬ ‭Mouse movements, clicks, and drags‬
‭○‬ ‭Keyboard presses‬
‭○‬ ‭System-generated events‬‭(e.g., window resizing)‬
‭●‬ ‭When an event occurs, the‬‭appropriate event handler‬‭method‬‭is called automatically.‬

‭Event Sources:‬

‭‬ T
● ‭ he‬‭event source‬‭is 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‬‭ MouseListener‬‭to respond to‬‭mouse 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() method‬‭of 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 like‬‭paint(Graphics g)‬‭or‬‭ update(Graphics‬‭ g)‬‭are‬
‭used for drawing tasks.‬

MouseMotionListener‬‭Interface:‬
‭2.‬‭

MouseMotionListener‬‭interface provides methods‬‭to track mouse movements. The‬


‭ he‬‭
T
‭two key methods are:‬

‭●‬ m‭ouseMoved(MouseEvent e)‬‭→ Triggered when the mouse‬‭moves‬‭without being‬


‭dragged‬‭.‬
‭●‬ ‭mouseDragged(MouseEvent e)‬‭→ Triggered when the mouse‬‭moves while the‬
‭mouse button is held down‬‭.‬

mouseDragged()‬‭in Action:‬
‭3.‬‭

‭●‬ W mouseDragged()‬‭method is called‬


‭ hen a user‬‭clicks and drags the mouse‬‭, the‬‭
‭continuously‬‭, providing the‬‭current mouse position‬‭.‬
‭●‬ ‭By capturing these‬‭mouse coordinates‬‭and updating‬‭the graphics, we can‬‭simulate a‬
‭drawing or painting effect‬‭.‬
‭4. Graphics Update:‬

‭●‬ T ‭ he‬‭ mouseDragged()‬‭method invokes‬‭drawing operations‬‭using the‬‭


Graphics‬
‭object‬‭.‬
‭●‬ ‭The graphics can be‬‭updated dynamically‬‭to draw:‬
‭○‬ ‭Lines‬
‭○‬ ‭Shapes‬
‭○‬ ‭Custom graphics‬
paint(Graphics‬‭
‭●‬ ‭Typically, the‬‭drawing 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:‬

You might also like