java applet_1
java applet_1
Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and
works at client side.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 are the
Plugin is required at client browser to execute 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 maximized. 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.
There are two ways to run an applet: 1.By html file.2.By applet Viewer tool (for testing purpose).
Example of Applet by html file:To execute the applet by html file, create an applet and compile it. After that create an html file and
place the applet code in html file. Now click the html file.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
g.drawString("welcome",150,150);
} }
Output:myapplet.html
<html>
<body>
</applet>
</body>
</html>
Parameter passing in applets: Java allows users to pass user-defined parameters to an applet with the help of
<PARAM>tags. The <PARAM>tag has a NAME attribute which defines the name of the parameter and a VALUE attribute
which specifies the value of the parameter. In the applet source code, the applet can refer to the parameter by its NAME to
find its value. The syntax of the <PARAM>tag is :< APPLET>
<PARAMNAME=parameter1_name VALUE=parameter1_value>
<PARAMNAME=parameter2_name VALUE=parameter2_value>
<PARAMNAME=parametern_name VALUE=parametern_value>
</APPLET>
For example, consider the following statements to set the text attribute of applet to This is an example of Parameter! ! !
<APPLET>
</APPLET>
Note that the <PARAM>tags must be included between the <APPLET> and</ APPLET> tags. The init () method in the applet
retrieves user-defined values of the parameters defined in the <PARAM>tags by using the get Parameter () method. This method
accepts one string argument that holds the name of the parameter and returns a string which contains the value of that parameter. Since
it returns String object, any data type other than String must be converted into its corresponding data type before it can be used.
import java.awt.*;
import java.applet.*;
<paramname=fontName value=Courier>
</applet>
{ String name;
int size;
float lead;
boolean active;
{String pa;
name = getParameter("Name");
if(name == null)
name = "Not Found";
pa = getParameter("Size");
try
{ if(pa != null)
catch(NumberFormatException e)
{size = -1; }
pa = getParameter("lead");
try
{ if(pa != null)
lead = Float.valueOf(pa).floatValue();
else
lead = 0;
catch(NumberFormatException e)
lead = -1;
pa = getParameter("AccountEnabled");
if(pa != null)
active = Boolean.valueOf(pa).booleanValue();
Output:
Event Handling:Event Handling is the mechanism that controls the event and decides what should happen if an event
occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the
Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the
events.Let's have a brief introduction to this model.
The Delegation Event Model has the following key participants namely:
1. Source - The source is an object on which event occurs. Source is responsible for providing information of the occurred event to it's
handler. Java provide as with classes for source object.
2. Listener - It is also known as event handler. Listener is responsible for generating response to an event. From java implementation
point of view the listener is also an object. Listener waits until it receives an event. Once the event is received, the listener process the
event a then returns.
The benefit of this approach is that the user interface logic is completely separated from the logic that generates the event. The user
interface element is able to delegate the processing of an event to the separate piece of code. In this model, Listener needs to be
registered with the source object so that the listener can receive the event notification. This is an efficient way of handling the event
because the event notifications are sent only to those listener that want to receive them.
In order to design a listener class we have to develop some listener interfaces. These Listener interfaces forecast some public abstract
callback methods which must be implemented by the listener class. If you do not implement the any if the predefined interfaces then
your class can not act as a listener class for a source object.
Callback Methods: These are the methods that are provided by API provider and are defined by the application programmer
and invoked by the application developer. Here the callback methods represents an event method. In response to an event
java jre will fire callback method. All such callback methods are provided in listener interfaces. If a component wants some
listener will listen to it's events the the source must register itself to the listener.
Event Handling Example:Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint
> gui >
package com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
public AwtControlDemo(){
prepareGUI();
}
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());
controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.: D:\AWT>javac com\
tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command:D:\AWT>java com.tutorial
spoint.gui.AwtControlDemo
Output:
GetCodeBase and GetDocumentBase: You will create applets that will need to explicitly load media and text. Java will
allow the applet to load data from the directory holding the html file that started the applet (the document base) and the
directory from which the applet’s class file was loaded (the code base). These directories are returned by
getDocumentBase( ) and getCodeBase( ).
Program:
import java.applet.Applet;
import java.awt.Graphics;
}}
Program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
private Vector v;
public PaintRepaintTest() {
v = new Vector();
setBackground(Color.white);
addMouseListener(this);
}
public void paint(Graphics g) { // paint() method
super.paint(g);
g.setColor(Color.black);
while(enumeration.hasMoreElements()) {
Point p = (Point)(enumeration.nextElement());
v.add(me.getPoint());
frame.getContentPane().add(new PaintRepaintTest());
frame.setTitle("PaintRepaint Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(375, 250);
frame.setVisible(true);
Output: