Applet Presented by R Mahendran M Sc M

Applet Presented by R Mahendran M. Sc. , M. Phil. , M. Tech. , Assistant Professor Department of Computer Science Nehru Memorial College Puthanampatti 12/31/2021 R Mahendran Java Applet 1

Applet Basis q An applet is a Java program that runs in a Web browser. q An applet can be a fully functional Java application because it has the entire Java API q Applet is a special type of program that is embedded in the webpage to generate the dynamic content 12/31/2021 R Mahendran Java Applet 2

…Applet Basis An applet is a Java class that extends the java. applet. Applet class. Two type of Applet 1. Applet Class This Applet Abstract Window Toolkit 2. JApplet Based on the swing Class Swing class provide the GUI q 12/31/2021 R Mahendran Java Applet 3

Applet Life Cycle n For creating any applet java. applet. Applet class must be inherited. It provides 4 life cycle methods of applet n Applet is initialized. n Applet is started. n Applet is painted. n Applet is stopped. n Applet is destroyed. 12/31/2021 R Mahendran Java Applet 4

Init() Method q public void init() It is used to initialized the Applet. ü It is invoked only once. ü public void init() { initialization } 12/31/2021 R Mahendran Java Applet 5

Start() Method public void start(): ü it is invoked after the init() method or browser is maximized. ü It is used to start the Applet. Public void start() { Start or resume execution } q 12/31/2021 R Mahendran Java Applet 6

Stop Method() public void stop() ü it is used to stop the Applet. ü It is invoked when Applet is stop or browser is minimized. q public void stop() { suspends execution } 12/31/2021 R Mahendran Java Applet 7

Destroy Method() public void destroy(): ü It is used to destroy the Applet. ü It is invoked only once. q public void destroy() { perform shutdown process } 12/31/2021 R Mahendran Java Applet 8

