# drawLine loop

• March 5th, 2013, 01:14 PM
shannonlee
drawLine loop
c. (10 points) Instead of using *s, use line segments for the plot. You must use a loop, not just draw the lines using separate drawLine() calls. No asterisks will be displayed. To draw a line, you need two points. You will have to remember the the first point's place in order to draw the line between the first and second point. Then you have to draw a line from the second to the third point.

This is sort of a fence post problem. Set up the first point with coordinates oldX and oldY before the loop. In the loop generate newX and newY for the second point. Draw the line. Give oldX and oldY the values of newX and newY. Continue looping.

Code java:

```import java.awt.Graphics;     public class BounceLine {   public static void main(String[] args) { DrawingPanel ball = new DrawingPanel (300,500); Graphics g = ball.getGraphics();   int x1= 10; int y1=25; for (int i=2; i<=20; i++){ int x2=(i*10); int y2=425*(i*25+50); g.drawLine(x1,y1,x2,y2); x1=x2; y1=y2; } } }```
This is what I have
I can't get it to work. Help!!
• March 5th, 2013, 01:30 PM
Norm
Re: drawLine loop
Quote:

I can't get it to work.
Please explain what the problem is.

A debugging technique to show where each line is being drawn: add a println statement right after the call to the drawLine() method that prints out the values of x1,y1 and x2,y2. The print out will help you see what the code sees when it executes and will be something you can copy and paste here to show the problem.

[code=java]
[/code]
to get highlighting and preserve formatting.
• March 5th, 2013, 01:39 PM
shannonlee
Re: drawLine loop
When I run the program, only the first line is printed. I have to create lines that look like this
Attachment 1849
But I can't get the lines to print where the last coordinate is and create a new line.

This is the output I'm getting:
20
20
42500
42500
30
30
53125
53125
40
40
63750
63750
50
50
74375
74375
60
60
85000
85000
70
70
95625
95625
80
80
106250
106250
90
90
116875
116875
100
100
127500
127500
110
110
138125
138125
120
120
148750
148750
130
130
159375
159375
140
140
170000
170000
150
150
180625
180625
160
160
191250
191250
170
170
201875
201875
180
180
212500
212500
190
190
223125
223125
200
200
233750
233750
• March 5th, 2013, 03:40 PM
Norm
Re: drawLine loop
What prints out from the println I suggested be added to the code? That will show where the lines are being drawn.

Also posted at: DrawLine loop - Dev Shed

--- Update ---

The printout would be easier if the numbers for each line were all on one line and if all the numbers had Strings for labels:
Code :

`System.out.println("first="+first+", sec="+sec + ....`

It looks like some of the numbers are out of range: 42500
These points won't be in the viewing area. Have you tried to plot the lines on a graph paper from the x,y values that were printed?