JaiswalTraining

Get the online training


Get Best Tutorial on all major programing language



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Applet:
  1. Java program that can be inserted into a web page and run by loading that page in a browser
  2. Brings web pages to life with interactive content, multimedia, games, and more
  3. The feature of Java that is primarily responsible for its initial popularity
  4. Users can run applets simply by  visiting a web page that contains an applet program  
    if they have the Java runtime environment installed on their computer
  5. Applets are reliant on browser's Java Virtual Machine to run


Implementation

A top-level container, like a JFrame behaves more like a JPanel

class javax.swing.JApplet


  • java.lang.Object
    • java.awt.Component
      • java.awt.Container
        • java.awt.Panel
          • java.applet.Applet
            • javax.swing.JApplet




problem: most web browsers (MS IE, Netscape, etc.) only have Java v1.1.8 installed as their Java VM

solution 1: only use Java classes/features that have been around since v1.1.8 (blech!)
solution 2: use Java Plug-in to give browser ability to use newer Java features


Applet life cycle

   Applet lofe cycle consists of the following steps

  • public void init()
  • public void start()
  • public void paint(Graphics g)
  • public void stop()
  • public void destroy()

  • init()  
when the browser loads the applet init method is called.
It initilases the applet.
  •  start()
browser calls init on that applet, once
browser calls start on that applet

browser goes away from that page
browser calls stop on that applet                                    

browser comes back to that page
browser calls start again on that applet
...
browser shuts down
browser calls destroy on the applet, onc

Example:-

import java.applet.Applet;

public class applet2 extends Applet {

  public void init() {
    System.out.println("init called");
  }

  public void start() {
    System.out.println("start called");
  }

  public void stop() {
    System.out.println("stop called");
  }

  public void destroy() {
    System.out.println("destroy called");
  }
}
//<applet code="applet2" width="200" height="200"></applet>





appletviewer  applet2.java

init called
start called
stop called
destroy called
Download code

Applet Graphical Example

Example:-1

import java.awt.Label;
import java.awt.Color;
import java.awt.TextField;
import java.applet.Applet;
//<applet code="applet1" width=200 height=200></applet>
public class applet1 extends Applet {

  public void init() {
    Label l = new Label("name");
    TextField t = new TextField(11);
    setBackground(Color.red);
    add(l);
    add(t);
  }
}

Output:-


Download code

Example:2

import java.applet.Applet;
import java.awt.Color;
import java.awt.Label;
// <applet code="AppletDemo" width=100 height=200>
//</applet>
public class AppletDemo extends Applet {

  public void init() {
    setBackground(Color.lightGray);
    for (int i = 1; i <=10; i++) {
      add(new Label("Label "+i));
    }
  }

}






AppletDemo.html
<html>
<body>

<applet code="AppletDemo" width=100 height=200>
</applet>

</body>
</html>

On Browser


Download code
Download html