Chapter 3 Introduction to Java Applets 129 Lines (Starting a web site)
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