0% found this document useful (0 votes)
16 views

Applet Unit 2

Applet Notes
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Applet Unit 2

Applet Notes
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

APPLET

 An applet is a Java program that runs in a Web browser.

 It runs inside the browser and works at client side.

 Applets are client side means they execute in your browser. When you
open a webpage or an url containing applet it fetches the applet from
server but it does not executes on server, it executes in your browser.

 Applets are used to make the web site more dynamic and entertaining.
HIERARCHY OF APPLET
LIFECYCLE OF APPLET
1.public void init(): is used to initialized the Applet. It is invoked only
once.

2.public void start(): is invoked after the init() method or browser is


maximised. It is used to start the Applet.

3.public void stop(): is used to stop the Applet. It is invoked when Applet
is stop or browser is minimized.

4.public void destroy(): is used to destroy the Applet. It is invoked only


once.

5.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.
Who is responsible to manage the life cycle of an
applet?
Java Plug-in software. The Java plugin is part of the Java
Runtime Environment (JRE) and allows a browser to work
with the Java platform to run Java applets to execute in the
browser. The Java Plugin is a special way to run Applets.
AWT
 AWT stands for abstract window toolkit.
 AWT is an API to develop GUI or window-based applications in
java.
 A GUI is built of graphical elements called components.
Typical components include such items as buttons,
scrollbars, and text fields.
 The container class is a subclass of component.
 In java GUI can be design using some predefined classes.All
these classes are defined in java.awt package.
COMPONENT CLASS
• The Component class is the superclass of all components. A component
class can be linked with a page, components of web applications.
Component clearly shows that is the graphical representation of an
Object.

CONTAINER
• A Container is a subclass of Component that can contain other
components, including other containers. Container allows you to create
groupings of objects on the screen.
Important methods of Component Class:
1.public void add(Component c): This method inserts a component into a
Container by taking a component as a parameter.
2.public void setSize(int width, int height): This method sets the size of
the component by taking height and width as parameters.
3.public void setLayout(LayoutManager lm): This method sets the layout
to set the components in a particular manner by taking LayoutManager
as a parameter.
4.public void setVisible(boolean status): This method sets the visibility of
a component to visible or not. If it sets to true then the component will
be visible in the output else if it sets to false or not defined component
won’t be visible in the output.
Types of Components in Component Class
The components in a component class are as follows :
1.Container
2.Button
3.Label
4.Checkbox
5.Choice
6.List
All these components are present in java.awt package.
Hierarchical Structure of Components
• Program to draw circle with the use of AppletViewer.

import
java.awt.*;
import
java.applet*;
public class circle extends Applet
{
public void paint(Graphics g)
{

g.drawOval(20,20,200,120);
g.setColor(Color.green);
g.fillOval(10,10,100,110);
}
}
/*
<applet code = "circle.class" width = "480" height = "360"></applet>
*/
import java.awt.*;
import java.applet.*;
public class circle extends Applet
{
public void paint(Graphics g)
{

g.drawOval(20,20,200,120);
g.setColor(Color.green);
g.fillOval(20,20,200,120);
}
}
/*
<applet code = "circle.class" width = "480" height = "360"></applet>
*/
• Program to choice box with the use of Applet.
import java.awt.*;
public class ChoiceExample
{
ChoiceExample()
{
Frame f= new Frame(); Choice c=new
Choice();
c.setBounds(100,100, 75,75);
c.add("Item 1");
c.add("Item 2");
c.add("Item 3");
c.add("Item 4");
c.add("Item 5"); f.add(c);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ChoiceExample();
}
}
Program to Radio box with the use of Applet.

import javax.swing.*;
public class RadioButtonExample {
JFrame f;

RadioButtonExample(){
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();

bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {

new RadioButtonExample();
}
}
Program to Checkox with the use of Applet.

import java.awt.*;
public class Check
{
Check()
{
Frame f = new Frame("checkbox");
Checkbox c1 = new Checkbox("C++");
Checkbox c2 = new Checkbox("JAVA");
Checkbox c3 = new Checkbox("SQL");
c1.setBounds(80,100,80,100);
c2.setBounds(80,150,80,150);
c3.setBounds(80,230,80,230);
f.add(c1);
f.add(c2);
f.add(c3);
f.setSize(400,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new Check();
}
}
Layout manager
 The LayoutManagers are used to arrange components in a particular manner.
 LayoutManager is an interface that is implemented by all the classes of layout managers.
 There are the following classes that represent the layout managers:
1.java.awt.BorderLayout
2.java.awt.FlowLayout
3.java.awt.GridLayout
4.java.awt.CardLayout
5.java.awt.GridBagLayout
 A layout manager is an object that controls the size and position (layout) of components inside a
Container object. For example, a window is a container that contains components such as buttons
and labels. The layout manager in effect for the window determines how the components are sized
and positioned inside the window.
1. BorderLayout
• The border layout is the default layout manager for all
Window objects. It consists of five fixed areas: North,
South, East, West, and Center. You do not have to put a
component in every area of the border layout.
• It arranges all the components along the edges or the
middle of the container i.e. top, bottom, right and
left edges of the area.
import java.awt.*;
public class Border

{
public static void main(String[] args)
{
Frame f = new Frame("frame 1");

f.setLayout(new BorderLayout(30,30));

f.add(new Button("CLICK"), BorderLayout.NORTH);

f.add(new Button("SUBMIT"), BorderLayout.SOUTH);


f.add(new Button("SAVE"), BorderLayout.EAST);
f.add(new Button("PREVIEW"), BorderLayout.WEST);
f.add(new Button("OK"), BorderLayout.CENTER);

f.pack();
f.setVisible(true);

}
}
2. Grid Layout:
The layout manager GridLayout arranges elements in a grid
of rows and columns. The layout manager is created with a
specified number of rows and columns, and components
are added one at a time, filling each grid cell from left to
right and from top to bottom.
import java.awt.*;
public class Grid{
Grid()
{
Frame f=new Frame();
Button b1=new Button("OK");
Button b2=new Button("SAVE");
Button b3=new Button("SUBMIT");
Button b4=new Button("CLICK");
Button b5=new Button("PREVIEW");
Button b6=new Button("NEXT");
Button b7=new Button("OK");
Button b8=new Button("SAVE");
Button b9=new Button("SUBMIT");
Button b10=new Button("CLICK");
Button b11=new Button("PREVIEW");
Button b12=new Button("NEXT");
f.add(b1); f.add(b2); f.add(b3);
f.add(b4); f.add(b5); f.add(b6);
f.add(b7); f.add(b8); f.add(b9);
f.add(b10); f.add(b11); f.add(b12);
f.setLayout(new GridLayout(7,7));
f.setSize(100,100);
f.setVisible(true);
}
public static void main(String[] args) {
new Grid();
} }
3.Flow Layout:
A layout manager called FlowLayout arranges components in
a row, adding additional rows as needed when the width of
the container is exceeded. From left to right, the components
are added, with the next component being added directly to
the right of the one before it.
import java.awt.*;
public class Flow
{
Frame f;
Flow(){
f=new Frame();
Button b1=new Button("OK");
Button b2=new Button("CLICK");
Button b3=new Button("SAVE");
Button b4=new Button("SUBMIT");
f.add(b1); f.add(b2);
f.add(b3); f.add(b4);
f.setLayout(new FlowLayout(FlowLayout.LEFT,20,25));
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new Flow();
}
}
4. CARD LAYOUT
The Java CardLayout class manages the components in such a manner that
only one component is visible at a time. It treats each component as a card
that is why it is known as CardLayout.

You might also like