Skip to main content
Lesson 10 - for, do-while, Nested Loops
Lesson MenuPrevious
  
L.A.10.3 - ParallelLines page 10 of 10

Background:

In this lab, you will practice using nested for loops to recreate this image using the DrawingTool class:

To do this efficiently, there are a few suggestions to consider. First, there are eight rows each containing seven filled boxes. This suggests a nested for loop, like this:

for (int row = 0; row < 8; row++)
{
  // calculate the start of the row of squares

  // calculate and add a horizontal offset
  
  for (int col = 0; col < 7; col++)
  {
    // draw the square
    // calculate and position for the next square
  }

  // calculate the location and draw the line
}

Calculating the square position will take some work. You might want to make a quick sketch, with coordinates, to save frustration. Hint: there will be a negative x offset and a positive y offset to have the image start in the upper left corner as shown.

The DrawingTool class includes a method for drawing filled rectangles:

fillRect(double width, double height)

For instance, with a DrawingTool called pen, the call pen.fillRect(40, 40) would create a 40x40 filled square centered about the current drawing position.


Assignment:

  1. Using the information given in the Background section above, write a program using nested for loops to display the image shown above.

  2. Write and debug your work as ParallelLines.java.

  3. Turn in your source code and run output.


Lesson MenuPrevious
Contact
 ©ICT 2003, All Rights Reserved.