Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.

Similar presentations


Presentation on theme: "1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program."— Presentation transcript:

1 1 Applets Programming

2 Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program. An applet is a kind of Java program that can be run across the Internet.

3 3 Introduction Applets are small Java programs that are embedded in Web pages. They can be transported over the Internet from one computer (web server) to another (client computers). They transform web into rich media and support the delivery of applications via the Internet.

4 4 Applet: Making Web Interactive and Application Delivery Media Hello Hello Java <app= “Hello”> 4 APPLET Development “hello.java” AT SUN.COM The Internet hello.class AT SUN’S WEB SERVER 2 31 5 Create Applet tag in HTML document Accessing from Your Organisation The browser creates a new window and a new thread and then runs the code

5 5 How Applets Differ from Applications Applets don’t use the main() method, but when they are load, automatically call certain methods (init, start, paint, stop, destroy). They are embedded inside a web page and executed in browsers. They cannot read from or write to the files on local computer. They cannot communicate with other servers on the network. They cannot run any programs from the local computer. They are restricted from using libraries from other languages. The above restrictions ensures that an Applet cannot do any damage to the local system.

6 Defining an Applet There is an older class, Applet, which has been superseded by the JApplet class. The class Applet is in the package java.awt.Applet The class JApplet is in the package javax.swing.JApplet

7 Applets in the Class Hierarchy

8 Running an Applet An applet class is compiled in the same way as any other Java class However, an applet is run differently from other Java programs The normal way to run an applet is to embed it in an HTML document The applet is then run and viewed through a Web browser

9 An applet can also be viewed using an applet viewer An applet viewer is a program designed to run an applet as a stand-alone program The Java appletviewer can be used to run an applet: appletviewer FirstApplet.html It may be necessary, however, to create the HTML document, and place the applet in it Running an Applet (Cont’d)

10 Designing an Applet An applet class can be designed as a derived class of Applet or JApplet in much the same way that regular Swing GUIs are defined as derived classes of Frame or Jframe. However, an applet normally defines no constructors. The method init performs the initializations that would be performed in a constructor for a regular Swing GUI

11 11 Building Applet Code: An Example //HelloWorldApplet.java import java.applet.Applet; import java.awt.*; public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString ("Hello World of Java!",25, 25); }

12 12 Embedding Applet in Web Page Hello World Applet Hi, This is My First Java Applet on the Web!

13 13 Accessing Web page (runs Applet)

14 Inserting an Applet in an HTML Document An applet can be placed in an HTML document with an applet tag: <applet code="PathToApplet" width=Number1 height=Number2> If given a.class file name only, then the HTML file and the applet file must be in the same directory The PathToApplet can be a full or relative path name

15 Note that the name of the.class file, not the.java file, is given Note also that the width and height of the applet is given in this command, and not within the applet class definition The width and height are in pixels The following code, when placed in an HTML document, will display the calculator applet in a browser as shown <applet code="AppletCalculator.class" width=400 height=300> Inserting an Applet in an HTML Document (Cont’d)

16 An Applet in an HTML Document Vampire Control......

17 The Browser View of Applets

18 Using an Old Web Browser An old browser may not be able to run applets from an HTML document: Even if a java application runs correctly on the same system. Web browsers do not use the same Java Virtual Machine used to run regular Java applications. An old browser will have an old Java Virtual Machine, or perhaps, no Java Virtual Machine. However, an applet viewer will work, as long as a recent version of Java is installed.

19 19 Applet Life Cycle Every applet inherits a set of default behaviours from the Applet class. As a result, when an applet is loaded, it undergoes a series of changes in its state. The applet states include: Initialisation – invokes init() Running – invokes start() Display – invokes paint() Idle – invokes stop() Dead/Destroyed State – invokes destroy()

20 20 Applet States Initialisation – invokes init() – only once Invoked when applet is first loaded. Running – invokes start() – more than once For the first time, it is called automatically by the system after init() method execution. It is also invoked when applet moves from idle/stop() state to active state. For example, when we return back to the Web page after temporary visiting other pages. Display – invokes paint() - more than once It happens immediately after the applet enters into the running state. It is responsible for displaying output. Idle – invokes stop() - more than once It is invoked when the applet is stopped from running. For example, it occurs when we leave a web page. Dead/Destroyed State – invokes destroy() - only once This occurs automatically by invoking destroy() method when we quite the browser.

