csi 3125, preliminaries, page 1 applet. csi 3125, preliminaries, page 2 applet an applet is a java...

15
CSI 3125, Preliminaries, page 1 Applet

Upload: deborah-mcgee

Post on 17-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

CSI 3125, Preliminaries, page 3 Applet An applet is a Java class that extends the java.applet.Applet class. A main() method is not invoked on an applet, and an applet class will not define main(). Applets are designed to be embedded within an HTML page. A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment. Applets have strict security rules that are enforced by the Web browser.

TRANSCRIPT

Page 1: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 1

Applet

Page 2: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 2

Applet• An applet is a Java program that runs in a Web

browser. • An applet can be a fully functional Java

application.• There are some important differences

between an applet and a standalone Java application

• And they are

Page 3: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 3

Applet

• An applet is a Java class that extends the java.applet.Applet class.

• A main() method is not invoked on an applet, and an applet class will not define main().

• Applets are designed to be embedded within an HTML page.• A JVM is required to view an applet.• The JVM can be either a plug-in of the Web browser or a

separate runtime environment.• Applets have strict security rules that are enforced by the

Web browser.

Page 4: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 4

Applet & Application• Applet• Small Program• Used to run a program on

client Browser• Applet is portable and can be

executed by any JAVA supported browser.

• Applet applications are executed in a Restricted Environment

• Applets are created by extending the java.applet.Applet

• Application• Large Program• Can be executed on stand

alone computer system• Need JDK, JRE, JVM

installed on client machine.• Application can access all

the resources of the computer

• Applications are created by writing public static void main(String[] s) method.

Page 5: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 5

Applet & Application• Applet• Applet application has 5 methods

which will be automatically invoked on occurance of specific event

• Example:• import java.awt.*;• import java.applet.*;

• public class Myclass extends Applet • {• public void init() { }• public void start() { }• public void stop() {}• public void destroy() {}• public void paint(Graphics g) {}• }

• Application• Application has a

single start point which is main method

• public class MyClass • {• public static void

main(String args[]) {}• }

Page 6: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 6

Applet• An applet program is written as a inheritance of the

java.applet.Applet class • Applet must be subclass of java.applet.Applet• ie • import java.applet.Applet;• Or• import java.applet.*;• And • extends the Applet class (for creating sub class of Applet class)

• And• Override the Applet class Methods (init(), start(), stop(), &

destroy())

Page 7: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 7

Applet Life Cycle• Applet provides 4 life cycle methods of applet.• public void init(): is used to initialized the Applet. • It is invoked only once.• public void start(): is invoked after the init() method or browser is

maximized. • It is used to start the Applet.• public void stop(): is used to stop the Applet. • It is invoked when Applet is stop or browser is minimized.• public void destroy(): is used to destroy the Applet. It is invoked only once.• public void paint(): it is inherited from java.awt • It invoke immediately after start() method• Used to paint the Applet. • It provides Graphics class object that can be used for drawing oval, rectangle,

arc etc.

Page 8: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 8

Applet Life Cycle

Page 9: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 9

Applet Life Cycle• Browser visits page containing an applet – Browser calls init on that applet, once – Browser calls start on that applet

• Browser goes away from that page– Browser calls stop on that applet

• Browser comes back to that page – Browser calls start again on that applet

• Browser shuts down– Browser calls destroy on the applet, once

Page 10: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 10

Run an Applet• There are two ways to run an applet• 1) By html file.• 2) By appletViewer tool (for testing purpose).

• To execute the applet by html file, create an applet and compile it.

• Create an html file which embedded the .class file of java with applet code tag in html file.

Page 11: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 11

<Applet> tag 1) By html file.• <APPLET code=“my.class" width="300" height="300“

align=“top”>

• <APPLET // the beginning of the HTML applet code• CODE=“my.class“ // the actual name of the applet (usually a

'class' file)• WIDTH="100“ // the physical width of the applet on the

page• HEIGHT="50“ // the physical height of the applet on the

page• ALIGN="Top“ // align the applet within its page space (top,

bottom, center)

Page 12: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 12

Applet example 1) By html file• //First.java • import java.applet.Applet; • import java.awt.Graphics; • public class First extends Applet{ • public void paint(Graphics g){ • g.drawString("welcome",150,150); • } } after compiling (javac First.java)

• //run.html• <html> • <body> • <applet code="First.class" width="300" height="300"> • </applet> • </body> • </html>

Page 13: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 13

Applet By 2) appletViewer tool • To execute the applet by appletviewer tool,

create an applet that contains applet tag in comment and compile it.

• First.java javac first.java

• After that run it by: appletviewer First.java.

• Now Html file is not required but it is for testing purpose only.

Page 14: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 14

Applet By 2) appletViewer tool • To execute the applet by appletviewer tool,

create an applet that contains applet tag in comment and compile it.

• First.java javac first.java

• After that run it by: appletviewer First.java.

• Now Html file is not required but it is for testing purpose only.

Page 15: CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be

CSI 3125, Preliminaries, page 15

Applet example 2) appletViewer tool • //First.java • /* • <applet code="First.class" width="300" height="300"> • </applet> • */ • import java.applet.Applet; • import java.awt.Graphics; • public class First extends Applet{ • public void paint(Graphics g){ • g.drawString("welcome",150,150); • } }

• after compiling • c:\>javac First.java• c:\>appletviewer First.java