Applets In Java Visit for more Learning Resources 1.

Slides:



Advertisements
Similar presentations
1 Applets Programming Enabling Application Delivery Via the Web.
Advertisements

Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
JAVA APPLETS By: Ms. Humaira Siddiqui. Java and the Internet Java is considered to be a language well suited to be used in the internet. In contrast with.
© 2007 Lawrenceville Press Slide 1 Chapter 2 HTML An example HTML document Hello world!
Internet Software Development Applets Paul J Krause.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
Applets.
JAPPLET.
9/21/99www.cs.vt.edu/wwtut/1 User Interface Programming In Java Part 1 – Introduction Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
User-defined parameters can be passed to an applet using the tags.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
1 CSC Computer Education (P) Ltd. DESIGNED BY K PRAKASH,
Java Applets 1. What is an applet? An applet is a small Java program that is typically embedded in a Web page and can be run using the applet viewer or.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Java applets. Outline What is Java? Java Applications Java Applets Java Applets Securities Summary.
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
1 Java Applet Basics Chapter Eight. 2 Applets vs. Applications l Applications: Stand alone Java programs run by interpreter l Applets WWW browser embedded.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Applet: An applet is a java program that is transmitted over the network from the server to client & executed within clients browser. Applets are used.
Sachin Malhotra Saurabh Choudhary
User Interface Programming In Java
Object Serialization in Java
Object Oriented Programming
RMI Packages Overview java.rmi java.rmi.server java.rmi.registry
JAVA Applets Pavan D.M..
Java Applet.
Graphics Applets By Mr. Dave Clausen
30 Java Applets.
Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
UNIT-5.
Java Applets.
Introduction to Applet, Application and JDK
Java applets 1/3/2019.
APPLETS.
Applet in Java.
Java Programming COMP-417 Applet
Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run.
Handout-14 Applets and Graphics
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Enabling Application Delivery Via the Web
APPLET PROGRAMMING.
Presentation transcript:

Applets In Java Visit for more Learning Resources 1

Applets Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side. Advantage of Applet It works at client side so less response time. Secured It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os etc. 2

Applets Lifecycle of Java Applet Applet is initialized. Applet is started. Applet is painted. Applet is stopped. Applet is destroyed. Lifecycle methods for Applet: The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet. 3

Applets java.applet.Applet class For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet. public void init(): is used to initialized the Applet. It is invoked only once. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized. public void destroy(): is used to destroy the Applet. It is invoked only once. java.awt.Component class The Component class provides 1 life cycle method of applet. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc. 4

Applets Applet code uses services of two classes Applet and graphics from java java.applet.Applet class , provides init(),start() &paint() Syntax: public void paint(Graphics g) Graphics parameter is required (output may be text,graphics,sound) Graphics class is present in awt package Applet execution: applet file is main class for the applet after loading applet java creates instance of this class and then series of applet class methods are called. 5

Applet hierarchy 6

Applet life cycle 7

Applet life cycle 1 initialization state It is loaded first by calling init() method of applet Create object needed by applet class Set up intial values Load images or fonts Set up colors Occurs only once 2 Running state. Automatically after initialization of applet Public void start() 3.Idle state: Public void stop() 4. Dead state: Public void destroy() 5. Display state; public void paint(Graphics g) 8

Applet tags <APPLET..> and </APPLET> : name of applet to be loaded and tells browser how much space required. <applet code="First.class" width="300" height="300"> </applet>  Name of applet Width of applet Height of applet Adding applet to HTML file 9

Applet tag in HTML <html> <head> <title>java applet</title> </head> <body> <p>adding applet</p> <CENTER> <applet code="First.class" width="300" height="300">  </applet>  </CENTER> </body> </html> 10

