Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: drawLine loop

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default drawLine loop

    My professor is asking this:
    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.

    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!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: drawLine loop

    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.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawLine loop

    When I run the program, only the first line is printed. I have to create lines that look like this
    Continuous bounce plot.jpg
    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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default 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:
    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?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. drawLine problem, lines not connected
    By crzybldthrwr in forum What's Wrong With My Code?
    Replies: 85
    Last Post: September 3rd, 2012, 10:47 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  4. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM
  5. g.drawLine doesn't draw line in for loop
    By shumpi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2010, 06:15 PM