The program below is the answer to Deitel’s Java How to Program (9th Edition) Chapter 4 Exercise 4.1.
Question: Using loops and control statements to draw lines can lead to many interesting designs.
- Create the design in the left screen capture of Fig. 4.20. This design draws lines from the top-left corner, fanning them out 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 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.
- Modify part (a) to have lines fan out from all four corners, as shown in the right screen capture of Fig. 4.20. Lines from opposite corners should intersect along the middle.
To compile all four classes at the same time using the command prompt, use the command
javac Lines.java LinesTest.java LinesB.java LinesBTest.java
To run LinesTest and LinesBTest, use the command
java LinesTest LinesBTest
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 4.1a - Lines fanning From A Corner Class<br /> * This Program Draws Lines fanning From A Corner of A Jframe<br /> *<br /> */ <br /><br />import java.awt.Graphics;<br />import javax.swing.JPanel;<br /><br />public class Lines {<br /> public void paintComponent(Graphics g) {<br /><br /> int width = getWidth(); // total width<br /> int height = getHeight(); // total height<br /> int x, y;<br /> int loopCount = 1;<br /> int xCount = 1;<br /> int yCount = 1;<br /><br /> super.paintComponent(g); // call paintComponent to ensure the panel displays correctly<br /> <br /> x = width / 15;<br /> y = height / 15;<br /><br /> //fan out from top left corner<br /><br /> g.drawLine(0, 0, 0, height);<br /><br /> while (loopCount < 15) {<br /> g.drawLine(0, 0, x * xCount, height - y * yCount);<br /> ++loopCount;<br /> ++xCount;<br /> ++yCount;<br /> }<br /><br /> } // end method paintComponent<br />}//end class<br /> |
Below is Class LinesTest.java to test Class Lines.java
1 2 |
//Exercise 4.1a - Lines fanning From A Corner Class Test<br />//This Program Tests Class Lines<br /><br />import javax.swing.JFrame;<br /><br />public class LinesTest {<br /> public static void main (String [] args) {<br /> Lines panel = new Lines(); // create a panel that contains our drawing<br /> JFrame application = new JFrame(); // create a new frame to hold the panel<br /> application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // exit frame when closed<br /> application.add( panel ); // add the panel to the frame<br /> application.setSize( 300, 300 ); // set the size of the frame<br /> application.setVisible( true ); // make the frame visible<br /> }<br />}<br /> |
4. 1(a) Fullscreen |
4.1(a) Resized |
4.1(a) |
Below is the code for the (b) part of the question – Lines fanning out from all corners of the frame
1 2 |
//Exercise 4.1b - Lines fanning From All Corners<br />//This Program Draws Lines fanning From All Corners of a JFrame<br /><br />import java.awt.Graphics;<br />import javax.swing.JPanel;<br /><br />public class LinesB extends JPanel {<br /> public void paintComponent(Graphics g) {<br /><br /> int width = getWidth();<br /> int height = getHeight();<br /> int x, y;<br /> int loopCount = 1;<br /> int xCount = 1;<br /> int yCount = 1;<br /> <br /> super.paintComponent(g);<br /> <br /> x = width / 15;<br /> y = height / 15;<br /><br /> //fan out from top left corner<br /><br /> g.drawLine(0, 0, 0, height);<br /><br /> while (loopCount < 15) {<br /> g.drawLine(0, 0, x * xCount, height - y * yCount);<br /> ++loopCount;<br /> ++xCount;<br /> ++yCount;<br /> }<br /><br /> //fan out from top right corner<br /><br /> loopCount = xCount = yCount = 1;<br /><br /> g.drawLine(width, 0, width, height);<br /><br /> while (loopCount < 15) {<br /> g.drawLine(width, 0, width - x * xCount, height - y * yCount);<br /> ++loopCount;<br /> ++xCount;<br /> ++yCount;<br /> }<br /><br /> //fan out from bottom left corner<br /><br /> loopCount = xCount = yCount = 1;<br /><br /> g.drawLine(0, height, width, height);<br /><br /> while (loopCount < 15) {<br /> g.drawLine(0, height, width - x * xCount, height - y * yCount);<br /> ++loopCount;<br /> ++xCount;<br /> ++yCount;<br /> }<br /><br /> //fan out from bottom right corner<br /><br /> loopCount = xCount = yCount = 1;<br /><br /> g.drawLine(width, height, 0, height);<br /> <br /> while (loopCount < 15) {<br /> g.drawLine(width, height, x * xCount, height - y * yCount);<br /> ++loopCount;<br /> ++xCount;<br /> ++yCount;<br /> }<br /><br /> } // end method paintComponent<br />} //end class<br /> |
Below is Class LinesBTest.java to test Class LinesB.java
1 2 |
//Exercise 4.1b - Lines fanning From All Corners Class Test<br />//This Program Tests Class LinesB<br /><br />import javax.swing.JFrame;<br /><br />public class LinesBTest {<br /> public static void main (String [] args) {<br /> LinesB panel = new LinesB ();<br /> JFrame application = new JFrame();<br /> application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );<br /> application.add( panel );<br /> application.setSize( 300, 300 );<br /> application.setVisible( true );<br /> }<br />}<br /> |
4. 1(b) Fullscreen |
4.1(b) Resized |
4.1(b) |
3 Comments
:)
Thank you very much for the corrections. I have already updated the code – works correctly now.
The class with main in it(first program) can't be the one to "extend Jpanel". This has to be the external .java class. Also, second program creates an object LineB instead of LinesB. Other than those minor errors you have the solution. Also, than you, I wanted to see the code to study it without taking the time to write it myself. Thank you.