Archive for August, 2007

Web hosting company - Chapter 3 Introduction to Java Applets 131 ly,

Friday, August 31st, 2007

Chapter 3 Introduction to Java Applets 131 ly, there are many different browser versions being used worldwide. Some support only Java 1.0 and many support Java 1.1. However, few support the Java 2 Platform. Also, even the browsers that support Java 1.1 do so inconsistently. In Section 3.6.1, we demonstrate an applet executing in Netscape Navigator 6, which supports Java 2. In Section 3.6.2, we demonstrate how to use the Java Plug-in to execute Java 2 applets in other Web browsers such as Microsoft Internet Explorer or earlier versions of Netscape Navigator. Portability Tip 3.3 Not all Web browsers support Java. Those that do often support different versions and are not always consistent across all platforms. 3.6.1 Viewing Applets in Netscape Navigator 6 When you install Netscape Navigator 6, one of the browser components in the default installation is Java 2. Once installed, you can simply load an applet s HTML file into the browser to execute the applet. You can download and install Netscape 6 from www.netscape.com by clicking the Download button at the top of the Web page. After installing the browser, open the program. On Windows, Netscape 6 typically places an icon on your desktop during the install process. In the File menu, click Open File to select an HTML document from your local computer s hard disk. In the Open File dialog, navigate to the location of the HTML file of Fig. 3.11. Select the file name WelcomeLines.html by clicking it, then click the Open button to open the file in the browser. In a few moments, you should see the applet of Fig. 3.10 appear in the browser window as shown in Fig. 3.14. 3.6.2 Viewing Applets in Other Browsers Using the Java Plug-In If you would like to use the features of the Java 2 platform in an applet and execute that applet in a browser that does not support Java 2, Sun provides the Java Plug-in to bypass a browser s Java support and use a complete version of the Java 2 Runtime Environment (J2RE) that is installed on the user s local computer. If the J2RE does not already exist on the client machine, it can be downloaded and installed dynamically. Performance Tip 3.1 Because of the size of the Java Plug-in, it is difficult and inefficient to download the Plug-in for users with slower Internet connections. For this reason, the Plug-in is ideal for corporate intranets where users are connected to a high-speed network. Once the Plug-in is downloaded, it does not need to be downloaded again. You must indicate in the HTML file containing an applet that the browser should use the Java Plug-in to execute the applet. To do so, requires that you convert the and tags into tags that load the Java Plug-in and execute the applet. Sun provides a conversion utility called the Java Plug-in 1.3 HTML Converter2 that performs the conversion for you. Complete information on downloading and using the Java Plug-in and the HTML Converter are available at the Web site java.sun.com/products/plugin/ Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

130 Introduction to Java Applets Chapter 3 gle (Web site optimization)

Thursday, August 30th, 2007

130 Introduction to Java Applets Chapter 3 gle in pixels and the height of the rectangle in pixels, respectively. This particular statement draws a rectangle starting at coordinate (15, 10) that is 270 pixels wide and 20 pixels tall. Common Programming Error 3.8 It is a logic error to supply a negative width or negative height as an argument to Graphics method drawRect. The rectangle will not be displayed and no error will be indicated. Common Programming Error 3.9 It is a logic error to supply two points (i.e., pairs of x- and y-coordinates) as the arguments to Graphics method drawRect. The third argument must be the width in pixels and the fourth argument must be the height in pixels of the rectangle to draw. Common Programming Error 3.10 It is normally a logic error to supply arguments to Graphics method drawRect that cause the rectangle to draw outside the applet s viewable area (i.e., the width and height of the applet as specified in the HTML document that references the applet). Either increase the applet s width and height in the HTML document or pass arguments to method drawRect that cause the rectangle to draw inside the applet s viewable area. Line 48 g.drawString( “The sum is ” + sum, 25, 25 ); sends the drawString message to the Graphics object to which g refers (calls the Graphics object s drawString method). The expression “The sum is ” + sum from the preceding statement uses the string concatenation operator + to concatenate the string “Thesumis ” and sum (converted to a string) to create the string drawString displays. Notice again that the preceding statement uses the instance variable sum even though method paint does not define sum as a local variable. The benefit of defining sum as an instance variable is that we were able to assign sum a value in init and use sum s value in the paint method later in the program. All methods of a class are capable of using the instance variables in the class definition. Software Engineering Observation 3.10 The only statements that should be placed in an applet s init method are those that are directly related to the one-time initialization of an applet s instance variables. The applet s results should be displayed from other methods of the applet class. Results that involve drawing should be displayed from the applet s paint method. Software Engineering Observation 3.11 The only statements that should be placed in an applet s paint method are those that are directly related to drawing (i.e., calls to methods of class Graphics) and the logic of drawing. Generally, dialog boxes should not be displayed from an applet s paint method. 3.6 Viewing Applets in a Web Browser We demonstrated several applets in this chapter using the appletviewer applet container. As we mentioned, applets also can execute in Java-enabled Web browsers. Unfortunate Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Chapter 3 Introduction to Java Applets 129 Lines (Starting a web site)

