Ralph Morelli, Ralph Walde, Beryl Hoffman, David G. Cooper
Section3.11Drawing Lines and Defining Graphical Methods (Optional)
We used a Graphics object in the previous chapter to draw rectangles and ovals in a Canvas which are then displayed in a JFrame window. The Graphics class also possesses a method for drawing a line segment. Problems involving drawing pictures on a Canvas using a series of line segments can be a source of examples of defining useful methods and also of making good use of loops.
The method call g.drawLine(x1, y1, x2, y2) draws a line from the point to where refers to a point that is pixels from the left edge of the area that g is drawing in and pixels from the top edge. Thus g.drawLine(10, 10, 10, 60) draws a vertical line segment that is 50 pixels long and is 10 pixels from the left edge of the drawing area, that is, a line segment from the point to the point .
Consider the problem of creating a Swing program with a method called drawSticks() to draw any specified number of vertical line segments. This method might be useful for an graphical user interface to the OneRowNim game to draw the number of sticks at a given point in a game. Suppose that this method must have an int parameter to specify the number of vertical lines to draw and two int parameters to specify the location of the top endpoint of the left most line segment. The drawSticks() method will need to use a Graphics object connected to the JFrame window for drawing the line segment. The only such Graphics object available is the parameter in the paint() method of the Canvas. Thus the method must have a Graphics parameter and it will be called in the paint() method using the Graphics object there as an argument. Thus the header of the method should look like:
The body of the method could use a while loop with a counter variable k to draw num lines. The counter k is initialized to 0 before the loop and incremented at the end of the loop. The loop runs as long as k < num.
publicvoiddrawSticks(Graphics g,int x,int y,int num){int k =0;while(k < num){ g.drawLine(x, y, x, y +50);
x = x +10;
k = k +1;}// while}// drawSticks()
Activity3.11.1.
Run the program below listed in Listing 3.11.1. Can you change the number of lines it draws?
Listing3.11.1.A Canvas Class with a method for drawing a set of sticks.
/** DrawSticksCanvas demonstrates some graphics commands.
*It draws a set of 12 vertical lines and a set of 7 lines.
*/importjava.awt.*;importjavax.swing.*;publicclassDrawSticksCanvasextendsCanvas/** drawSticks(g,x,y,num) will draw num vertical line
* segments. The line segments are 10 pixels apart and
* 50 pixels long. The top endpoint of the left most
*line segment is at the point (x,y).
*/publicvoiddrawSticks(Graphics g,int x,int y,int num){int k =0;while(k < num){ g.drawLine(x, y, x, y +50);
x = x +10;
k = k +1;}// while}// drawSticks()publicvoidpaint(Graphics g){
g.setColor(Color.red);drawSticks(g,25,25,12);
g.setColor(Color.cyan);drawSticks(g,25,125,7);}// paint()// every java program needs a main to run!publicstaticvoidmain(String[] args){DrawSticksCanvas c =newDrawSticksCanvas();JFrame f =newJFrame("Draw Sticks Program");
f.add(c);
f.setSize(200,300);
f.setVisible(true);}}// DrawSticksCanvas
Note that the body of drawSticks() uses a while-loop to draw the lines and declares and initializes a local variable to zero to use for counting the number of lines drawn. The statement g.drawLine(x, y, x, y + 50); draws a vertical line which is pixels long. Increasing the value of by each time through the loop moves the next line pixels to the right.
The first call to drawSticks() in the paint() method draws lines with the top point of the left-most line. The second call to drawSticks() will draw cyan sticks pixels lower. Note that changing the color of g before passing it as an argument to drawSticks() changes the drawing color.
As we have seen in this example, defining methods with parameters to draw an object makes the code reusable and makes it possible to draw a complex scene by calling a collection of simpler methods. It is a typical use of the divide-and-conquer principle. The while-loop can be useful in drawing almost any geometrically symmetric object.