java. awt. Component class paint − Invoked immediately after the start() method, q any time the applet needs to repaint itself q It provides Graphics class object that can be used for drawing oval, rectangle, arc etc. q public void paint(Graphics g) { // Redisplay Content of window } 12/31/2021 R Mahendran Java Applet 9

Invoking an Applet The code attribute of the <applet> tag is required q It specifies the Applet class to run. Width and height are also required to specify the initial size of the panel /* <applet code=“java class" width="300" height="300"> </appl et> */ q 12/31/2021 R Mahendran Java Applet 10

Applet Display Methods AWT to perform Input and Output q Output string to an applet, use draw. String() q Member of the Graphics class q Void draw. String(String Message, int x, int y) 12/31/2021 R Mahendran Java Applet 11

Sample Program import java. applet. Applet; import java. awt. *; import java. awt. Graphics; public class First extends Applet { public void init() { set. Background(Color. cyan); set. Foreground(Color. red); } public void paint(Graphics g){ g. draw. String("welcome", 150); } } /*<applet code="First. class" width="300" height="300"> </applet>*/ 12/31/2021 R Mahendran Java Applet 12

Graphics class public void draw. Rect(int x, int y, int width, int height) draws a rectangle with the specified width and height. Example: public void paint(Graphics g) { g. draw. Rect(70, 100, 30); } n 12/31/2021 R Mahendran Java Applet 13

…Graphics class public void fill. Rect(int x, int y, int width, int height) it is used to fill rectangle with the default color and specified width and height. Example: public void paint(Graphics g) { g. fill. Rect(170, 100, 30); } 12/31/2021 R Mahendran Java Applet 14

…Graphics class public abstract void draw. Oval(int x, int y, int width, int height) n it is used to draw oval with the specified width and height. Example: public void paint(Graphics g) { g. draw. Oval(70, 200, 30); } n 12/31/2021 R Mahendran Java Applet 15

…Graphics class public void fill. Oval(int x, int y, int width, int height) q it is used to fill oval with the default color and specified width and height. Example: public void paint(Graphics g) { g. fill. Oval(170, 200, 30); } q 12/31/2021 R Mahendran Java Applet 16

…Graphics class Public void draw. Line(int x 1, int y 1, int x 2, int y 2) q it is used to draw line between the points(x 1, y 1) and (x 2, y 2). Example: public void paint(Graphics g) { g. draw. Line(20, 30, 20, 300); } q 12/31/2021 R Mahendran Java Applet 17

…Graphics class public void draw. Arc(int x, int y, int width, int height, int start. Angle, int arc. Angle) q it is used draw a circular or elliptical arc. Example: public void paint(Graphics g) { g. draw. Arc(90, 150, 30, 30, 270); } q 12/31/2021 R Mahendran Java Applet 18

…Graphics class public void fill. Arc(int x, int y, int width, int height, int start. Angle, int arc. Angle) n it is used to fill a circular or elliptical arc. Example: public void paint(Graphics g) { g. fill. Arc(270, 150, 30, 0, 180); } n 12/31/2021 R Mahendran Java Applet 19

…Graphics class public abstract void set. Color(Color c): q it is used to set the graphics current color to the specified color. Example: public void paint(Graphics g) { g. set. Color(Color. cyan); } q 12/31/2021 R Mahendran Java Applet 20

…Graphics class public abstract void set. Font(Font font) q it is used to set the graphics current font to the specified font. n Example public void paint(Graphics g) { g. set. Font(Font. ”Times. New. Roman”); } q 12/31/2021 R Mahendran Java Applet 21

Sample Code import java. applet. Applet; import java. awt. *; public class Graphics. Demo extends Applet{ public void paint(Graphics g){ g. set. Color(Color. red); g. draw. String("Welcome", 50); g. draw. Line(20, 30, 20, 300); g. draw. Rect(70, 100, 30); g. fill. Rect(170, 100, 30); g. draw. Oval(70, 200, 30); g. set. Color(Color. pink); g. fill. Oval(170, 200, 30); g. draw. Arc(90, 150, 30, 30, 270); g. fill. Arc(270, 150, 30, 0, 180); }} /*<applet code="Graphics. Demo. class" width=200 height=200></applet>*/ 12/31/2021 R Mahendran Java Applet 22

Displaying Image in Applet n n The java. awt. Graphics class provide a method draw. Image() to display the image. public boolean draw. Image(Image img, int x, int y, Image. Observer observer): It is used draw the specified image. 12/31/2021 R Mahendran Java Applet 23

Image in Applet n n n public Image get. Image(URL u, String ima ge){ } public URL get. Document. Base() it is used to return the URL of the document in which applet is embedded 12/31/2021 R Mahendran Java Applet 24

…Image in Applet Image picture public void init() { picture = get. Image(get. Document. Base(), "mahe. jpg"); } 12/31/2021 R Mahendran Java Applet 25

Sample Code import java. awt. *; import java. applet. *; public class Display. Image extends Applet { Image picture; public void init() { picture = get. Image(get. Document. Base(), "mahe. jpg"); } public void paint(Graphics g) { g. draw. Image(picture, 30, this); }} /*<applet code="Display. Image. class" width=200 height=200></applet>*/ 12/31/2021 R Mahendran Java Applet 26

Event Handling Most event to which your generated your program will respond are generated q when the user interacts with a GUI based program q Event supported number of packages include q Java. awt. event. q Several types of event include those generated by the mouse, the keyboard q 12/31/2021 R Mahendran Java Applet 27

…Event Handling n n perform event handling in AWT or Swing, we can perform it in applet Event handling in applet that prints a message by click on the button. 12/31/2021 R Mahendran Java Applet 28

import java. applet. *; import java. awt. event. *; public class Event. Applet extends Applet implements Action. Listener{ Button b; Text. Field tf; public void init(){ tf=new Text. Field(); tf. set. Bounds(30, 40, 150, 20); b=new Button("Click"); b. set. Bounds(80, 150, 60, 50); add(b); add(tf); b. add. Action. Listener(this); set. Layout(null); } public void action. Performed(Action. Event e){ tf. set. Text("Welcome"); } } /*<applet code="Event. Applet. class" width=200 height=200></applet>*/ 12/31/2021 R Mahendran Java Applet 29

Parameter in Applet q q Get any information from the HTML file as a parameter. Applet class provides a method named get. Parameter(). 12/31/2021 R Mahendran Java Applet 30

…Parameter in Applet Syntax: public String get. Parameter(String parame ter. Name) Example: <param name="msg" value="Welcome to a pplet"> 12/31/2021 R Mahendran Java Applet 31

Sample Code import java. applet. Applet; import java. awt. *; import java. awt. Graphics; public class Use. Param extends Applet{ public void init(){ set. Background(Color. red); set. Foreground(Color. green); } public void paint(Graphics g){ String str=get. Parameter("msg"); g. draw. String(str, 50); } } /*<applet code="Use. Param. class" width="300" height="300"> <param name="msg" value="Welcome to applet"> </applet> */ 12/31/2021 R Mahendran Java Applet 32

Thank You 12/31/2021 R Mahendran Java Applet 33
- Slides: 33