21 21 Applet Life Cycle Diagram Born RunningIdle Dead Begin init() start() paint() stop() start() destroy() End

22 22 Passing Parameters to Applet Hello World Applet Hi, This is My First Communicating Applet on the Web! <APPLET CODE="HelloAppletMsg.class" width=500 height=400>

23 23 Applet Program Accepting Parameters //HelloAppletMsg.java import java.applet.Applet; import java.awt.*; public class HelloAppletMsg extends Applet { String msg; public void init() { msg = getParameter("Greetings"); if( msg == null) msg = "Hello"; } public void paint(Graphics g) { g.drawString (msg,10, 100); } This is name of parameter specified in PARAM tag; This method returns the value of paramter.

24 24 HelloAppletMsg.html

25 25 What happen if we don’t pass parameter? See HelloAppletMsg1.html Hello World Applet Hi, This is My First Communicating Applet on the Web! <APPLET CODE="HelloAppletMsg.class" width=500 height=400>

26 26 getParameter() returns null. Some default value may be used.

27 27 Displaying Numeric Values //SumNums.java import java.applet.Applet; import java.awt.*; public class SumNums extends Applet { public void paint(Graphics g) { int num1 = 10; int num2 = 20; int sum = num1 + num2; String str = "Sum: "+String.valueOf(sum); g.drawString (str,100, 125); }

28 28 SunNums.html Hello World Applet Sum of Numbers

29 29 Applet – Sum Numbers

30 30 Interactive Applets Applets work in a graphical environment. Therefore, applets treats inputs as text strings. We need to create an area on the screen in which use can type and edit input items. We can do this using TextField class of the applet package. When data is entered, an event is generated. This can be used to refresh the applet output based on input values.

31 31 Interactive Applet Program..(cont) //SumNumsInteractive..java import java.applet.Applet; import java.awt.*; public class SumNumsInteractive extends Applet { TextField text1, text2; public void init() { text1 = new TextField(10); text2 = new TextField(10); text1.setText("0"); text2.setText("0"); add(text1); add(text2); } public void paint(Graphics g) { int num1 = 0; int num2 = 0; int sum; String s1, s2, s3; g.drawString("Input a number in each box ", 10, 50); try { s1 = text1.getText(); num1 = Integer.parseInt(s1); s2 = text2.getText(); num2 = Integer.parseInt(s2); } catch(Exception e1) {}

32 32 Interactive Applet Program. sum = num1 + num2; String str = "THE SUM IS: "+String.valueOf(sum); g.drawString (str,100, 125); } public boolean action(Event ev, Object obj) { repaint(); return true; }

33 33 Interactive Applet Execution

34 Components can be added to an applet in the same way that a component is added to a Frame or JFrame The method add is used to add components to an applet in the same way that components are added to a Frame or Jframe Designing an Applet

35 Java Applets: An Example 16 } Output using an applet viewer

36 How Applets Differ from Swing GUIs? Some of the items included in a Swing GUI are not included in an applet Applets do not contain a main or setVisible method Applets are displayed automatically by a Web page or an applet viewer Applets do not have titles Therefore, they do not use the setTitle method They are normally embedded in an HTML document, and the HTML document can add any desired title

37 Applets do not use the setSize method The HTML document takes care of sizing the applet Applets do not have a close-window button Therefore, they do not have a setDefaultCloseOperation method When the HTML document containing the applet is closed, then the applet is automatically closed How Applets Differ from Swing GUIs? (Cont’d)

38 Menus in a JApplet Menus are constructed and added to a JApplet as they are for a Jframe JApplet has a method named setJMenuBar that behaves the same as the setJMenuBar method of a JFrame JApplet can also have menu bars added to a JApplet or to a panel that is part of the JApplet using the add method

39 39 Summary Applets are designed to operate in Internet and Web environment. They enable the delivery of applications via the Web. This is demonstrate by things that we learned in this lecture such as: How do applets differ from applications? Life cycles of applets How to design applets? How to execute applets? How to provide interactive inputs?


Download ppt "1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program."

Similar presentations


Ads by Google