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

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

Leave a Reply