Вы находитесь на странице: 1из 23

Programming Language (JAVA)

Unit 6.11 Applets

Presentation 2

Objectives
At the end of this presentation, you will be able to : Write an applet program to print a text using applet class and its methods View an applet using a Web browser

Applet State Transition

Hands-On!
Program HelloJava.java prints the string hello in the applet window, which is contained in the browser window.

The Applet Class


All the applets that are created should be subclasses of the Applet class.
Applet programs should import two packages, awt and applet.

Class Inheritance of Applet Class

Execution of Applet

Applet Viewer
Appletviewer is a standard JDK tool used to run an applet program as a standalone application.
A comment statement, containing the applet tag, is added to the java file.

Hands-On!
Program Welcome.java prints a string in an applet window using appletviewer.

<APPLET> tag
Syntax

/* <applet code = "<applet name>" width = <pixels> height = <pixels> </applet> */

Activity 6.11.1
Step 1: Open the file applet_view.java.
Step 2: Add the given comment at the end of the file.

/*
<applet code=applet_view.class width= 200 height=100> */

Activity 6.11.1 (Contd..)


Step 3: Save the program.
Step 4: Compile the program. Step 5: Run and observe the output by executing the program using applet viewer as

appletviewer applet_view.java

Executing Applets Through HTML


Applets can also be viewed using a Web browser.
Steps to view an applet in a Web browser.

Step 1: Compile the java file.


Step 2: Include the applet tag in an HTML file.

Step 3: Open the HTML file in a Web browser.

Web Page Template

Sections of Web page


Comment Section Contains comments about the Web page.
Head Section Contains the title of the Web page. Body Section Contains text and commands that specify how the text, image, animation etc. should appear in the Web page. The Applet tag is inserted in the body section.

Sample Applet

Hands-On!
Program add.java calculates the sum of two numbers and prints it in the applet window.

Activity 6.11.2
Step 1: Open A_11_2.java from Student data file. Step 2: Compile the above program.
Step 3: Edit the code line 7 as public class

FirstApplet.
50.

Step 4: Edit the code line 11 as Welcome, 150, Step 5: Save the program as FirstApplet.java. Step 6: Compile the Java program and save the .class file in the same directory.

Activity 6.11.2 (Contd..)


Step 7: Type the following HTML code and save it as a.html.

1. <HTML> 2. <body> 3. <applet code=A_11_2.class width=300 height=300> 4. </applet> 5. </body> 6. </HTML>

Activity 6.11.2 (Contd..)


Step 8: Replace the text A_11_2.class in line 3 with FirstApplet.class.
Step 9: Save the program. Step 10: Run the HTML file in the browser window and observe the applet window.

Lab Exercise
1. Write an applet program to print your friends name as a string and display the applet window through the browser and applet viewer.
2. Write an applet program to print the difference between two numbers and display the applet window through browser.

Summary
In this presentation, you learnt the following Applets created are subclasses of Applet class. All applets must import java.applet package. Applet class is contained in the java.applet package. The Applet class provides the necessary support for applets. All applets should also import java.awt package.

Assignment
1. Write an applet program to display the name of your school.

Вам также может понравиться