Attribute of APPLET tag meaning CODE AppletFile This REQUIRED attribute gives the name of the file that contains the applet's compiled Applet subclass WIDTH = pixels HEIGHT = pixels These REQUIRED attributes give the initial width and height (in pixels) of the applet display area ALIGN = alignment This OPTIONAL attribute specifies the alignment of the applet : left,right,.. VSPACE = pixels HSPACE = pixels These OPTIONAL specify the number of pixels above and below the applet (VSPACE) and on each side of the applet (HSPACE) <PARAM NAME = appletAttribute1 VALUE = value> Pass parammeter ALT = alternateText This OPTIONAL attribute specifies any text that should be displayed if the browser 11

Applets //First.java import java.applet.Applet; import java.awt.Graphics;   public class First extends Applet{   public void paint(Graphics g){   g.drawString("welcome",150,150);   }  }   /* <applet code="First.class" width="300" height="300"> </applet> */   c:\>javac First.java c:\>appletviewer First.java OR html file saved by other name with .html extension <html>   <body>   <applet code="First.class" width="300" height="300">   </applet>   </body>   </html>   12

Different graphics class methods public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and (x2, y2). public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used draw a circular or elliptical arc. public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used to fill a circular or elliptical arc. public abstract void setColor(Color c): is used to set the graphics current color to the specified color. public abstract void setFont(Font font): is used to set the graphics current font to the specified font. 13

Different graphics class methods public abstract void drawString(String str, int x, int y): is used to draw the specified string. public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and height. public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the default color and specified width and height. public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the specified width and height. public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the default color and specified width and height. 14

Different graphics class methods import java.applet.*;   import java.awt.*;      public class ADemo extends Applet {     public void paint(Graphics g){   g.setColor(Color.red);   g.drawString("Welcome",50, 50);   g.drawLine(20,30,20,300);   g.drawRect(70,100,30,30);   g.fillRect(170,100,30,30);   g.drawOval(70,200,30,30);   g.setColor(Color.pink);   g.fillOval(170,200,30,30);   g.drawArc(90,150,30,30,30,270);   g.fillArc(270,150,30,30,0,180);   }   15

Get parameter of applet We can get any information from the HTML file as a parameter. For this purpose, Applet class provides a method named getParameter() public String getParameter(String PName) import java.applet.*;   import java.awt.*;   public class UseParam extends Applet{   public void paint(Graphics g){   String str=getParameter("msg"); g.drawString(str,50, 50);   }  }   <html>   <body>   <applet code="UseParam.class" width="300" height="300">   <param name="msg" value="Welcome to applet">   </applet>   </body>   </html>   16

Add image in the applet import java.awt.*; import java.applet.*;    public class DisplayImage extends Applet {     Image picture;     public void init() {       picture = getImage(getDocumentBase(),"sonoo.jpg");     }        public void paint(Graphics g) {       g.drawImage(picture, 30,30, this);          /*<applet code="DisplayImage.class" width="300" height="300">*/ this-> is ImageObserver objectThe Component class implements ImageObserver interface 17

Serialzation Serialization in java is a mechanism of writing the state of an object into a byte stream. It is mainly used in Hibernate, RMI, JPA, EJB and JMS technologies. The reverse operation of serialization is called de- serialization. ObjectOutputStream class :The ObjectOutputStream class is used to write primitive data types and Java objects to an OutputStream. Only objects that support the java.io.Serializable interface can be written to streams. public final void writeObject(Object obj) throws IOException {} : writes the specified object to the ObjectOutputStream. 18

Serialzation ObjectInputStream class: An ObjectInputStream deserializes objects and primitive data written using an ObjectOutputStream. 1) public final Object readObject() throws IOException, ClassNotFoundException{}: reads an object from the input stream. 2) public void close() throws IOException {}: closes ObjectInputStream. 19

Serialzation Example import java.io.Serializable; class Person implements Serializable{ int id; String name; Person(int id, String name) { this.id = id; this.name = name; } import java.io.*; class Persist{ public static void main(String args[])throws Exception{ Student s1 =new Student(211,"ravi"); FileOutputStream fout=new FileOutputStream("f.txt"); ObjectOutputStream out=new ObjectOutputStream(fout); out.writeObject(s1); out.flush(); System.out.println("success"); } } For more detail contact us 20