Applet in java Introduction
Table of Content:
What is java applet?
An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible web browser. or in another way we can say 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.
The creation of the applet changed Internet programming because it expanded the universe of objects that can move about freely in cyberspace. In general, there are two very broad categories of objects that are transmitted between the server and the client: passive information and dynamic, active programs. For example, when you read your e-mail, you are viewing passive data. Even when you download a program, the program’s code is still only passive data until you execute it. By contrast, the applet is a dynamic, self-executing program. Such a program is an active agent on the client computer, yet it is initiated by the server.
Why applet is important
There are many importance of applet. They are as follows:
- Security and portability : The same applet must be able to be downloaded and executed by the wide variety of CPUs, operating systems, and browsers connected to the Internet. It is not practical to have different versions of the applet for different computers. The same code must work on all computers. Therefore, some means of generating portable executable code was needed. As you will soon see, the same mechanism that helps ensure security also helps create portability
- It works at client side so less response time.
- It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc.
- An applet can be a fully functional Java application because it has the entire Java API at its disposal.
Drawback of Applet
- Plugin is required at client browser to execute applet.
Life Cycle of an Applet
Various states, an applet, undergoes between its object creation and object removal (when the job is over) is known as Applet Life Cycle. Each state is represented by a method. There exists 5 states represented by 5 methods. That is, in its life of execution, the applet exists (lives) in one of these 5 states.
- Applet is initialized(init).
- Applet is started(start).
- Applet is painted(paint).
- Applet is stopped(stop).
- Applet is destroyed(destroy).
These methods are known as "callback methods" as they are called automatically by the browser whenever required for the smooth execution of the applet. Programmer just write the methods with some code but never calls.
Following are the methods.
- init() method
- start() method
- paint() method
- stop() method
- destroy() method
These methods are known as Applet Life Cycle methods. These methods are defined in java.applet.Applet class except paint() method. The paint() method is defined in java.awt.Component class, an indirect super class of Applet.
Description of Applet Life Cycle methods
Even though the methods are called automatically by the browser, the programmer should know well when they are called and what he can do with the methods. Following is the schematic representation of the methods.
Brief Description of Life Cycle Methods
Following is the brief description of the above methods.
- init(): The applet's voyage starts here. In this method, the applet object is created by the browser. Because this method is called before all the other methods, the programmer can utilize this method to instantiate objects, initialize variables, setting background and foreground colors in GUI etc.; the place of a constructor in an application. It is equivalent to born state of a thread.
- start(): In init() method, even through applet object is created, it is in inactive state. An inactive applet is not eligible for microprocessor time even though the microprocessor is idle. To make the applet active, the init() method calls start() method. In start() method, applet becomes active and thereby eligible for processor time.
- paint(): This method takes a java.awt.Graphics object as a parameter. This class includes many methods of drawing necessary to draw on the applet window. This is the place where the programmer can write his code of what he expects from applet like animation etc. This is equivalent to runnable state of thread.
- stop(): In this method, the applet becomes temporarily inactive. An applet can come any number of times into this method in its life cycle and can go back to the active state (paint() method) whenever would like. It is the best place to have a cleanup code. It is equivalent to the blocked state of the thread.
- destroy(): This method is called just before an applet object is a garbage collected. This is the end of the life cycle of an applet. It is the best place to have cleanup code. It is equivalent to the dead state of the thread.
After knowing the methods, let us know when they are called by the browser.
- init() method is called at the time of starting the execution. This is called only once in the life cycle.
- start() method is called by the init() method. This method is called a number of times in the life cycle; whenever the applet is deiconifed , to make the applet active.
- paint() method is called by the start() method. This is called a number of times in the execution.
- stop() method is called whenever the applet window is iconified to inactivate the applet. This method is called a number of times in the execution.
- destroy() method is called when the applet is closed. This method is called only once in the life cycle.
Observe, the init() and destroy() methods are called only once in the life cycle. But, start(), paint() and stop() methods are called a number of times.
Hierarchy of Applet
As displayed in the below diagram, Applet class extends Panel. Panel class extends Container which is the subclass of Component.
How to run an Applet?
There are two ways to run an applet
- By html file.
- By appletViewer tool (for testing purpose).
Simple example of Applet by html file:
- Any applet in Java is a class that extends the java.applet.Applet class.
- An Applet class does not have any main() method. It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a separate runtime environment to run an applet application.
- JVM creates an instance of the applet class and invokes init() method to initialize an Applet.
Program
file name: Simple.javaimport java.awt.*; import java.applet.*; public class Simple extends Applet { public void paint(Graphics g) { g.drawString("A simple Applet", 20, 20); } }
Html file
file name: mySimpleApplet.html代做工资流水公司沧州代开自存流水邢台背调银行流水代做郑州查入职流水绵阳工资流水app截图价格威海签证工资流水报价桂林企业对公流水多少钱石家庄代办购房银行流水曲靖企业对公流水开具开车贷银行流水淮安签证银行流水 开具湖州代办企业对公流水郑州打印离职证明厦门查入职流水揭阳做收入证明曲靖企业对公流水样本代办流水单黄冈转账银行流水代办揭阳个人流水代做常州流水费用舟山企业对公流水图片九江银行流水账单兰州制作薪资流水单扬州查询对公流水桂林贷款工资流水 代做衡阳入职流水代做江门查日常消费流水济宁银行对公流水样本广州查流水账单贵阳银行流水电子版样本佛山银行流水账打印香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声卫健委通报少年有偿捐血浆16次猝死汪小菲曝离婚始末何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言男子被猫抓伤后确诊“猫抓病”周杰伦一审败诉网易中国拥有亿元资产的家庭达13.3万户315晚会后胖东来又人满为患了高校汽车撞人致3死16伤 司机系学生张家界的山上“长”满了韩国人?张立群任西安交通大学校长手机成瘾是影响睡眠质量重要因素网友洛杉矶偶遇贾玲“重生之我在北大当嫡校长”单亲妈妈陷入热恋 14岁儿子报警倪萍分享减重40斤方法杨倩无缘巴黎奥运考生莫言也上北大硕士复试名单了许家印被限制高消费奥巴马现身唐宁街 黑色着装引猜测专访95后高颜值猪保姆男孩8年未见母亲被告知被遗忘七年后宇文玥被薅头发捞上岸郑州一火锅店爆改成麻辣烫店西双版纳热带植物园回应蜉蝣大爆发沉迷短剧的人就像掉进了杀猪盘当地回应沈阳致3死车祸车主疑毒驾开除党籍5年后 原水城县长再被查凯特王妃现身!外出购物视频曝光初中生遭15人围殴自卫刺伤3人判无罪事业单位女子向同事水杯投不明物质男子被流浪猫绊倒 投喂者赔24万外国人感慨凌晨的中国很安全路边卖淀粉肠阿姨主动出示声明书胖东来员工每周单休无小长假王树国卸任西安交大校长 师生送别小米汽车超级工厂正式揭幕黑马情侣提车了妈妈回应孩子在校撞护栏坠楼校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变老人退休金被冒领16年 金额超20万西藏招商引资投资者子女可当地高考特朗普无法缴纳4.54亿美元罚金浙江一高校内汽车冲撞行人 多人受伤
Now Compile the program and open the html file using cmd
Microsoft Windows [Version 6.2.9200] (c) 2012 Microsoft Corporation. All rights reserved. C:\Users\Hello World>I: I:\>cd Java Programming I:\Java Programming>javac Simple.java I:\Java Programming>appletviewer mySimpleApplet.html
After Compilation: folder contains
My Location (present working directory): I:/Java Programming/
Output
Littlebit Explanation
Every Applet application must import two packages - java.awt
and java.applet
.
java.awt.*
imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user (either
directly or indirectly) through the AWT. The AWT contains support for a window-based, graphical user interface.
java.applet.*
imports the applet package, which contains the class Applet. Every applet
that you create must be a subclass of Applet class.
The class in the program must be declared as public, because it will be accessed by code that is outside the program.Every Applet application must declare a paint() method. This method is defined by AWT class and must be overridden by the applet. The paint() method is called each time when an applet needs to redisplay its output. Another important thing to notice about applet application is that, execution of an applet does not begin at main() method. In fact an applet application does not have any main() method.
Simple example of Applet by appletviewer tool
Program
file name: Simple.javaimport java.awt.*; import java.applet.*; public class Simple extends Applet { public void paint(Graphics g) { g.drawString("A simple Applet", 20, 20); } }/* */
Now Compile the program and run using appletviewer by cmd
Microsoft Windows [Version 6.2.9200] (c) 2012 Microsoft Corporation. All rights reserved. C:\Users\Hello World>I: I:\>cd Java Programming I:\Java Programming>javac Simple.java I:\Java Programming>appletviewer Simple.java I:\Java Programming>
After Compilation: folder contains
My Location (present working directory): I:/Java Programming/