Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applets In Java Visit for more Learning Resources 1.

Similar presentations


Presentation on theme: "Applets In Java Visit for more Learning Resources 1."— Presentation transcript:

1 Applets In Java Visit for more Learning Resources 1

2 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

3 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

4 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

5 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

6 Applet hierarchy 6

7 Applet life cycle 7

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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


Download ppt "Applets In Java Visit for more Learning Resources 1."

Similar presentations


Ads by Google