Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Applet in Java

  • An applet, a Java programme, can be added to your website. Within a web browser, this applet runs client-side. You can host the applet on a web server and embed it directly into an HTML page by using the object or applet tag. This gives your website a lively and enjoyable touch.
    • Every applet is a subclass of the java.applet.Applet class. Applets don't work on their own; they run inside an applet viewer or a web browser. The applet viewer is a common tool provided by JDK.
    • Unlike regular programs that start from the main() method, applets have a different way of starting to run.
Table Of Content

  • Applet in Java
  • Life cycle of an applet
  • Applet Example
  • How to run an Applet
  • Applets' advantages over HTML
  • Restrictions imposed on Java applets


  • Instead of using System.out.println for displaying output in the applet window, AWT methods like drawString() are used.
  • Your website can be made more interactive and interesting for your visitors by using java applets.





Hierarchy of Applet in Java



Life cycle of an applet



  • In Java applet there are 4 life cycle methods for applets in the applet class, and 1 life cycle methods for applets in the java.awt.Component class.

    java.applet.Applet class

    • void init() : The init() method is the first one to be called when your applet starts. Its main purpose is to initialize variables. It's essential to note that this method is executed only once during the entire runtime of your applet.
    • void start() : After init(), the start() method is called. It acts as a way to restart the applet after it has been stopped. One thing to keep in mind is that start() gets called every time the applet's HTML document is displayed on the screen. On the other hand, init() is only triggered once when the applet is initially loaded. This means that if a user leaves the web page and returns, the applet resumes from the beginning.




    • void stop() : When a web browser navigates away from the HTML document containing the applet, such as when it changes to another page, the stop() method is invoked. It is likely that the applet is running when stop() is called. When the applet is not visible, threads that are unnecessary to run should be suspended using the stop() function. If the user revisits the page, you can restart them when start() is called.
    • void destroy() : When your applet needs to be removed entirely from memory, the destroy() method is called by the environment. At this point, you should release any resources that the applet might be using. Prior to destroy(), the stop() method is always invoked.

    java.awt.Component class

    • paint() : The Applet has been painted using paint (Graphics Gra). It offers objects of the Graphics class, which can be used to draw shapes like arcs, rectangles, ovals and circle

Simple example of Applet by html file

import java.applet.Applet;
import java.awt.Graphics;
public class Main extends Applet{
    @Override    // Overriding paint() method
    public void paint(Graphics g){
        g.drawString("Hello India", 40, 40);
    }    
}

html file

<html>  
<body>  
     <applet 
       code="Main.class" width="250" height="250">  
     </applet>  
   </body>  
</html>  





How to run an Applet?

There are two accepted methods for running an applet.

  • In a web browser that supports Java.
  • Using an applet viewer like the common tool applet-viewer. This is usually the quickest and simplest way to test your applet.

Simple example of Applet by appletviewer tool

import java.applet.Applet;
import java.awt.Graphics;
public class Main extends Applet{
	@Override // Overriding paint() method
	public void paint(Graphics g){
	   g.drawString("Hello India", 40, 40);
	} 
}
/* <applet code="Main.class" width=250 height=250> </applet> */




Output:

Hello India 


Applets' advantages over HTML

  • Dynamic Web Pages: Applets enable the display of dynamic web pages, making the user experience more interactive and engaging.
  • Audio Integration: Applets allow seamless integration of audio files into web applications, enhancing multimedia capabilities.
  • Document Presentation: With applets, documents can be presented in a more interactive and user-friendly manner.
  • Animation Playback: Applets facilitate smooth playback of animations, adding visual appeal to web pages.


Restrictions imposed on Java applets

  • No Native Methods or Libraries: Applets cannot define native methods or load external libraries, restricting their access to certain functionalities.
  • Limited File Access: Typically, applets cannot read or write to files on the host system, ensuring security and privacy.
  • Limited File Access: Typically, applets cannot read or write to files on the host system, ensuring security and privacy.
  • Network Connection Limitation: Applets can only connect to the host they originated from when establishing network connections.
  • No Execution of Host Programs: Applets cannot start any running program on the host system, preventing potential security risks.

Animation in Applet Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.