Results 1 to 14 of 14
  1. #1
    trebor11584 is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default 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. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    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);
    }
    for more information I recommend reading this
    Draw line : Shape**2D Graphics GUI**Java
    Last edited by Webuser; 08-14-2010 at 02:45 AM.
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    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. #4
    trebor11584 is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    @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. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    trebor11584 is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You can do this with a while loop, just figure out the logic on paper first.
    Thanks for the reply, :)

    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.

    Could you give me a head start please?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Increment the counter inside the loop, silly ;)

    db

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    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
    }

  9. #9
    T.Deepa is offline Member
    Join Date
    Jul 2011
    Location
    Chennai,India.
    Posts
    3
    Rep Power
    0

    Default 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?

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

    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 ...
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

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

    db

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

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

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Yes, but Trebor posted that nearly a year ago.

    db

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

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

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 04-20-2010, 08:28 AM
  2. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  3. Looping if statements
    By rice in forum New To Java
    Replies: 10
    Last Post: 10-02-2009, 02:50 AM
  4. Replies: 0
    Last Post: 07-02-2008, 07:50 PM
  5. Demonstration of drawing lines in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 09:27 PM

Posting Permissions

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