Initializing an Instance Variable with Method init

Our next applet (Fig. 20.10) computes the sum of two values input by the user and displays the result by drawing a String inside a rectangle on the applet. The sum is stored in an instance variable of class AdditionApplet, so it can be used in both method init and method paint. The HTML document to load this applet into the appletviewer is shown in Fig. 20.11.

Figure 20.10. Adding double values.

(This item is displayed on pages 970 - 971 in the print version)

1 // Fig. 20.10: AdditionApplet.java 2 // Adding two floating-point numbers. 3 import java.awt.Graphics; // program uses class Graphics 4 import javax.swing.JApplet; // program uses class JApplet 5 import javax.swing.JOptionPane; // program uses class JOptionPane 6 7 public class AdditionApplet extends JApplet 8 { 9 private double sum; // sum of values entered by user 10 11 // initialize applet by obtaining values from user 12 public void init () 13 { 14 String firstNumber; // first string entered by user 15 String secondNumber; // second string entered by user 16 17 double number1; // first number to add 18 double number2; // second number to add 19 20 // obtain first number from user 21 firstNumber = JOptionPane.showInputDialog( 22 "Enter first floating-point value" ); 23 24 // obtain second number from user 25 secondNumber = JOptionPane.showInputDialog( 26 "Enter second floating-point value" ); 27 28 // convert numbers from type String to type double 29 number1 = Double.parseDouble( firstNumber ); 30 number2 = Double.parseDouble( secondNumber ); 31 32 sum = number1 + number2; // add numbers 33 } // end method init 34 35 // draw results in a rectangle on applet's background 36 public void paint( Graphics g ) 37 { 38 super.paint( g ); // call superclass version of method paint 39 40 // draw rectangle starting from (15, 10) that is 270 41 // pixels wide and 20 pixels tall 42 g.drawRect( 15, 10, 270, 20 ); 43 44 // draw results as a String at (25, 25) 45 g.drawString( "The sum is " + sum, 25, 25 ); 46 } // end method paint 47 } // end class AdditionApplet  

Figure 20.11. AdditionApplet.html loads class AdditionApplet of Fig. 20.10 into an applet container.

(This item is displayed on page 971 in the print version)

1

2 "AdditionApplet.class" width = "300" height = "65"> 3 4

The applet requests that the user enter two floating-point numbers. Line 9 (Fig. 20.10) declares instance variable sum of type double. The applet contains two methodsinit (lines 1233) and paint (lines 3646). When an applet container loads this applet, the container creates an instance of class AdditionApplet and calls its init methodthis occurs only once during an applet's execution. Method init normally initializes the applet's fields (if they need to be initialized to values other than their defaults) and performs other tasks that should occur only once when the applet begins execution. The first line of init always appears as shown in line 12, which indicates that init is a public method that receives no arguments and returns no information when it completes.

Lines 1430 declare variables to store the values entered by the user, obtain the user input and convert the Strings entered by the user to double values by using Double method parseDouble.

The assignment statement at line 32 sums the values stored in variables number1 and number2, and assigns the result to instance variable sum. At this point, the applet's init method returns program control to the applet container, which then calls the applet's start method. We did not declare start in this applet, so the one inherited from class JApplet is called here. You will see typical uses of method start in Chapters 21 and 23.

Next, the applet container calls the applet's paint method. In this example, method paint draws a rectangle (line 42) in which the result of the addition will appear. Line 45 calls the Graphics object's drawString method to display the results. The statement concatenates the value of instance variable sum to the String "The sum is" and displays the concatenated String.

Software Engineering Observation 20.1

The only statements that should be placed in an applet's init method are those that should execute only once when the applet is initialized.

Категории