Download presentation
Presentation is loading. Please wait.
Published byLester Copeland Modified over 8 years ago
1
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001
2
Object-Oriented Programming OOPS encapsulates data(attributes) and methods(behaviors) into packages called objects. Java programming tends to object –oriented. In java, the unit of programming is the class from which objects are eventually instantiated. Java programmers concentrate on creating user-defined types called classes. The class contains data and set of functions that manipulate the data. The programmer uses built-in types and other classes as building blocks for constructing new user-defined types.
3
The Java Applet An applet is an java code that can be inserted in a HTML document. The java code runs in a appletviewer – a test utility for applets to execute an applet. The appletviewer executes an applet when a HTML document containing the applet is opened in a browser. The appletviewer loads the HTML file, determines from the file which applet to load and begins the execution of the applet.
4
A Simple Java Applet import java.awt.*; import java.applet.Applet; public class welcomeApplet extends Applet { public void paint(Graphics g) { g.drawString("Welcome to Java Programming",25,25); }
5
1. Java contains many predefined pieces called classes or grouped into packages. The classes are included by using the statement import java.awt.Graphics; -Line1 import javax.swing.JApplet; - Line 2 The import statements tell the compiler where to locate the classes require to compile the java applet. Line 2 specifies that class Japplet is located in package javax.swing and class Graphics is located in the package java.awt. 2. Every Java applet is composed of atleast one class definition. You create your own class using pieces of existing class definition. Java uses inheritance to create new classes from existing class definitions.
6
3. The extends keyword followed by class name indicates the class from which our new class inherits existing pieces. 4. To inherit from existing classes, the programmer does not need to know every detail of the class and use every functions of the class, he uses only necessary functions. 5. The welcomeApplet that is inherited from JApplet uses the paint method of the class. 6. JApplet class consists start, init and paint. These function are automatically called when the applet begins the execution. These function contain nothing in the JApplet class. The programmer overrides this function with actions he wants. 7. The paint has been defined in the JApplet to receive a Graphics object which is in the package java.awt. The Graphics contains a set of function to print information on the screen.
7
8. The paint function prints the message on the screen. The syntax is g.drawString(“This is a message string”,25,25) – The function requires three parameters string, x- coordinate and y-coordinate. 9. The x-coordinate is equivalent as column and y- coordinate is the row equivalent. 10. The upper left-hand coordinates are 0,0.
8
Embedding the Applet in HTML Code The applet is inserted with the applet tag. The applet contains the width and height of the browser window where the applet is loaded. The default width and height is 0,0.
9
Running the appletviewer without.html or.htm does not load the applet in the browser. The applet tag has the code which specifies the class file to loaded. Placing additional characters such as commas between components in the tag may cause the appletviewer or browser to produce an error message. Forgeting an tag prevents the applet from loading into the browser.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.