Tutorial - 6
Tutorial - 6
Advantage of Applet
There are many advantages of applet. They are as follows:
It works at client side so less response time.
Secured
It can be executed by browsers running under many plateforms,
including Linux, Windows, Mac Os etc.
Drawback of Applet
Plugin is required at client browser to execute applet.
Lifecycle of Java Applet
1.Applet is initialized.
2.Applet is started.
3.Applet is painted.
4.Applet is stopped.
5.Applet is destroyed.
1.public void init(): is used to initialized the Applet. It is invoked only
once.
}
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
Difference between a Java Application and a Java Applet
import java.applet.Applet;
import java.awt.Graphics;
The repaint method is an asynchronous method of applet class. When call to repaint method is made, it performs a request
to erase and perform redraw of the component after a small delay in time.
Example:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
/*<applet code="RepaintJavaExample.class" width="350" height="150"> </applet>*/
public class RepaintJavaExample extends Applet implements MouseListener
{
private int mouseX, mouseY;
private boolean mouseclicked = false;
public void init()
{
setBackground(Color.CYAN);
addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
mouseX = e.getX();
mouseY=e.getY();
mouseclicked = true;
repaint();
}
public void mouseEntered(MouseEvent e){};
public void mousePressed(MouseEvent e){};
public void mouseReleased(MouseEvent e){};
public void mouseExited(MouseEvent e){};
public void paint( Graphics g )
{
String str;
g.setColor(Color.RED);
if (mouseclicked)
{
str = "X="+ mouseX + "," + "Y="+ mouseY ;
g.drawString(str,mouseX,mouseY);
mouseclicked = false;
}
}
}
getCodeBase():
The getCodeBase() method returns the complete URL of the . class file that contains the applet.
import java.awt.*;
import java.applet.*; import java.net.*;
}
Java LayoutManagers
ava BorderLayout
The BorderLayout is used to arrange the
components in five regions: north, south, east,
The Java LayoutManagers facilitates us to control the west, and center. Each region (area) may contain
positioning and size of the components in GUI forms. one component only. It is the default layout of a
LayoutManager is an interface that is implemented by all the frame or window. The BorderLayout provides five
classes of layout managers. There are the following classes that constants for each region:
represent the layout managers: 1.public static final int NORTH
1.java.awt.BorderLayout 2.public static final int SOUTH
2.java.awt.FlowLayout 3.public static final int EAST
3.java.awt.GridLayout 4.public static final int WEST
4.java.awt.CardLayout 5.public static final int CENTER
5.java.awt.GridBagLayout
6.javax.swing.BoxLayout
7.javax.swing.GroupLayout Constructors of BorderLayout class:
8.javax.swing.ScrollPaneLayout
9.javax.swing.SpringLayout etc. BorderLayout(): creates a border layout but with no gaps between
the components.
BorderLayout(int hgap, int vgap): creates a border layout with the
given horizontal and vertical gaps between the components.
import java.awt.*;
import javax.swing.*;
// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER
f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction
f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction
f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}
}