How can I use a loop to create parallel lines?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Logan82
    New Member
    • Mar 2010
    • 3

    How can I use a loop to create parallel lines?

    I am trying to draw a grid in my panel, but i can't quite figure out the loop to draw the parallel lines.

    Thanks
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Could you show us the code you're using to draw a single line?
    What output are you getting right now?

    Comment

    • Logan82
      New Member
      • Mar 2010
      • 3

      #3
      here is the code for my grid without the loops

      private void drawgrid(Graphi cs paper) {
      paper.setColor( Color.black); // set the color of walls black


      paper.drawLine( 50, 50, 250, 50);//draws h line
      paper.drawLine( 50, 70, 250, 70);//draws h line
      paper.drawLine( 50, 90, 250, 90);//draws h line
      paper.drawLine( 50, 110, 250, 110);//draws h line
      paper.drawLine( 50, 130, 250, 130);//draws h line
      paper.drawLine( 50, 150, 250, 150);//draws h line
      paper.drawLine( 50, 170, 250, 170);//draws h line
      paper.drawLine( 50, 190, 250, 190);//draws h line
      paper.drawLine( 50, 210, 250, 210);//draws h line
      paper.drawLine( 50, 230, 250, 230);//draws h line
      paper.drawLine( 50, 250, 250, 250);//draws h line

      paper.drawLine( 50, 50, 50, 250);//draws v line
      paper.drawLine( 70, 50, 70, 250);//draws v line
      paper.drawLine( 90, 50, 90, 250);//draws v line
      paper.drawLine( 110, 50, 110, 250);//draws v line
      paper.drawLine( 130, 50, 130, 250);//draws v line
      paper.drawLine( 150, 50, 150, 250);//draws v line
      paper.drawLine( 170, 50, 170, 250);//draws v line
      paper.drawLine( 190, 50, 190, 250);//draws v line
      paper.drawLine( 210, 50, 210, 250);//draws v line
      paper.drawLine( 230, 50, 230, 250);//draws v line
      paper.drawLine( 250, 50, 250, 250);//draws v line

      }
      }

      Comment

      • Logan82
        New Member
        • Mar 2010
        • 3

        #4
        sorry the previous message is my code for my parallel lines drawn ine at a time. This returns a grid, but i would like to improve my code with two loops.

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Please use [ code ] [ / code ] tags next time

          paper.drawLine( 50, 50, 250, 50);//draws h line
          paper.drawLine( 50, 70, 250, 70);//draws h line
          These numbers change, starting from 50, adding 20 at a time, until 250

          Replace the bolded parts with an integer variable instead:
          Code:
          for (int i = 50; i < = 250; i += 20){
            paper.drawLine(50,i, 250,i);//draws h line
          }
          Similarly do the same thing for the vertical lines.
          paper.drawLine(50, 50, 50, 250);//draws v line
          paper.drawLine(70, 50, 70, 250);//draws v line
          Code:
          for (int j = 50; j < = 250; j += 20){
            paper.drawLine(50,j, 250,j);//draws v line
          }
          If you can keep track, you can even combine the 2!
          Code:
          for (int i = 50; i < = 250; i += 20){
            paper.drawLine(50,i, 250,i);//draws h line
            paper.drawLine(i,50,i, 250);//draws v line
          }

          Comment

          • anurag275125
            New Member
            • Aug 2009
            • 79

            #6
            Code:
            for(int i=0;i<11;i++)
            {
                    paper.drawLine(50, 50+i*20, 250, 50+i*20);//draws h line
            	paper.drawLine(50+i*20, 50, 50+i*20, 250);//draws v line
            }

            Comment

            Working...