124 Introduction to Java (Web hosting company) Applets Chapter 3 24
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