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

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

Leave a Reply