Thursday, August 30th, 2007

Chapter 3 Introduction to Java Applets 129 Lines 26 27 // obtain second number from user secondNumber = JOptionPane.showInputDialog( “Enter second floating-point value” ); read the second floating-point value from the user by displaying an input dialog. Lines 30 31 number1 = Double.parseDouble( firstNumber ); number2 = Double.parseDouble( secondNumber ); convert the two strings input by the user to double values for use in a calculation. Method Double.parseDouble (a static method of class Double) converts its String argument to a double floating-point value. Class Double is in package java.lang. The floating-point value returned by parseDouble in line 30 is assigned to variable number1. The floating-point value returned by parseDouble in line 31 is assigned to variable number2. Software Engineering Observation 3.9 Each primitive data type (such as int or double) has a corresponding class (such as Integer or Double) in package java.lang. These classes (commonly known as type- wrapper classes) provide methods for processing primitive data type values (such as converting a String to a primitive data type value or converting a primitive data type value to a String). Primitive data types do not have methods. Therefore, methods related to a primitive data type are located in the corresponding type-wrapper class (e.g., method parseDouble that converts a String to a double value is located in class Double). See the online API documentation for the complete details of the methods in the type-wrapper classes. The assignment statement at line 34 sum = number1 + number2; calculates the sum of the values stored in variables number1 and number2 and assigns the result to variable sum using the assignment operator =. The statement is read as sum gets the value of number1 + number2. Notice that instance variable sum is used in method init even though sum was not defined in method init. We can use sum in init (and all other methods of the class), because sum is an instance variable. At this point the applet s init method returns and the applet container calls the applet s start method. We did not define start in this applet, so the one inherited from class JApplet is called here. Normally, the start method is used with an advanced concept called multithreading. See Chapter 15 and Chapter 18 for typical uses of start. Next, the applet container calls the applet s paint method. In this example, method paint draws a rectangle in which the result of the addition will appear. Line 45 g.drawRect( 15, 10, 270, 20 ); sends the drawRect message to the Graphics object to which g refers (calls the Graphics object s drawRect method). Method drawRect draws a rectangle based on its four arguments. The first two integer values represent the upper-left x-coordinate and upper-left y-coordinate where the Graphics object begins drawing the rectangle. The third and fourth arguments are non-negative integers that represent the width of the rectan Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

My web site - 128 Introduction to Java Applets Chapter 3 quires

Wednesday, August 29th, 2007

