1 / 20

Applets In Java

Applets In Java. Visit for more Learning Resources. 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

joycegraham
Download Presentation

Applets In Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Applets In Java Visit for more Learning Resources

  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.

  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.

  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.

  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.

  6. Applet hierarchy

  7. Applet life cycle

  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)

  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

  10. Applet tag in HTML

  11. Attribute of APPLET tag

  12. Applets

  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.

  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.

  15. Different graphics class methods

  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)

  17. Add image in the applet

  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.

  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.

  20. Serialzation Example For more detail contact us

More Related