Java Applet & its life Cycle. By Iqtidar Ali

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:

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

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

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

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

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

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

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

SIMPLE APPLET PROGRAM

PROGRAMMING LANGUAGE 2

CSC System Development with Java Introduction to Java Applets Budditha Hettige

G51PRG: Introduction to Programming Second semester Applets and graphics

9. APPLETS AND APPLICATIONS

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

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,

CSD Univ. of Crete Fall Java Applets

Java - Applets. public class Buttons extends Applet implements ActionListener

CT 229 Arrays in Java

UNIT-2: CLASSES, INHERITANCE, EXCEPTIONS, APPLETS. To define a class, use the class keyword and the name of the class:

Contents 8-1. Copyright (c) N. Afshartous

CS335 Graphics and Multimedia

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

Java Applets / Flash

Introduction to Computer Science I

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

Object Oriented Programming Concepts-15CS45

Chapter 3 - Introduction to Java Applets

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Java TM Applets. Rex Jaeschke

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

CSC 1214: Object-Oriented Programming

Lecture Static Methods and Variables. Static Methods

Advanced Internet Programming CSY3020

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

Lecture Static Methods and Variables. Static Methods

Graphics Applets. By Mr. Dave Clausen

Chapter 12 Advanced GUIs and Graphics

SNS COLLEGE OF ENGINEERING, Coimbatore

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

Programming In Java Prof. Debasis Samanta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Applets as front-ends to server-side programming

Introduction to Java Applets 12

CSC 551: Web Programming. Fall 2001

Chapter 7 Applets. Answers

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

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

UNIT -1 JAVA APPLETS

An applet is called from within an HTML script with the APPLET tag, such as: <applet code="test.class" width=200 height=300></applet>

Java Programming Unit 6. Inner Classes. Intro to Apples. Installing Apache Tomcat Server.

Java. GUI building with the AWT

Prashanth Kumar K(Head-Dept of Computers)

The init() Method. Browser Calling Applet Methods

Introduction to the Java T M Language

Chapter #1. Program to demonstrate applet life cycle


User interfaces and Swing

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

CHAPTER 2. Java Overview

Graphics Applets. By Mr. Dave Clausen

DEMONSTRATION OF AWT CONTROLS

Sri Vidya College of Engineering & Technology

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

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

CS1004: Intro to CS in Java, Spring 2005

For live Java EE training, please see training courses at

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

Sun ONE Integrated Development Environment

PESIT Bangalore South Campus

SCHEME OF COURSE WORK

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

Programming graphics

JFrame & JLabel. By Iqtidar Ali

Week 9. Abstract Classes

Content. Client-Server Model. Server Roles

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

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

Core JAVA Training Syllabus FEE: RS. 8000/-

EE219 - Semester /2009 Solutions Page 1 of 10

Setup and Getting Startedt Customized Java EE Training:

UCLA PIC 20A Java Programming

EE219 Object Oriented Programming I (2007/2008) REPEAT SOLUTIONS

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Java Control Fundamentals

JAVA PROGRAM EXAMPLE WITH OUTPUT PDF

Graphical User Interface (GUI)

Component Based Software Engineering

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

EE219 Object Oriented Programming I (2005/2006) SEMESTER 1 SOLUTIONS

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

MODULE 8p - Applets - Trials B

Chapter 2 Using Objects. Types. Number Literals. A type defines a set of values and the operations that can be carried out on the values Examples:

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 OOP THROUGH JAVA (Common to CT, CM and IF)