128 Introduction to Java Applets Chapter 3 quires to do its task. The Graphics object uses this data to draw the String at the specified location. The identifiers number1, number2 and sum are the names of variables. 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 may contain many individual pieces of data. The distinction between a variable and a reference is based on the data type of the identifier, which is stated in a declaration. If the data type 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 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. Software Engineering Observation 3.8 A hint to help you determine if an identifier is a variable or a reference is the variable s data type. By convention all class names in Java start with a capital letter. Therefore, if the data type starts with a capital letter, normally you can assume that the identifier is a reference to an object of the declared type (e.g., Graphics g indicates that g is a reference to a Graphics object). Lines 22 23 // obtain first number from user firstNumber = JOptionPane.showInputDialog( “Enter first floating-point value” ); read the first floating-point number from the user. JOptionPane method showInput- Dialog displays an input dialog that prompts the user to enter a value. The user types a value in the input dialog s text field, then clicks the OK button to return the string the user typed to the program. If you type and nothing appears in the text field, position the mouse pointer in the text field and click the mouse to make the text field active. Variable first- Numberis assigned the result of the call to JOptionPane.showInputDialog operation with an assignment statement. The statement is read as firstNumber gets the value of JOptionPane.showInputDialog(”Enterfirstfloating-point value”). In lines 22 23, notice the method call syntax. At this point, we have seen two different ways to call methods. This statement uses the static method call syntax introduced in Chapter 2. All static methods are called with the syntax ClassName.methodName( arguments ) Also in this chapter, we have called methods of class Graphicswith a similar syntax that started with a reference to a Graphics object. Generically, this syntax is referenceName.methodName( arguments ) This syntax is used for most methods calls in Java. In fact, the applet container uses this syntax to call methods init, start and paint on your applets. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Chapter 3 Introduction to Java (Best web hosting site) Applets 127 The

Tuesday, August 28th, 2007

Chapter 3 Introduction to Java Applets 127 The applet of Fig. 3.12 contains two methods init (lines 14 35) and paint (lines 38 50). When an applet container loads an applet, the container creates an instance of the applet class and calls its initmethod. The applet container calls method init only once during an applet s execution. Method init normally initializes the applet s instance variables (if they need to be initialized to a value other than their default value) and performs tasks that should occur only once when the applet begins execution. As we will see in later chapters, the applet s init method typically creates the applet s graphical user interface. Software Engineering Observation 3.7 The order in which methods are defined in a class definition has no effect on when those methods are called at execution time. However, following conventions for the order in which methods are defined improves program readability and maintainability. The first line of the init method always appears as public void init() indicating that init is a public method that returns no information (void) when it completes and receives no arguments (empty parentheses after init) to perform its task. The left brace (line 15) marks the beginning of init s body, and the corresponding right brace (line 35) marks the end of init. Lines 16 17 String firstNumber; // first string entered by user String secondNumber; // second string entered by user declare local String variables firstNumber and secondNumber in which the program stores the Strings input by the user. Lines 18 19 double number1; // first number to add double number2; // second number to add declare local variables number1 and number2 of primitive data type double these variables hold floating-point values. Unlike sum, number1 and number2 are not instance variables, so they are not initialized to 0.0 (the default value of double instance variables). As an important aside, there are actually two types of variables in Java primitive data type variables (normally called variables) and reference variables (normally called references). The identifiers firstNumber and secondNumber are actually references names that are used to refer to objects in the program. Such references actually contain the location of an object in the computer s memory. In our preceding applets, method paint actually receives a reference called g that refers to a Graphics object. Statements in method paint use that reference to send messages to the Graphics object. These messages are calls to methods (like drawString, drawLine and drawRect) that enable the program to draw. For example, the statement g.drawString( “Welcome to Java Programming!”, 25, 25 ); sends the drawString message to the Graphics object to which g refers. As part of the message, which is simply a method call, we provide the data that drawString re Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

126 Introduction to Java Applets Chapter 3 Software (Web design company)

Monday, August 27th, 2007

