Drawing Lines using looping statements

• 08-14-2010, 02:17 AM
trebor11584
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.

Quote:

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.

http://javaclass.billkrieger.com/cla...2/Fig4.21a.png

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,
• 08-14-2010, 02:42 AM
Webuser
Welcome :)

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

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
• 08-14-2010, 02:43 AM
Norm
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.
• 08-14-2010, 03:38 AM
trebor11584
@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?
• 08-14-2010, 03:41 AM
Fubarable
You can do this with a while loop, just figure out the logic on paper first.
• 08-14-2010, 07:58 AM
trebor11584
Quote:

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.

• 08-14-2010, 10:01 AM
DarrylBurke
Increment the counter inside the loop, silly ;)

db
• 08-14-2010, 01:04 PM
Norm
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:
Code:

```while(true) {   x = x + 20;  // change x each time around the loop   y = y + 23;  // ditto for y }```
• 07-06-2011, 08:21 AM
T.Deepa
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?
• 07-06-2011, 09:21 AM
JosAH
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
• 07-06-2011, 10:59 AM
DarrylBurke
T.Deepa has started another thread. Locking up here.

db
• 07-06-2011, 11:26 AM
JosAH
Quote:

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
• 07-06-2011, 11:33 AM
DarrylBurke
Yes, but Trebor posted that nearly a year ago.

db
• 07-06-2011, 12:00 PM
JosAH
Quote:

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