Applet-HTML Programming2018
Applet-HTML Programming2018
Using Java
•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.
•IT Describe what a webpage should look
like
•HTML is written in the from of tags,
enclosed in angle brackets like <html>
•A browser read HTML documents &
displays them on screen
•The browser does not display the HTML
tags but uses the tags to decide how to
display the content of the page
•Applet are program that run over the internet browsers
and hence can be transported over the internet and run
over there .
•Applets are Java programs that are integrated into Web
pages.
•When a Web page containing an applet is displayed by a
Web browser the applet is loaded and executed.
•The applet's output is displayed within a subset of the
browser's display area.
•All applets are subclasses of Applet.
•Thus, all applets must import java.applet.
•Applets must also import java.awt.
•AWT stands for the Abstract Window Toolkit.
•Applets run in a window, it is necessary to include
support for that window. it handled with various
AWT methods, such as drawstring( )
•which outputs a string to a specified X,Y Location.
•Once an applet has been compiled, it is included in
an HTML file using the APPLET tag. The applet will
be executed by a Java-enabled web browser when it
encounters the APPLET tag within the HTML file.
APPLET tag :-
Here is an example of such a comment:-
/*<applet code="MyApplet.class" width=200
height=60>
</applet>
*/
Packages in Applet
• java.applet: - applets are programs which come
from a server into a client and get executed on
the client machine. An applet is a program that
comes to the internet server.
• java.awt : - abstract window tool kit.This package
helps to develop GUI.
- The models for event handling techniques
different tools for graphics and images and the
LayoutManger also it facilities the mechanism for
data transfer by cut and paste process through
clipboard .
Java.applet package
•It consists of a single class, the Applet class
& 3 interfaces:-
AppletContext,
Applet Stub, &
Audio Clip.
Creating Applets
public class Class_NAME extends Applet
{
........
}
//MYCLASS : class name
//APPLET :applet class
•Difference between java application and java applet?
With example.
What Makes a Java Applet Different from a Java Application?
Since you now know that applets can use the entire Java API during runtime,
what actually makes an applet an applet? The most important differences
between the two include:
•Unlike traditional Java applications, applets do not invoke a main() method.
•Applets are designed to be embedded within an HTML webpage.
•The applet Đode is autoŵatiĐallLJ doǁŶloaded to the user͛s ŵaĐhiŶe ǁheŶ theLJ
view an HTML webpage with an embedded applet.
•A Java Virtual Machine (JVM) is required to view an applet. In some cases, the
JVM is a browser plug-in, but it can also be a separate runtime environment
iŶstalled loĐallLJ oŶ the user͛s Đoŵputer.
•The JVM creates an instance of the applet class and invokes methods defined
ǁithiŶ the applet duriŶg the applet͛s lifetiŵe.
•The security rules for applets are enforced by the web browser and tend to be
much stricter than the security rules that apply to standalone Java applications.
It is often referred to as a sandbox environment. This is done to prevent
ŵaliĐious Đode froŵ edž eĐutiŶg oŶ a user͛s Đoŵputer at ruŶtiŵe.
•If an applet requires a custom class, it can be downloaded in a single Java
Archive (JAR) file. This means the possibilities are endless because you can
create custom classes as your skill level progresses.
Java Application Java applet
1) Java applications are stand- Java applets run inside a Java
alone Java programs that can browser.
be run by using just the Java Applet run in the context of a web
interpreter (outside the browser ,being typically embedded
browser) within an html page
Applications programming does not
require a web browser to run .they can
created like normal programming
language program .
2) Application can run by itself. Applications cannot run by itself
and requires browser software to
run it.
3) The main() method must be The paint()method should be
capable of accepting an array capable of accepting an array of
of string object string objects
Java Application Java applet
4 No control over the There is some control over
) execution of the program the execution of the program
.
5 Example : Example:
) class Hello import java.applet.Applet;
{ import java.awt.*;
// Your program begins with a /*<applet
call to main( ) CODE="helloworld.class"
public static void WIDTH=400 HEIGHT=100>
main(String args[]) </applet>*/
{ Public class helloworld
System.out.println(" Hello extends Applet
World "); {
} Public void paint(Graphics g)
} {
g.drawString(ˈhello
pw oarikalrd!ˉ,10,10);
ak G
Applet Life Cycle
APPLET LIFE CYCLE
Steps -Applet Life cycle
1.Born or initialization state
2.Running state
3.Idle state
4.Dead or destroyed state
1.Born state
•Applet eŶters the ͚ďorŶ͛ state ǁheŶ it is first loaded this is
achieved by calling the init() method of applet class
1.create objects needed by the applet .
2.set up initial values
3.load image or fonts
4.setup colors
Applet enter into this state only once in its life cycle
because init( ) method is called only once in the beginning.
The init( ) method is the first method to
Syntax :
be called. This is where you should
public void init() initialize variables. This method is called
{ only once during the run time of your
……. applet.
}
2.Running state
Start( ) method:
•The start( ) method is called after init( ).
• It is also called to restart an applet after it has been stopped.
•Hence if the web page left for some time and we go back to that
web page which has an applet ,then the start() method of the applet
is called again.
whereas init( ) is called once—the first time an applet is
loaded—start; Ϳis Đalled eaĐh tiŵe aŶ applet͛s HTML document
is displayed onscreen. So, if a user leaves a web page and
comes back, the applet resumes execution at start( )
Paint() method:
The paint( ) method has one parameter of type Graphics.
This parameter will contain the graphics context, which
describes the graphics environment in which the applet is
running.
3.Idle state
Stop() method:
When the page containing the applet is closed or the browser goes
to another page then the stop() method is called . It just stops the
thread running the applet .the applet can be started by the usage of
start() method after stopping it .
4.Destroyed state :
The destroy( )method:
needs to be removed completely from memory. This occurs
automatically by invoking destroy() method when we quit the
browser. An applet gets destroyed only once its life cycle
•The stop( ) method is always called before destroy( ).
Note :
• init(): Initialization occurs when applet is first loaded. Tasks
performed here are creating objects setting initial state, loading
images, fonts etc. and setting parameters.
• start(): Starting occurs after initialization and after stop occurs. Tasks
include starting threads, sending messages to helper objects, or to
tell the applet to start running.
void drawArc(int top, int left, int width, int height, int
startAngle, int sweepAngle)
void fillArc(int top, int left, int width, int height, int
startAngle,int sweepAngle)
• The arc is bounded by the rectangle whose upper-left
corner is specified by top, left and whose width and
height are specified by width and height.
• The arc is drawn from startAngle through the angular
distance specified by sweepAngle.
• Angles are specified in degrees.
• Zero degrees is oŶ the horizoŶtal, at the three o͛ĐloĐk
position.
• The arc is drawn counterclockwise if sweepAngle is
positive, and clockwise if sweepAngle is negative.
• Therefore, to draǁ aŶ arĐ froŵ tǁ elǀe o͛ĐloĐk to sidž
o͛ĐloĐk, the start aŶgle ǁ ould ďe 9Ϭ aŶd the sǁ eep
angle 180.
import java.awt.*;
import java.applet.*;
/* <applet code="Arcs" width=300 height=200>
</applet> */
public class Arcs extends Applet {
public void paint(Graphics g) {
g.drawArc(10, 40, 70, 70, 0, 75);
g.fillArc(100, 40, 70, 70, 0, 75);
g.drawArc(10, 100, 70, 80, 0, 175);
g.fillArc(100, 100, 70, 90, 0, 270);
g.drawArc(200, 80, 80, 80, 0, 180);
}
}
Drawing Polygons
It is possible to draw arbitrarily shaped figures using
drawPolygon( ) and fillPolygon( ),
shown here:
void drawPolygon(int x[ ], int y[ ], int numPoints)
void fillPolygon(int x[ ], int y[ ], int numPoints)