E N D
Methods The apple in the previous lecture consisted of a leaf, a stem, and the fruit itself. We drew the apple as one combined thing. What we should do instead is describe how to draw each of the individual parts separately. The instructions for drawing each component of the apple are called a procedure, or more particularly to the terminology used in java -- a method. Each part has a color, a size, and a location (a point that specifies the upper left-hand corner of the bounds box). This information can be “hardwired” into the method in the form of predefined constants, or, more flexibly, it can be passed to the method as a set of parameters.
Graphics Here is the original code for drawing the apple applet. import javax.swing.*; import java.awt.*; publicclass AppleDrawing extends JApplet { public void paint(Graphics g) { //set the color of the outline to black, the color of the apple to red, and the stem to green Color appleColor = new Color(238,23,30); Color stemColor = new Color(92,67,35); Color leafColor = new Color(23, 117,37); g.setColor(appleColor); g.fillOval(75 ,30, 250, 350); g.setColor(stemColor); g.fillRect(195, 10, 10, 30); g.setColor(leafColor); g.fillOval(205, 30, 50, 20); g.setColor(Color.black); g.drawOval(75, 30, 250, 350); g.drawRect(195,10,10, 30); //stem g.drawOval(205, 30, 50, 20); //leaf } } The instructions for drawing the individual parts are mixed together.
Draw an Apple in your Applet First draw the fruit Next add the stem Finally, add the leaf
visibility modifier identifier parameter list return type Parameters A method consists of a header and a method body. The header consists of a visibility modifier, a return type, a method name or identifier, and a parameter list. privatevoid drawLeaf(Point pt, Color shade, int width, int length) {..} private access means that this method is accessible only within the class in which it resides. With no modifier the class is visible only within its package. The return type can be an instance of any class, any primitive type, or no return at all (void). The body of the method is written between an opening and closing pair of curly brackets. (The body of this method will be added later.) The parameter list consists of type identifier and formal parameter name for each item on the list.
method call Parameters A method may be called numerous times in an application, or, if public, used in many different applications. It may be required to associate different variable names wih a single parameter. A distinction is made between the formal parameters of the method and the actual parameters of the calling function. public void paint(Graphics g) { Color leafColor= new Color(23, 117, 37); Point p = new Point(205, 30); drawLeaf(p, leafColor, 50, 20, g); ….. } actual identifiers used in the application private void drawLeaf(Point pt, Color shade, int wid, int len, Graphics gr) {..} formal parameter names used inside of the method
Parameters private void drawLeaf(Point pt, Color shade, intwid, intlen, Graphics gr) { gr.setColor(shade); gr.fillOval(pt, wid, len); //next draw the outline gr.setColor(Color.black); gr.drawOval(pt, wid, len); } Inside the method, objects and primitive types are referred to by their “formal parameter” name.
Parameters In java the only parameter passing method is “pass by value”. For objects, the “value” that is passed is the address or “handle” to the location of the object. setColor(Color shade); Callled method Calling environment parameter name variable declaration Color shade; AC14 F2CA Color leafColor; AC14 F2CA null object creation leafColor = new Color(23,117,37); When the method is called, the address of the object is passed to the corresponding parameter This provides a “handle” to the original object Color object located at AC14 F2CA gr.setColor(leafColor)
Calling function Graphics gr Color shade Point pt 205 30 int wid int len Parameters privatevoid drawLeaf (Point pt, Color shade, int wid, int len, Graphics gr) {..} method drawLeaf( ) In method paint(Graphics g) Handle to g Color leafColor AC14 F2CA AC14 F2CA Point p AC14 F402 AC14 F402 50 20 p = new Point (205, 30); //starting at AC14 F402 leafColor = new Color(23, 117, 37); starting at AC14 F2CA drawLeaf(p, leafColor, 50, 20, g); //method call
Revised implementation of paint( ) import javax.swing.*; import java.awt.*; publicclass AppleDrawing extends JApplet { //private methods used by paint -- implementation left as an exercise privatevoid drawApple(Point pt, Color shade, int wid, int len, Graphics gr) { } privatevoid drawStem(Point pt, Color shade, int wid, int len, Graphics gr) { } privatevoid drawLeaf(Point pt, Color shade, int wid, int len, Graphics gr) { } public void paint(Graphics g) { //set the color of the apple to red, the leaf to green, and the stem to brown Color appleColor = new Color(238,23,30); Color stemColor = new Color(92,67,35); Color leafColor = new Color(23, 117,37); drawApple(new Point(75, 30), appleColor, 250, 350, g); drawStem(new Point(195, 10), stemColor, 10, 30, g); drawLeaf(new Point(205, 30), leafColor, 50, 20, g); } }