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 2 of 2

Thread: Help with Square Shaped Spiral

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

    Default Help with Square Shaped Spiral

    I am currently working an Assignment that involves creating a square shaped spiral using loops. I thought I'd figure it out but I found out that the right side of the square spiral is not drawing a line.. Any ideas on how to fix this.. Thanks a lot you guys.

    P.S. I uploaded a picture of my output, if you guys could please look at it to know what I'm talking about or you could instead copy and run the code on your computers ... Thanks for your help..

     
    import java.awt.Graphics; 
    import javax.swing.JPanel;
    import javax.swing.JFrame;
     
     
    public class JAssign4B extends JPanel
    {
     
          public void paintComponent(Graphics g)
          {
     
             int width = getSize().width;
             int height = getSize().height;
     
             int widthCenter = width / 2;
             int heightCenter = height / 2;
     
             for(int i = 0; i < 4; i++)
             {
                g.drawLine(widthCenter + (20 * i), heightCenter + (20 * i), widthCenter + (20 * i), heightCenter + 20 + (20 * i));
                g.drawLine(widthCenter + (20 * i), heightCenter + 20 + (20 * i), widthCenter - 20 - (20 * i), heightCenter + 20 + (20 * i));
     
                //increase width and height here.....
     
                g.drawLine(widthCenter - 20 - (20 * i), heightCenter + 20 + (20 * i), widthCenter - 20 - (20 * i), heightCenter - 20 - (20 * i));
                g.drawLine(widthCenter - 20 - (20 * i), heightCenter - 20 - (20 * i), widthCenter + 20 + (20 * i), heightCenter - 20 - (20 * i));
     
     
             }
     
     
     
     
          }
     
     
     
          public static void main(String[] args)
          {
     
                      JAssign4B panel = new JAssign4B();
     
     
                      JFrame application = new JFrame();
     
                      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     
                      application.add( panel );      
                      application.setSize( 300, 300 ); 
                      application.setVisible( true );     
     
          }
     
     }
    Attached Images Attached Images


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with Square Shaped Spiral

    If you reduce your for loop to a single pass ( i < 1 ), you'll see that you're drawing 4 lines each pass when you should be drawing 5. (Kinda obvious without reducing the loops, but it helped me see what was going on.)

    Add the 5th line, and you'll get the desired result. The 5th line is the 3rd line of the for loop shifted 20 to the right of center rather than 20 to the left of center.

Similar Threads

  1. Replies: 10
    Last Post: April 21st, 2013, 09:28 AM
  2. 15 square puzzle
    By Yamaha360 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 10th, 2013, 02:10 PM
  3. What is a non-zero square?
    By djl1990 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 30th, 2011, 03:11 PM
  4. triad square
    By mistu in forum Member Introductions
    Replies: 2
    Last Post: August 22nd, 2011, 08:13 AM
  5. Fibonacci Spiral Help?
    By cmh0114 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 12th, 2010, 09:21 PM