APPLETS

Vaishnavi
The STEM
Published in
5 min readNov 19, 2021

--

We’ll look at java applets in this blog, which are utilized in interactive online applications.
One sort of Java program is a console-based application, and another type of Java program is an applet. An applet is a type of Java program that runs in a browser that supports Java. On the client side, the JVM runs the applet. Applets are online apps that are interactive.

Syntax for Applet tag:

<APPLET [CODEBASE = codebaseURL] CODE= appletfile [ALT=alternate 
text] [NAME=applet-instance-name]
WIDTH =pixels HEIGHT= pixels [ALIGN=alignment]
[VSPACE=pixels] [HSPACE= pixels]>
[<PARAM NAME=attr-name VALUE=attr-value>]
[<PARAM NAME=attr-name VALUE=attr-value>]
...
</APPLET>

CODEBASE: Its not compulsory or mandatory attribute that is used to specify the base URL of Applet code, which is the applet executable class file.

CODE: This attribute gives the name of the file which is having the applet complied class file.

ALT: It is also an optional attribute which is used to give a short messages that can displayed in the browser when it can understand its a applet tag but cannot run it.

NAME: This attribute is used to specify name for an applet instance.

WIDTH & HEIGHT: Used to specify the size of applet display area.

ALIGN: specifies the alignment of the applet(position of the applet).

VSPACE & HSPACE: VSPACE(Vertical Spaces) specifies the space in pixels, above and below the applet. HSPACE(Horizontal Spaces) specifies the space in pixels, on each side of the applet.

PARAM NAME AND VALUE: this tag allows you to specify applet specific arguments in the HTML page

Applets are of 2 types

  1. AWT Applets
  2. Swing Applets

Sample Applet Program:

import java.awt.*;
import java.applet.*;
/*<applet code="simpleApplet" width=250 height=200
*</applet>
*/
public class simpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello World", 50, 50);
}
}

Output:

eclipse output done by author

Note: Applets will not have main() method and they must be under applet viewer or a java enabled web browser.

Life cycle of an Applet:

  1. Applet is initialized
  2. Applet is started
  3. Applet is Painted
  4. Applet is stopped
  5. Applet is destroyed

Methods in Applet life cycle are

java.applet.Applet class:

  1. public void init(): used to initialize the applet and is invoked only once.
  2. public void start(): used to start an applet and is invoked after the init method.
  3. public void stop(): used to stop the applet and is invoked when applet is stopped or browser is minimized.
  4. public void destroy(): used to destroy the applet and is invoked only once.

java.awt.Component class:

  1. public void paint(Graphic g): used to paint the applet and provides graphic class objects can be used for drawing some shapes like rectangle, oval..

Sample Code by using all methods:

import java.awt.*;
import java.applet.*;
/*
<applet code="SampleCode" width=300 height=100>
</applet>
*/
public class SampleCode extends Applet {
public void init() {
// initialization
}
public void start() {
// start or resume execution
}
public void stop() {
// suspends execution
}
public void destroy() {
// perform shutdown activities
}
public void paint(Graphics g) {
// redisplay contents of window
}
}

Advantages of Applets:

  • It is secure and takes less time to execute.
  • Used to create dynamic web pages.
  • Provides interactive features to web pages.
  • Writing and displaying with the opening of browser, graphics and animations is easier than applications.

Disadvantages/ Restrictions of Applets:

  • It can’t read some system properties.
  • Some Browsers like Google Chrome, Safari, Opera will not support Java Applets.
  • Applets will not access client side data like files, OS.

AWT Applets:

  • AWT supports window based, graphical interface.
  • AWT class can be accessed from java.awt package it is one of the largest package in java packages.
  • Some AWT Class are:
  • Button (creates a push button control)
  • Canvas( create a empty window)
  • checkbox( create a check box control)
  • Choice( creates a pop-up list)
  • Dialog(creates a dialog window)
  • Event (Encapsulates the event)
  • Frame(creates a standard window that has a title bar, resize corners and a menu bar )
  • Graphics( encapuslating the graphics context)
  • Window( creates a window with no frame, no menu bar, and no title)
  • Label ( creates a label that displays a string)
  • GridLayout( it displays components in a 2-dimentional grid.)
  • TextField( creates a single-line edit control)
  • TextArea( creates a multi-line edit control)
  • Panel( the simplest concrete subclass of container)

Example Program:

import java.awt.*;  
public class AwtDemo extends Frame {
AwtDemo (){
Label firstName = new Label("First Name");
firstName.setBounds(20, 50, 80, 20);
Label lastName = new Label("Last Name");
lastName.setBounds(20, 80, 80, 20);
Label dob = new Label("Date of Birth");
dob.setBounds(20, 110, 80, 20);
TextField firstNameTF = new TextField();
firstNameTF.setBounds(120, 50, 100, 20);
TextField lastNameTF = new TextField();
lastNameTF.setBounds(120, 80, 100, 20);
TextField dobTF = new TextField();
dobTF.setBounds(120, 110, 100, 20);
Button sbmt = new Button("Submit");
sbmt.setBounds(20, 160, 100, 30);
Button reset = new Button("Reset");
reset.setBounds(120,160,100,30);
add(firstName);
add(lastName);
add(dob);
add(firstNameTF);
add(lastNameTF);
add(dobTF);
add(sbmt);
add(reset);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AwtDemo awt = new AwtDemo ();
}
}

OUTPUT:

eclipse output done by author

SWING Applets:

  • Swing is a set of classes that provide more flexible and powerful components and it contains some extra features like Scroll panes, trees, tables and tabbed panes.
  • It is a part of Java Foundation classes (JFC), which is used to create window based applications.
  • It is built on the top of AWT API and written in java.
  • Swing class can be accessed from javax.swing.package.
  • Some Swing Classes are:
  • JButton
  • JTextField
  • JTextArea
  • JRadioButton
  • JCheckbox
  • JMenu
  • JColorChooser

Example Program:

import javax.swing.*; 
public class SwingDemo{
public static void main(String[] args) {
JFrame f=new JFrame();
JButton b=new JButton("clickable button");
b.setBounds(100,100,150, 40);
f.add(b);
f.setSize(400,500);
f.setLayout(null);
f.setVisible(true);
}
}

OUTPUT:

eclipse output done by author

Did you like the blog? Clap it! Let us know your views on blog by commenting.

End of this blog..

--

--