Introduction To Applets methods and simple examples

MsPariyalNituLaxman 96 views 31 slides Sep 30, 2024
Slide 1
Slide 1 of 31
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31

About This Presentation

Introduction To Applets methods and simple examples


Slide Content

Introduction To Applets Unit-V Presented by: Ms Nitu L. Pariyal 1

Introduction Small java programs. Used in internet computing. They can be transported over the internet from one computer to another and run using appletviewer or any web browser. It can Perform arithmetic operations. Display graphics Play sound Accept user input create animation and Play interactive games. 2

Local and Remote Applet We can embed applets into web pages in two ways: We can write our own applet and embed them into web pages. We can download an applet from a remote computer system and then embed it into a web page. Applet developed locally and stored in local system is called as local applet. So internet is not required. Remote applet is developed by someone and stored on other system and we can download the remote applet to our computer via internet and run it on our computer. In order to run remote applet we need the address of applet on the web. This URL must be mentioned in applet’s HTML document. 3

How Applets Differ from Application There are significant differences between applet and application programs. Are not full-featured application programs. Usually written to do small task. Designed for use on internet, they impose certain limitations and restrictions in their design. Applets do not use main() method for initiating the execution of code. Applets, when loaded, automatically call certain methods of Applet class to start and execute the appplet code. Unlike stand-alone applications, applets cannot be run independently. They are run from inside a web page using a special feature known as HTML tag. Applets can not read from or write to the files in local computer. 4

How Applets Differ from Application Applets can not communicate with other servers on the network. Applets can not run any program from the local computer. Are restricted from using libraries from other languages such as C or C++ Theses restrictions & limitations are placed in the interest of security of systems, which ensures applet can not do any damage to the local system. 5

First, let us consider the situations when we might need to use applets When we need something dynamic to be included in the display of a web page. When we require some flash outputs. When we want to create a program & make it available on the internet for us by others on their computers. Preparing to Write Applet 6

Applet code uses two calsses Applet & Graphics Steps involved in developing and testing in applet are: Building an applet code (.java file) Creating an executable applet (.class file). Designing a web page using HTML tags. Preparing <APPLET> tag Incorporating <APPLET> tag into the web page. Creating HTML file Testing the applet code. Building Applet code 7

The genealogy of Applet java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet 8

Lifecycle of Java Applet Applet is initialized Applet is started Applet is painted Applet is stopped Applet is destroyed The java.applet.Applet class 4 lifecycle methods and java.awt.Component class provides 1 Life cycle methods for an applet 9

Lifecycle of Java Applet For creating any applet java.applet.Applet class must be inherited. It provides 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(Graphics g): is used to paint the applet. It provides Graphics class object that can be used for drawing oval, rcatngle, arc etc. 11

12 import java.awt.*; import java.applet.*; public class WelcomeApplet extends Applet { public void init() { } public void paint(Graphics g) { g.drawString("Welcome to Java Programming!", 25, 25 ); } } extends allows us to inherit the capabilities of class Applet . Method paint is guaranteed to be called in all applets. Its first line must be defined as above.  2003 Prentice Hall, Inc. All rights reserved. Modified by Evan Korth

13 /* <applet code=" WelcomeApplet.class " width="300" height="300"> </applet> */ import java.awt.*; import java.applet .*; public class WelcomeApplet extends Applet { public void init() { } public void paint(Graphics g) { g.drawString ("Welcome to Java Programming!", 25, 25 ); } }

14

15 import java.awt.*; import java.applet.*; public class WelcomeApplet extends Applet { public void init() { } public void paint(Graphics g) { g.drawString("Welcome to Java Programming!", 25, 25 ); } } <html> <body> <applet code="WelcomeApplet.class width="300" height="300"> </applet> </body> </html> WelcomeApplet.java welcomeApplet.html

16 Simple Java Applet: Drawing a String Our class inherits method paint from Applet By default, paint has empty body Override (redefine) paint in our class Methods init , start and paint . Guaranteed to be called automatically Our applet gets "free" version of these by inheriting from Applet Free versions have empty body (do nothing) Every applet does not need all three methods Override the ones you need Applet container “draws itself” by calling method paint public void paint( Graphics g ) Modified by Evan Korth

17 Simple Java Applet: Drawing a String Body of paint Method drawString (of class Graphics ) Called using Graphics object g and dot ( . ) Method name, then parenthesis with arguments First argument: String to draw Second: x coordinate (in pixels) location Third: y coordinate (in pixels) location g.drawString( "Welcome to Java Programming!" , 25 , 25 );

18 Simple Java Applet: Drawing a String Running the applet Compile javac WelcomeApplet.java If no errors, bytecodes stored in WelcomeApplet.class Create an HTML file Loads the applet into appletviewer or a browser Ends in . htm or .html To execute an applet Create an HTML file indicating which applet the browser (or appletviewer ) should load and execute Modified by Evan Korth

19 Simple Java Applet: Drawing a String <html> <applet code = "WelcomeApplet.class" width = "300" height = "45" > </applet> </html>

public void init( ) Applet enters initialization state when it is first loaded. This is the first method to execute. It is an ideal place to initialize variables. It is the best place to define the GUI Components (buttons, text fields, scrollbars, etc.), lay them out, and add listeners to them. It the place to load images or fonts as well as set up colors. Almost every applet you ever write will have an init( ) method. 20

Running State [public void start ( ) ] Not always needed. Called after init( ). Called each time the page is loaded and restarted. Used mostly in conjunction with stop( ) start() and stop( ) are used when the Applet is doing time-consuming calculations that you don’t want to continue when the page is not in front. We may override the start() method to create a thread to control the applet. 21

Idle or Stopped State [public void stop( )] Not always needed. Called when the browser leaves the page. Called just before destroy( ). Use stop( ) if the applet is doing heavy computation that you don’t want to continue when the browser is on some other page. Used mostly in conjunction with start(). 22

Dead State public void destroy( ) An applet is said to be dead when it is removed from memory. This occurs automatically by invoking the destroy() method when we quit the browser. Seldom (rarely) needed. Called after stop( ). Use to explicitly release system resources (like threads). System resources are usually released automatically. 23

Methods are called in this order 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 init() start() stop() destroy() do some work 24

25 More about HTML tag

26 More about HTML tag

Write a java applet program for addition of two numbers import java.awt.*; import java.applet .*; public class numval extends Applet { public void paint(Graphics g) { int val1=10, val2=20; int sum=val1+val2; String str ="sum:" + String.valueOf (sum); g.drawString (str,100,150); } } Displaying Numerical Values 27

/* < html> <applet code= numval.class width=200 height=200> </applet> < html >*/ 28

Sample Graphics methods A Graphics is something you can paint on 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 ); g.drawString (“Hello”, 20, 20); Hello 29

import java.awt.*; import java.applet .*; public class Hello extends Applet { String msg ; public void init() { setBackground ( Color.red ); setForeground ( Color.blue ); } public void paint(Graphics g) { msg ="Hello"; g.setFont (new Font("Times New Roman", Font.BOLD , 24)); g.drawString (msg,20,20); } } <html> <applet code= Hello.class width=450 height=500> </applet> </html>` 30

31