Chapter 3 Introduction to Java Applets 117 a (Medical web site)
Chapter 3 Introduction to Java Applets 117 a message on the screen. When the applet container tells the applet to draw itself on the screen by calling method paint, our message Welcome to Java Programming! appears rather than a blank screen. Lines 13 21 are the definition of paint. The task of method paint is to draw graphics (such as lines, ovals and strings of characters) on the screen. Keyword voidindicates that this method does not return any results when it completes its task. The set of parentheses after paint defines the method s parameter list. The parameter list is where methods receive data required to perform their tasks. Normally, this data is passed by the programmer to the method through a method call (also known as invoking a method or sending a message). For example, in Chapter 2 we passed data to JOptionPane s showMessageDialog method such as the message to display or the type of dialog box. However, when writing applets, the programmer does not call method paint explicitly. Rather, the applet container calls paint to tell the applet to draw and the applet container passes to the paint method the information paint requires to perform its task, namely a Graphics object (called g). It is the applet container s responsibility to create the Graphics object to which g refers. Method paint uses the Graphics object to draw graphics on the applet. The publickeyword at the beginning of line 13 is required so the applet container can call your paint method. For now, all method definitions should begin with the public keyword. We introduce other alternatives in Chapter 8. The left brace, {, on line 14 begins method paint s body. The corresponding right brace, }, on line 21 ends paint s body. Line 16 super.paint( g ); calls the version of method paint inherited from superclass JApplet.1 Line 19 g.drawString( “Welcome to Java Programming!”, 25, 25 ); instructs the computer to perform an action (or task), namely to draw the characters of the string Welcome to Java Programming! on the applet. This statement uses method drawString defined by class Graphics(this class defines all the drawing capabilities of a Java program, including strings of characters and shapes such as rectangles, ovals and lines). The statement calls method drawString using the Graphics object g (in paint s parameter list) followed by a dot operator (.) followed by the method name drawString. The method name is followed by a set of parentheses containing the argument list drawString needs to perform its task. The first argument to drawString is the String to draw on the applet. The last two arguments in the list 25 and 25 are the x-y coordinates (or position) at which the bottom-left corner of the string should be drawn on the applet. Drawing methods from class Graphics require coordinates to specify where to draw on the applet (later in the text we 1. For reasons that will become clear later in the text, this statement should be the first statement in every applet s paint method. Although the early examples of applets will work without this statement, omitting this statement causes subtle errors in more elaborate applets that combine drawing and GUI components. Including this statement now will get you in the habit of using it and will save time and effort as you build more substantial applets later. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01