Java History. Java History (cont'd)

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

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

Transcription:

Java Applet & its life Cycle By Iqtidar Ali

Java Applet Basic An applet is a java program that runs in a Web browser. An applet can be said as a fully functional java application. When browsing the Web, often the graphical user interface and animation you see are developed by the use of java.

Java Applet Basic An applet is a java class that extends the java.applet.applet class. They are executed either by Web browser or an Applet Viewer. Applets are designed to be embedded within an HTML page.

Java Applet Basic (Cont--) A JVM is required to view an applet. The JVM on the user's machine creates an instance of the applet class The JVM invokes various methods during the applet's lifetime.

Java Application VS Java Applet The way of writing about java application is same as java Applet. Applet and application shares many common features but still have some differences. Application must have main method while applet do not need that. Applet run in Web Browser environment bcoz they are invoked by Web Page.

Java Application VS Java Applet (Cont--) Java provide special features that enables applets to be run from a Web browser. The applet class is an AWT class and is not designed to work with Swing components. To use Swing component in java applet, you need to create a java applet that extends javax.swing.japplet, which is a subclass of java.applet.applet.

Life Cycle of an Applet

Life Cycle of an Applet Four methods in the Applet class give you the framework on which you build any serious applet. init method Start method Stop method Destroy method Paint method // all the above are from an java.applet

Life Cycle of an Applet (Cont--) The init method The init method is invoked after the applet is created. Initialization of methods and variables take place. It is called only once in the life cycle. The Start method The start method is invoked after the init method. It can be call any number of time. It is also called when the user returns to the Web page containing the applet after surfing other pages.

Life Cycle of an Applet (Cont--) The Stop method The stop method is the opposite of the start method. The start method is called when the user moves back to the page that contains the applet. The stop method is invoked when the user leaves the page. It can be called any number of time. The Destroy method The destroy method is invoked when the browser exits normally to inform the applet that it is no longer needed and should release any resources it has allocated. The stop method is always called before the destroy method. It perform memory clean up process.

Life Cycle of an Applet (Cont--) The Paint method 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. General form of paint() is public void paint(graphics g) { // drawstring() of Graphics The drawstring is a member of Graphics class.

import java.applet.*; import java.awt.*; Example public class W_firstapplet extends Applet { public void paint (Graphics g) { g.drawstring("welcome to java Applet World",100,50); /*<applet code= W_firstapplet.class width 1016 height=752> </applet> */

Applet Life Cycle Example

import java.awt.*; import java.applet.*; /* <applet code="appletskel" width=300 height=100> </applet> */ public class AppletSkel extends Applet { // Called first. public void init() { // initialization /* Called second, after init(). Also called whenever the applet is restarted. */ public void start() { // start or resume execution // Called when the applet is stopped. public void stop() { // suspends execution /* Called when applet is terminated. This is the last method executed. */ public void destroy() { // perform shutdown activities // Called when an applet's window must be restored. public void paint(graphics g) { // redisplay contents of window

import java.applet.*; import java.awt.*; public class FirstApp1 extends Applet { public void init() { System.out.println(" I m init"); public void start() { System.out.println(" I m start"); public void paint() { System.out.println(" I m paint"); public void stop() { System.out.println(" I m stop"); public void destory() { System.out.println(" I m destory");

import java.awt.*; import java.applet.*; /* This is HTML Code you have to implement it in HTML <applet code="sample" width=300 height=50> </applet> */ public class Sample extends Applet{ String msg; // set the foreground and background colors. public void init() { setbackground(color.blue); setforeground(color.white); msg = "Inside init( ) --"; // Initialize the string to be displayed. public void start() { msg += " Inside start( ) --"; // Display msg in applet window. public void paint(graphics g) { msg += " Inside paint( )."; g.drawstring(msg, 10, 30);

Example Applet import java.applet.*; import java.awt.*; public class HelloWorldApplet extends Applet { public void paint (Graphics g) { g.drawstring ("Hello World",25,50);

Implementing Applet in HTML <html> <body> <Applet code=testapp width=200 height=200> </Applet> </body> </html> Save the File named TestApp.html Run the file using browser or AppletViewer Appletviewer TestApp.html

import java.awt.*; import java.applet.*; public class OvalApp extends Applet { public void paint(graphics g) { g.setcolor(color.red); g.drawoval(40,40,160,160); g.setcolor(color.blue); g.drawoval(60,60,120,120); g.setcolor(color.green); g.drawoval(65,65,110,110); g.setcolor(color.red); g.drawstring("hello world",100,120);

HTML coding for implementing Applet <html> <body> <Applet code=ovalapp width=200 height=200> </Applet> </body> </html>

The End

Java Applet & its Control By Iqtidar Ali

Control Fundamentals The awt supports the following types of controls: Label Push buttons Check boxes Choice lists Lists Scroll bars Text editing

Button The most widely used control is the push button. A push button is a component that contains a label and that generates an event when it is pressed. Push buttons are objects of type Button.

Button (Cont--) Button defines these two constructors: Button( ):- The first version creates an empty button. Button(String):- The second creates a button that contains string as a label.

Button Applet import java.awt.*; public class week2_2 extends java.applet.applet{ public void init() { Button b1=new Button ("OK"); add(b1);

Text Fields Text Fields provide an area where one can edit or enter a single line of text. Text Fields are usually used for getting inputs from a user. It is created using any of the following constructor. TextField():- Blank TextField(int):- int indicate minimum no s of character to be displayed. TextField(String):- Welcome msg to be displayed by default. TextField(String, int):- Combination of two.

TextField Applet import java.awt.*; public class week2_2 extends java.applet.applet{ public void init() { TextField t1=new TextField(20); add(t1);

LABEL To include a control in a window, you must add it to the window To do this, you must first create an instance of the desired control Then add it to a window by calling add() The syntax is Label compobj=new label() add(compobj); Here, compobj is an instance of the control that you want to add.

Label The easiest control to use is a label A label is an object of type Label, and it contains a string, which it displays Labels are passive controls that do not support any interaction with the user Label defines the following constructors: -Label( ) :- create empty label Label(String):- create label with given string Label(String, int):- Label with string & alignment

Label import java.awt.*; public class week2_2 extends java.applet.applet{ public void init() { Label l1=new Label("Student Name"); add(l1);

Adding Three Label import java.awt.*; import java.applet.*; public class LabelDemo extends Applet { public void init() { Label one = new Label("One"); Label two = new Label("Two"); Label three = new Label("Three"); add(one); add(two); add(three); /* The sample output is shown here<applet code="labeldemo" width=300 height=200></applet>*/

CheckBox import java.awt.*; public class week2_2 extends java.applet.applet{ public void init() { Checkbox c1=new Checkbox ("Male"); add(c1); Checkbox c2=new Checkbox ("Female"); add(c2);

Combine all in one applet import java.awt.*; public class week2_2 extends java.applet.applet{ public void init() { Button b1=new Button ("OK"); TextField t1=new TextField(20); Label l1=new Label("Student Name"); add(b1); add(t1); add(l1);

The END