126 Introduction to Java Applets Chapter 3 Software Engineering Observation 3.5 Many packages have subpackages. For example, the java.awt package has subpackage event for the package java.awt.event. When the compiler encounters an import statement that uses the * (e.g., java.awt.*) notation to indicate that a program uses multiple classes from a package, the compiler does not load classes from the subpackage event. Thus, you cannot define an import of java.* to search for classes from all Java core packages. Software Engineering Observation 3.6 When using import statements, separate import statements must be specified for each package used in a program. Common Programming Error 3.6 Assuming that an import statement for an entire package (e.g., java.awt.*) also imports classes from subpackages in that package (e.g., java.awt.event.*) results in syntax errors for the classes from the subpackages. There must be separate import statements for every package from which classes are used. Remember that applets inherit from the JApplet class, so they have all the methods that an applet container requires to execute the applet. Line 10 public class AdditionApplet extends JApplet { begins class AdditionApplet s definition and indicates that it inherits from JApplet. All class definitions start with an opening left brace (end of line 10), {, and end with a closing right brace, } (line 52). Common Programming Error 3.7 If braces do not occur in matching pairs, the compiler indicates a syntax error. Line 11 double sum; // sum of values entered by user is an instance variable declaration every instance (object) of the class contains one copy of each instance variable. For example, if there are 10 instances of this applet executing, each instance has its own copy of sum. Thus, there would be 10 separate copies of sum (one per applet). Programmers declare instance variables in the body of the class definition, but outside the bodies of all the class s method definitions. The preceding declaration states that sumis a variable of primitive type double. A benefit of instance variables is that all the methods of the class can use the instance variables. Until now, we declared all variables in an application s main method. 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. Another distinction between instance variables and local variables is that instance variables have default values and local variables do not. The default value of variable sum is 0.0, because sum is an instance variable. Good Programming Practice 3.3 Explicitly initializing instance variables rather than relying on automatic initialization improves program readability. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Chapter 3 Introduction (Web page design) to Java Applets 125 1

Monday, August 27th, 2007

Chapter 3 Introduction to Java Applets 125 1 2 3 4 Fig. 3.13 AdditionApplet.htmlloads class AdditionAppletof Fig. 3.12 into the appletviewer. Lines 1 2 // Fig. 3.12: AdditionApplet.java // Adding two floating-point numbers. are single-line comments stating the figure number, file name and purpose of the program. Line 5 import java.awt.Graphics; // import class Graphics imports class Graphics (package java.awt) for use in this applet. Actually, the import statement at line 5 is not required if we always use the complete name of class Graphics java.awt.Graphics which includes the full package name and class name. For example, the first line of method paintcan be defined as public void paint( java.awt.Graphics g ) Software Engineering Observation 3.3 The Java compiler does not require import statements in a Java source code file if the complete class name the full package name and class name (e.g., java.awt.Graphics) is specified every time a class name is used in the source code. Line 8 import javax.swing.*; // import package javax.swing specifies to the compiler that several classes from package javax.swingare used in this applet. The asterisk (*) indicates that all classes in the javax.swing package (such as JApplet and JOptionPane) should be available to the compiler so the compiler can ensure that we use the classes correctly. This allows programmers to use the shorthand name (the class name by itself) of any class from the javax.swingpackage in the program. Remember that our last two programs imported only class JAppletfrom package javax.swing. In this program, we use classes JAppletand JOptionPanefrom that package. Importing an entire package into a program is also a shorthand notation so the programmer is not required to provide a separate importstatement for every class used from that package. Remember that you can always use the complete name of every class, i.e., javax.swing.JApplet and javax.swing.JOptionPane rather than import statements. Software Engineering Observation 3.4 The compiler does not load every class in a package when it encounters an import statement that uses the * (e.g., javax.swing.*) notation. The compiler loads from the package only those classes the program uses. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

124 Introduction to Java (Web hosting company) Applets Chapter 3 24

Sunday, August 26th, 2007

124 Introduction to Java Applets Chapter 3 24 25 // obtain second number from user 26 secondNumber = JOptionPane.showInputDialog( 27 “Enter second floating-point value” ); 28 29 // convert numbers from type String to type double 30 number1 = Double.parseDouble( firstNumber ); 31 number2 = Double.parseDouble( secondNumber ); 32 33 // add numbers 34 sum = number1 + number2; 35 } 36 37 // draw results in a rectangle on applet s background 38 public void paint( Graphics g ) 39 { 40 // call inherited version of method paint 41 super.paint( g ); 42 43 // draw rectangle starting from (15, 10) that is 270 44 // pixels wide and 20 pixels tall 45 g.drawRect( 15, 10, 270, 20 ); 46 47 // draw results as a String at (25, 25) 48 g.drawString( “The sum is ” + sum, 25, 25 ); 49 50 } // end method paint 51 52 } // end class AdditionApplet Fig. 3.12An addition program in action (part 2 of 2). Fig. 3.12 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Web hosting top - Chapter 3 Introduction to Java Applets 123 Lines

Saturday, August 25th, 2007

Chapter 3 Introduction to Java Applets 123 Lines 19 and 22 of method paint g.drawLine( 15, 10, 210, 10 ); g.drawLine( 15, 30, 210, 30 ); use method drawLine of class Graphicsto indicate that the Graphicsobject that g refers to should draw lines. Method drawLinerequires four arguments that represent 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. Method drawLinedraws a straight line between the two end points. 3.5 Another Java Applet: Adding Floating-Point Numbers Our next applet (Fig. 3.12) mimics the application of Fig. 2.9 for adding two integers. However, this applet requests that the user enter two floating-point numbers (i.e., numbers with a decimal point such as 7.33, 0.0975 and 1000.12345). To store floating-point numbers in memory we introduce primitive data type double, which represents double-precision floating-point numbers. There is also primitive data type float for storing 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 doublevs. seven significant digits for float). Once again, we use JOptionPane.showInputDialogto request input from the user. The applet computes the sum of the input values and displays the result by drawing a string inside a rectangle on the applet. The HTML file to load this applet into the applet- vieweris shown in Fig. 3.13. 1 // Fig. 3.12: AdditionApplet.java 2 // Adding two floating-point numbers. 3 4 // Java core packages 5 import java.awt.Graphics; // import class Graphics 6 7 // Java extension packages 8 import javax.swing.*; // import package javax.swing 9 10 public class AdditionApplet extends JApplet { 11 double sum; // sum of values entered by user 12 13 // initialize applet by obtaining values from user 14 public void init() 15 { 16 String firstNumber; // first string entered by user 17 String secondNumber; // second string entered by user 18 double number1; // first number to add 19 double number2; // second number to add 20 21 // obtain first number from user 22 firstNumber = JOptionPane.showInputDialog( 23 “Enter first floating-point value” ); Fig. 3.12An addition program in action (part 1 of 2). Fig. 3.12 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

