Download - 6.applet programming in java
APPLET
PROGRAMMING IN
JAVA
BCA 5th sem
DEEPAK SHARMA
12KSSB6031
APPLET
• An applet is a Panel that allows interaction with a Java program
• A applet is typically embedded in a Web page and can be run
from a browser
• You need special HTML in the Web page to tell the browser
about the applet
• For security reasons, applets run in a sandbox: they have no
access to the client’s file system
• Most modern browsers support Java 1.4 if they have the
appropriate plug-in
• A programmer should be able to write applets that can run on
any browser
Applet Life Cycle
Born
Running
Idle Dead
Begin init()
paint() start()
stop()
destroy()
End
Applets have 4 life cycle methods. They are:
init() – It is called to initialize the applet before it gets
loaded
start() – It is called to provide start up behavior
stop() – Called to stop any operations which are started
using start() method.
destroy() – Is called when an applet has finished
execution.
init()
start()
stop()
destroy()
do some work
‘init’ and ‘destroy’ are only called
once each
‘start’ and ‘stop’ are called
whenever the browser enters and
leaves the page
‘do some work’ is code called by
your listeners
‘paint’ is called when the applet
needs to be repainted
public void paint(Graphics g)
• Needed if you do any drawing or painting other than
just using standard GUI Components.
• Any painting you want to do should be done here, or in
a method you call from here.
• Painting that you do in other methods may or may not
happen.
• Never call paint(Graphics), call repaint( ).
Repaint()
Call repaint( ) when you have changed something
and want your changes to show up on the screen
repaint( ) is a request--it might not happen
When you call repaint( ), Java schedules a call to
update(Graphics g)
Example Graphics Methods
g.drawString(“Hello”, 20, 20); Hello
g.drawRect(x, y, width, height);
g.fillRect(x, y, width, height);
g.drawOval(x, y, width, height);
g.fillOval(x, y, width, height);
g.setColor(Color.red);
Example –Applet
Html Code For Applet
<html>
<body>
<applet code="ball1.class" width=200
height=200>
</applet>
</body>
</html>
Example- Applet
Example- Applet
Applet
ADVANTAGES
Applets are cross platform and can run on Windows, Mac OS and
Linux platform
Applets can work on all the versions of Java Plug-in.
Applet runs in a sandbox, so the user doesn’t need to trust the
code and it can work without security approval.
DISADVANTAGES:
Java Plug-in is required to run applet.
Java applet needs JVM so first time it takes significant start-up
time.
Difficult to design and build good user interface in applets
compared to other technologies.
THANK YOU