Using loops and control statements to draw lines can lead to many interesting desingns.
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 DrawOneSetOfLinesReplace 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 DrawFourSetsOfLinesBe 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.