122 Introduction to Java Applets Chapter 3 to (Web hosting support)

Friday, August 24th, 2007

122 Introduction to Java Applets Chapter 3 to output a string containing a newline character (n), you will simply see a small black box at that position in the string. To make drawing more interesting, the applet of Fig. 3.10 draws two lines and a string. The HTML file to load the applet into an applet container is shown in Fig. 3.11. 1 // Fig. 3.10: WelcomeLines.java 2 // Displaying text and lines 3 4 // Java core packages 5 import java.awt.Graphics; // import class Graphics 6 7 // Java extension packages 8 import javax.swing.JApplet; // import class JApplet 9 10 public class WelcomeLines extends JApplet { 11 12 // draw lines and a string on applet s background 13 public void paint( Graphics g ) 14 { 15 // call inherited version of method paint 16 super.paint( g ); 17 18 // draw horizontal line from (15, 10) to (210, 10) 19 g.drawLine( 15, 10, 210, 10 ); 20 21 // draw horizontal line from (15, 30) to (210, 30) 22 g.drawLine( 15, 30, 210, 30 ); 23 24 // draw String between lines at location (25, 25) 25 g.drawString( “Welcome to Java Programming!”, 25, 25 ); 26 27 } // end method paint 28 29 } // end class WelcomeLines Coordinate (15, 10) Coordinate (15, 30) Coordinate (210, 10) Coordinate (210, 30) Fig. 3.10 Drawing strings and lines. 1 2 3 4 Fig. 3.11 The WelcomeLines.htmlfile, which loads class WelcomeLinesof Fig. 3.10 into the appletviewer. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01