CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2013
    Posts
    1

    Using a for loop to shorten the coding.

    //New to learning Java, I am better at reading and understanding than using and creating code... hardly a useful skill at all but I am at least trying
    import java.awt.*;
    import java.applet.*;
    public class GraphicsLab02 extends Applet
    {
    public void paint(Graphics g)
    {
    int width = 980;
    int height = 630;
    int y1 = 640;
    int x2 = 990;
    int y2 = 640;
    g.drawRect(10,10,width,height);

    // Bottom right
    //I am attempting to cause an "optical illusion" by using straight lines and moving them by 10 pixels on both the x and y axis. I would like to have a for loop, I believe, to shorten the coding.
    //y1 and x2 change by +- 10. +=10 and -=10?
    g.drawLine(990,630,10,640);
    g.drawLine(990,620,20,640);
    g.drawLine(990,610,30,640);
    g.drawLine(990,600,40,640);
    g.drawLine(990,590,50,640);
    g.drawLine(990,580,60,640);
    g.drawLine(990,570,70,640);
    g.drawLine(990,560,80,640);
    //Now y1 still -=10, x2 still +=10 now y2 -=10?
    g.drawLine(990,550,90,560);
    g.drawLine(990,540,100,550);
    g.drawLine(990,530,110,540);
    g.drawLine(990,520,120,530);
    g.drawLine(990,510,130,520);
    g.drawLine(990,500,140,510);
    // It all stays constant, it redraws the line many times to create the curve.
    g.drawLine(990,630,10,640);
    g.drawLine(990,630,10,640);
    g.drawLine(990,630,10,640);
    g.drawLine(990,630,10,640);
    g.drawLine(990,630,10,640);
    g.drawLine(990,630,10,640);
    g.drawLine(990,630,10,640);
    g.drawLine(990,630,10,640);
    g.drawLine(990,630,10,640);
    g.drawLine(990,630,10,640);
    }
    }

    I am attempting to end up with this product, but am currently focusing on the bottom right-hand corner. Thank you very much for your help ^^
    Name:  applet.png
Views: 1485
Size:  87.1 KB

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Using a for loop to shorten the coding.

    It all stays constant, it redraws the line many times
    That could be done in a for loop that loops the number of times that you want the draw method called.

    For the other draw method calls where one of the x or y value changes from line to line could be down with a for loop that starts at one value and goes to the other value incrementing (or decrementing) by the steps needed:
    Code:
      for(int idx=startValue; idx <= endValue; idx += incrAmt)
    
    // to move two indexes:
      for(int idx=startValue, idy=sV2; idx <= endValue; idx += incrAmt, idy--)
    Last edited by Norm; September 23rd, 2013 at 02:40 PM.
    Norm

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured