ASSIGNMENT NO 14. Objectives: To learn and demonstrated use of applet and swing components

Similar documents
An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Java Applet Basics. Life cycle of an applet:

SNS COLLEGE OF ENGINEERING, Coimbatore

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Part 3: Graphical User Interface (GUI) & Java Applets

Graphical User Interfaces (GUIs)

Introduction. Introduction

A socket is a software endpoint that establishes bidirectional communication between a server program and one or more client programs.

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CSD Univ. of Crete Fall Java Applets

Swing from A to Z Some Simple Components. Preface

Introduction to Computer Science I

G51PRG: Introduction to Programming Second semester Applets and graphics

Day before tests of Java Final test. IDM institution of Bandarawela. Project for department of education

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Java. GUI building with the AWT

CSC 1214: Object-Oriented Programming

SINGLE EVENT HANDLING

JAVA PROGRAMMING COURSE NO. IT-705(A) SECTION - A

Java Applet & its life Cycle. By Iqtidar Ali

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Java History. Java History (cont'd)

SD Module-1 Advanced JAVA

Chapter #1. Program to demonstrate applet life cycle

SD Module-1 Advanced JAVA. Assignment No. 4

Java Applets This is not a Java course! (You re supposed to know about Java.)

Java - Applets. public class Buttons extends Applet implements ActionListener

PROGRAMMING LANGUAGE 2

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

MULTIMEDIA PROGRAMMING IN JAVA. Prof.Asoc. Alda Kika Department of Informatics Faculty of Natural Sciences University of Tirana

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Java Applets / Flash

JApplet. toy example extends. class Point { // public int x; public int y; } p Point 5.2. Point. Point p; p = new Point(); instance,

Module 5 Applets About Applets Hierarchy of Applet Life Cycle of an Applet

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

User interfaces and Swing

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Original GUIs. IntroGUI 1

9. APPLETS AND APPLICATIONS

CS2 Advanced Programming in Java note 8

Overview. Applets. A Java GUI inside your browser! Important methods Drawing images Playing audio Getting input parameters Double buffering

Introduction to the JAVA UI classes Advanced HCI IAT351

SELF-STUDY. Glossary

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Component Based Software Engineering

1. What is Jav a? simple

Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

Graphic User Interfaces. - GUI concepts - Swing - AWT

NRI INSTITUTE OF TECHNOLOGY

Graphical User Interface (GUI)

Programming graphics

(Refer Slide Time: 02:01)

Swing. By Iqtidar Ali

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Chapter 1 GUI Applications

Outline. Introduction to Java. What Is Java? History. Java 2 Platform. Java 2 Platform Standard Edition. Introduction Java 2 Platform

Outline. Announcements. Feedback. CS1007: Object Oriented Design and Programming in Java. Java beans Applets

Applet. 1. init (): called once by the applet containers when an applet is loaded for execution.

Lecture 5: Java Graphics

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Lecture Static Methods and Variables. Static Methods

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Unit 1- Java Applets. Applet Programming. Local Applet and Remote Applet ** Applet and Application

PESIT Bangalore South Campus

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CHAPTER 2. Java Overview

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

To gain experience using GUI components and listeners.

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

MIT AITI Swing Event Model Lecture 17

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Core JAVA Training Syllabus FEE: RS. 8000/-

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

CS 251 Intermediate Programming GUIs: Components and Layout

CSC System Development with Java Introduction to Java Applets Budditha Hettige

Summary. 962 Chapter 23 Applets and Java Web Start

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

JAVA Swings Sample Codes CODE NO: EduLanka Sample Java Codes - ONLINE JAVA

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

Packages: Putting Classes Together

Review. Designing Interactive Systems II. Introduction. Web 2.0 in keywords GWT Cappuccino HTML5. Cross platform GUI Toolkit

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Designing Interactive Systems II

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

1 GUI GUI GUI GUI GUI. GUI(Graphical User Interface) JDK java.awt. ? Component

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Java Programming Lecture 6

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

PESIT Bangalore South Campus

Programming Language Concepts: Lecture 8

GUI Forms and Events, Part II

Merge Sort Quicksort 9 Abstract Windowing Toolkit & Swing Abstract Windowing Toolkit (AWT) vs. Swing AWT GUI Components Layout Managers Swing GUI

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Transcription:

Create an applet with three text Fields and four buttons add, subtract, multiply and divide. User will enter two values in the Text Fields. When any button is pressed, the corresponding operation is performed and the result is displayed in the third Text Fields. ASSIGNMENT NO 14 Title: Demonstration of Applet and basic Swing components Objectives: To learn and demonstrated use of applet and swing components Problem Statement: Create an applet with three text Fields and four buttons add, subtract, multiply and divide. User will enter two values in the Text Fields. When any button is pressed, the corresponding operation is performed and the result is displayed in the third Text Fields. Outcomes: 1) Students will be able to demonstrate applet 2) Students will be able to demonstrate basic swing components Hardware requirements: Any CPU with Pentium Processor or similar, 256 MB RAM or more, 1 GB Hard Disk or more. Software requirements: 64 bit Linux/Windows Operating System, JDK 1.4 or later Theory: An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal. There are some important differences between an applet and a standalone Java application, including the following An applet is a Java class that extends the java.applet.applet class. A main() method is not invoked on an applet, and an applet class will not define main(). Applets are designed to be embedded within an HTML page. When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine. A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.

The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime. Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed. Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file. Life Cycle of an Applet Four methods in the Applet class gives you the framework on which you build any serious applet init This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed. start This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages. stop This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet. destroy This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet. paint Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt. Advantage of Applet There are many advantages of applet. They are as follows: o o It works at client side so less response time. Secured

o It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc. Drawback of Applet o Plugin is required at client browser to execute applet. Invoking an Applet An applet may be invoked by embedding directives in an HTML file and viewing the file through an applet viewer or Java-enabled browser. The <applet> tag is the basis for embedding an applet in an HTML file. JApplet class in Applet As we prefer Swing to AWT. Now we can use JApplet that can have all the controls of swing. The JApplet class extends the Applet class. Event Handling Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism has a code which is known as an event handler, that is executed when an event occurs. Java uses the Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the events. The Delegation Event Model has the following key participants. Source The source is an object on which the event occurs. Source is responsible for providing information of the occurred event to it's handler. Java provide us with classes for the source object. Listener It is also known as event handler. The listener is responsible for generating a response to an event. From the point of view of Java implementation, the listener is also an object. The listener waits till it receives an event. Once the event is received, the listener processes the event and then returns. The benefit of this approach is that the user interface logic is completely separated from the logic that generates the event. The user interface element is able to delegate the processing of an event to a separate piece of code.

In this model, the listener needs to be registered with the source object so that the listener can receive the event notification. This is an efficient way of handling the event because the event notifications are sent only to those listeners who want to receive them. Steps Involved in Event Handling Step 1 The user clicks the button and the event is generated. Step 2 The object of concerned event class is created automatically and information about the source and the event get populated within the same object. Step 3 Event object is forwarded to the method of the registered listener class. Step 4 The method is gets executed and returns. Conclusion : In this assignment, we have studied and demonstrated applet, event handling and swing basic components. Roll No. Name Student of Date of Performance Date Assessment of Grade Sign of Student Sign Faculty of PROGRAM ************************CalculatorApplet.java********************************* import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CalculatorApplet extends JApplet implements ActionListener{ JTextField num1,num2,num3; JLabel lnum1,lnum2,lnum3; JButton add1,sub,mul,div,mod,clear; public void init() { getcontentpane().setlayout(new FlowLayout()); lnum1=new JLabel("First Number"); lnum2=new JLabel("Second Number"); lnum3=new JLabel("Answer "); num1=new JTextField("0.0",10); num2=new JTextField("0.0",10);

num3=new JTextField("0.0",22); num3.setenabled(false); add1=new JButton("+"); sub=new JButton("-"); mul=new JButton("*"); div=new JButton("/"); mod=new JButton("%"); clear=new JButton("Reset"); add1.addactionlistener(this); sub.addactionlistener(this); mul.addactionlistener(this); div.addactionlistener(this); mod.addactionlistener(this); clear.addactionlistener(this); getcontentpane().add(lnum1); getcontentpane().add(num1); getcontentpane().add(lnum2); getcontentpane().add(num2); getcontentpane().add(lnum3); getcontentpane().add(num3); getcontentpane().add(add1); getcontentpane().add(sub); getcontentpane().add(mul); getcontentpane().add(div); getcontentpane().add(mod); getcontentpane().add(clear); } public void actionperformed(actionevent e) { Object o=e.getsource(); double n1=0,n2=0,ans=0; n1=double.parsedouble(num1.gettext()); n2=double.parsedouble(num2.gettext()); if(o==add1) ans=n1+n2; else if(o==sub) ans=n1-n2; else if(o==mul) ans=n1*n2; else if(o==div) ans=n1/n2; else if(o==mod) ans=n1%n2; num3.settext(string.valueof(ans));

if(o==clear) { num1.settext("0.0"); num2.settext("0.0"); num3.settext("0.0"); } } } *********************************calciapplet.html******************************* <html> <head> <title>calculator Applet</title> </head> <body> <applet code="calculatorapplet.class" width=750 height=400> </applet> </body> </html> OUTPUT