Results 1 to 14 of 14
 08142010, 02:17 AM #1Member
 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 (selfstudy). 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 topleft corner, fanning out the lines until they cover the upperleft 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 topleft corner (0,0). The second endpoint can be found by starting at the bottomleft 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,
 08142010, 02:42 AM #2Senior 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*«*JavaLast edited by Webuser; 08142010 at 02:45 AM.
If my answer helped you. Please click my "REP" button and add a comment
Have a Good Java Coding :)
 08142010, 02:43 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; 08142010 at 02:47 AM.
 08142010, 03:38 AM #4Member
 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?

You can do this with a while loop, just figure out the logic on paper first.
 08142010, 07:58 AM #6Member
 Join Date
 Aug 2010
 Posts
 3
 Rep Power
 0
Thanks for the reply, :)
That's what I've been trying to do. But the problem is I'm having a hardtime 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.
Could you give me a head start please?
 08142010, 10:01 AM #7
Increment the counter inside the loop, silly ;)
db
 08142010, 01:04 PM #8
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 }
 07062011, 08:21 AM #9Member
 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?
 07062011, 09:21 AM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,373
 Blog Entries
 7
 Rep Power
 25
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,
JosLast edited by JosAH; 07062011 at 11:20 AM. Reason: typo ...
The only person who got everything done by Friday was Robinson Crusoe.
 07062011, 10:59 AM #11
T.Deepa has started another thread. Locking up here.
db
 07062011, 11:26 AM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,373
 Blog Entries
 7
 Rep Power
 25
 07062011, 11:33 AM #13
Yes, but Trebor posted that nearly a year ago.
db
 07062011, 12:00 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,373
 Blog Entries
 7
 Rep Power
 25
Similar Threads

drawing three (or more) lines with input of coordinat
By koddy in forum New To JavaReplies: 0Last Post: 04202010, 08:28 AM 
Help with While and For Looping
By gmoney8316 in forum New To JavaReplies: 2Last Post: 03032010, 11:54 PM 
Looping if statements
By rice in forum New To JavaReplies: 10Last Post: 10022009, 02:50 AM 
Drawing lines and polygons with different cap and join styles
By Java Tip in forum SWT TipsReplies: 0Last Post: 07022008, 07:50 PM 
Demonstration of drawing lines in SWT
By Java Tip in forum SWT TipsReplies: 0Last Post: 06282008, 09:27 PM
Bookmarks