# Thread: Drawing Lines using looping statements

1. Member
Join Date
Aug 2010
Posts
3
Rep Power
0

## Drawing Lines using looping statements

Hi,

First of all, I hope that I've posted on the correct section/thread. Moving on, I would like to seek help from anyone here. As you can see, I'm new to Java programming and I'm only using books to support my study (self-study). And, I can say that I'm slowly grasping the logic and concept of Java. But, I've encountered this problem, and here it is.

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.

The result should look like this.

I've been trying to do this for the past 2 weeks but up until now I haven't solved this problem.

Hope anyone can here can share tips on how to do this. I'm not actually asking for the exact code (since I would still want to do this on my own). But, I'm asking for at least a hint/clue on how to do this. Maybe, just a code snippet will help and from there I would do it on my own.

Thanks,

2. Senior Member
Join Date
Dec 2008
Posts
526
Rep Power
0
Welcome :)

to get the effect you have to control y coordinates that's all :)

Java Code:
```for(int i=10, j=10; i<=100; i+=10, j--)
{
g.drawLine(0,0,j,i);
}```
Draw line : Shape*«*2D Graphics GUI*«*Java
Last edited by Webuser; 08-14-2010 at 02:45 AM.

3. Looks like a problem in trig/geometry.
Compute the line from the lower left corner to the upper right corner. Divide it into the segments of the size you want and use the pythagorian(??) theorem to compute the x,y locations of the ends of the segments on the line. Then draw the lines from the upper left corner to each of these x,y points. It the panel is a square its probably easier to use the approach in the quote.

NOTE:
The code posted above will draw a series of lines one on the top of the other each line getting a little longer from the upper left towards the lower right.
Last edited by Norm; 08-14-2010 at 02:47 AM.

4. Member
Join Date
Aug 2010
Posts
3
Rep Power
0
@webuser & norm

Thanks! for the prompt reply, really appreciate it. :)

@webuser

how about using the "while" statement? because I'm not yet on the "for" statement, I'm still on the "if...else" and "while" statement. I'm using the Deitel and Deitel book and following each chapter diligently.

@norm

Could you site an example of what you've said?

5. Member
Join Date
Aug 2010
Posts
3
Rep Power
0
Originally Posted by Fubarable
You can do this with a while loop, just figure out the logic on paper first.

That's what I've been trying to do. But the problem is I'm having a hard-time figuring it out.

I've been thinking if I need some sort of counter here (15) and how am i going to apply it to the loop to make it draw lines several times.

6. Increment the counter inside the loop, silly ;)

db

7. What have you seen when you draw the designs on paper?
How do the x and y values change for each line?
Can you write a loop and change the x and y values each time around the loop?
for example:
Java Code:
```while(true) {
x = x + 20;  // change x each time around the loop
y = y + 23;  // ditto for y
}```

8. Member
Join Date
Jul 2011
Location
Chennai,India.
Posts
3
Rep Power
0

## Moving lines

I also follow the same book. I know the answer for the two excercises.
But i want to move the lines.
Just move from top left corner to half of the width.
Can help?

9. If the size of your panel is WxH and you want to find the endpoints on the diagonal (0,H) -- (W, 0) in n steps the endpoints are EP(i)= (0,H)+i*(W/n, -H/n) for i in [0, n); so you have to draw n lines from (0,0) to EP(i).

kind regards,

Jos
Last edited by JosAH; 07-06-2011 at 11:20 AM. Reason: typo ...

10. T.Deepa has started another thread. Locking up here.

db

11. Originally Posted by DarrylBurke
T.Deepa has started another thread. Locking up here.
Aren't T.Deepa and trebor two different posters?

kind regards,

Jos

12. Yes, but Trebor posted that nearly a year ago.

db

13. Originally Posted by DarrylBurke
Yes, but Trebor posted that nearly a year ago.
<Standing in the corner with a stupid look on my face/>

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•