Cool web site - Chapter 3 Introduction to Java Applets 143

Chapter 3 Introduction to Java Applets 143 Many HTML codes (referred to as tags) come in pairs. HTML tags begin with a left angle bracket < and end with a right angle bracket >. Normally, the applet and its corresponding HTML file are stored in the same directory on disk. The first component of the tag indicates the file containing the compiled applet class. The second and third components of the tag indicate the width and the heightof the applet in pixels. Generally, each applet should be less than 800 pixels wide and 600 pixels tall. The appletviewer only understands the and HTML tags, so it is sometimes referred to as the minimal browser. It ignores all other HTML tags. Method drawLine of class Graphics draws lines. The method requires four arguments representing the two end points of the line on the applet the x-coordinate and y-coordinate of the first end point in the line and the x-coordinate and y-coordinate of the second end point in the line. All coordinate values are specified with respect to the upper-left corner (0, 0) coordinate of the applet. Primitive data type double stores double-precision floating-point numbers. Primitive data type float stores single-precision floating-point numbers. A double requires more memory to store a floating-point value, but stores it with approximately twice the precision of a float (15 significant digits for double vs. seven significant digits for float). The importstatements are not required if you always use the complete name of a class, including the full package name and class name (e.g., java.awt.Graphics). The asterisk (*) notation after a package name in an importindicates that all classes in the package should be available to the compiler so the compiler can ensure that the classes are used correctly. This allows programmers to use the shorthand name (the class name by itself) of any class from the package in the program. Every instance (object) of a class contains one copy of each of that class s instance variables. Instance variables are declared in the body of a class definition, but not in the body of any method of that class definition. An important benefit of instance variables is that their identifiers can be used in all methods of the class. Variables defined in the body of a method are known as local variables and can be used only in the body of the method in which they are defined. Instance variables are always assigned a default value, and local variables are not. Method initnormally initializes the applet s instance variables (if they need to be initialized to a value other than their default value) and performs any tasks that should occur only once when the applet begins execution There are actually two types of variables in Java primitive data type variables and references. References refer to objects in a program. References actually contain the location in the computer s memory of an object. A reference is used to send messages to (i.e., call methods on) the object in memory. As part of the message (method call), we provide the data (arguments) that the method requires to do its task. A variable is similar to an object. The primary difference between a variable and an object is that an object is defined by a class definition that can contain both data (instance variables) and methods, whereas a variable is defined by a primitive (or built-in) data type (one of char, byte, short, int, long, float, doubleor boolean) that can contain only data. A variable can store exactly one value at a time, whereas one object can contain many individual data members. If the data type used to declare a variable is a class name, the identifier is a reference to an object and that reference can be used to send messages to (call methods on) that object. If the data type used to declare a variable is one of the primitive data types, the identifier is a variable that can be used to store in memory or retrieve from memory a single value of the declared primitive type. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply