6.applet programming in java

14
APPLET PROGRAMMING IN JAVA BCA 5 th sem DEEPAK SHARMA 12KSSB6031

Upload: deepak-sharma

Post on 07-Jul-2015

199 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 6.applet programming in java

APPLET

PROGRAMMING IN

JAVA

BCA 5th sem

DEEPAK SHARMA

12KSSB6031

Page 2: 6.applet programming in java

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

Page 3: 6.applet programming in java

Applet Life Cycle

Born

Running

Idle Dead

Begin init()

paint() start()

stop()

destroy()

End

Page 4: 6.applet programming in java

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.

Page 5: 6.applet programming in java

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

Page 6: 6.applet programming in java

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( ).

Page 7: 6.applet programming in java

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)

Page 8: 6.applet programming in java

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);

Page 9: 6.applet programming in java

Example –Applet

Page 10: 6.applet programming in java

Html Code For Applet

<html>

<body>

<applet code="ball1.class" width=200

height=200>

</applet>

</body>

</html>

Page 11: 6.applet programming in java

Example- Applet

Page 12: 6.applet programming in java

Example- Applet

Page 13: 6.applet programming in java

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.

Page 14: 6.applet programming in java

THANK YOU