Using loops and control statements to draw lines can lead to many interesting desingns.

  1. Create the design in the left screen capture of Fig 4.21. This design draws lines from the top-left corner, fanning out the lines until they cover the upper-left half of the panel. One approach is to divide the width and height into an equal number of steps (we found 15 steps worked well). The first endpoint of a line will always be in the top-left corner (0,0). The second endpoint can be found by starting at the bottom-left corner and moving up one vertical step and moving right one horizontal step. Draw a line between the two endpoints. Continue moving up and to the right one step to find each successive endpoint. The figure should scale accordingly as you resize the window.
  2. Modify your answer in part (a) to have lines fan out from all four corners, as shown in the right screen capture of Fig 4.21. Lines from opposite corners should intersect along the middle.
Figure 4.21a - Lines fanning from one corner Figure 4.21b - Lines fanning from four corners
Fig 4.21 - Lines fanning from a corner

For case study 4.1a, your code should look something like this:

// Case Study 4.1a: DrawOneSetOfLines.java
// Draws multiple lines from one corner of a panel.
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class DrawOneSetOfLines extends JPanel
{
public static void main(String args[])
{
// create a panel that contains our drawing
DrawOneSetOfLines panel = new DrawOneSetOfLines();

// create a new frame to hold the panel
JFrame application = new JFrame();

// set the frame to exit when it is closed
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

application.add(panel); // add the panel to the frame
application.setSize(250, 250); // set the size of the frame
application.setVisible(true); // make the frame visible
} // end main

public void paintComponent(Graphics g)
{
// call paintComponent to ensure the panel displays correctly
super.paintComponent(g);

int linesToDraw = 15;
int width = getWidth(); // total width
int height = getHeight(); // total height
int xDelta = width / linesToDraw;
int yDelta = height / linesToDraw;

// Put in a loop to draw draw lines from the
// upper left corner to the end points as
// follows:
// x = width, y = 0
// x = width - delta, y = delta
// x = width - 2 * delta, y = 2 * deltav
// ...
// x = (width - (n - 1) * delta), y = (n - 1) * delta
} // end method paintComponent
} // end class DrawOneSetOfLines

Replace the above comments with valid Java statements and you should be good to go. Of course, you need to get the first one going before you attempt part b. For part b, do something like the following.

// Case Study 4.1b: DrawFourSetsOfLines.java
// Draws multiple lines from four corners of a panel.
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class DrawFourSetsOfLines extends JPanel
{
public static void main(String args[])
{
// create a panel that contains our drawing
DrawFourSetsOfLines panel = new DrawFourSetsOfLines();

// create a new frame to hold the panel
JFrame application = new JFrame();

// set the frame to exit when it is closed
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

application.add(panel); // add the panel to the frame
application.setSize(250, 250); // set the size of the frame
application.setVisible(true); // make the frame visible
} // end main

public void paintComponent(Graphics g)
{
// call paintComponent to ensure the panel displays correctly
super.paintComponent(g);

int linesToDraw = 15;
int width = getWidth(); // total width
int height = getHeight(); // total height
int xDelta = width / linesToDraw;
int yDelta = height / linesToDraw;

// Put in a loop identical to the one you used for 4.1a

// Put in a second loop loop to draw draw lines from
// the lower left corner to the end points as
// follows:
// x = width, y = height
// x = width - delta, y = height - delta
// x = width - 2 * delta, y = height - 2 * delta
// ...
// x = (width - (n - 1) * delta), y = (height - (n - 1) * delta)

//***************************************************
// Important.
//
// Get the second loop working befor even trying
// to put in the third or fourth loop. Once the
// second loop is working, then try the third.
// Don't even try the fourth loop until the first
// are working correctly.
//***************************************************

// Put in a third loop loop to draw draw lines from
// the lower right corner to the end points as
// follows:
// x = 0, y = height
// x = 0 + delta, y = height - delta
// x = 0 + 2 * delta, y = height - 2 * delta
// ...
// x = (n - 1) * delta, y = (height - (n - 1) * delta)

// Put in a fourth loop loop to draw draw lines from
// the upper right corner to the end points as
// follows:
// x = 0, y = 0
// x = 0 + delta, y = 0 + delta
// x = 0 + 2 * delta, y = 0 + 2 * delta
// ...
// x = (n - 1) * delta, y = (n - 1) * delta
} // end method paintComponent
} // end class DrawFourSetsOfLines

Be sure to test each of the four corners one at a time. If you try to get all four corners working at once, it will be very confusing. The solution will be available here and here after the end of next week’s class.