04-10-2012, 01:30 PM
Applet
Intro applet.doc (Size: 198.5 KB / Downloads: 33)
Introduction
An applet is a Java program that runs on a web page. An applet is not a stand-alone application, and it does not have a main() routine. In fact, an applet is an object rather than a class. In the Java language, a class represents a data type. Every class defines a new data type. For example, you can define a class named Point to represent a data point in the two dimensional X and Y graphing system. This class can define fields (each of type double) to hold the X and Y coordinates of a point and methods to manipulate and operate on the point. The Point class is a new data type. When discussing data types, it is important to distinguish between the data type itself and the values the data type represents. char is a data type: it represents Unicode characters. But, a char value represents a single, specific character. A class is a data type; a class value is called an object. But, when compiling applet code using thejavac.exe compiler that ships with Sun’s J2EE SDK that contains the JDK, a class file is generated. That file, which has the same name as the class defined in the source code, is referred to within the applet tags that reside in the body of an HTML document. For instance, if your applet is named myApplet.java, then after using the javac.exe compiler, you will have a myApplet.class file. This file must be contained in quotes when embedded in the applet tags. An important point to remember is that the Swing API and the Java 2D API use a ‘J’ prefix before any component: JApplet,JFrame, JPanel, etc. To keep things simple, some applet code in this article will use the old Applet class defined in the java.awt package.
Underlying Concepts
The above applet involved drawing a rectangle in which the squares would have a color based on the condition that if the row and column square were both odd or even, then the color would be Red. If not, the color would be Black. To fill the rectangle with squares, a for loop iterates over 8 elements to form 8 rows, and 8 elements to form 8 columns. The if statement, as we know, executes a block of code if a condition has a boolean true. If not (else), the opposite type code is executed. So, the understanding applet code not only involves color, graphics, and coordinates, but also controls flow structures and subroutines.