Results 1 to 2 of 2

Thread: For Loop Help

  1. #1
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default For Loop Help

    Hey I have made a koch curve below. I have a huge code which can be shortened to a for loop. Can someone help me make it into a for loop. Thanks

    Java Code:
    package lab_5;
    
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TurtleDriver extends JPanel
    {
        JFrame frame;
    
        public TurtleDriver() {
    	super();
    	frame = new JFrame("Testing the Turtle");
    	frame.setSize(600, 600);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.add(this);
    	frame.setVisible(true);
            this.setBackground(Color.white);
        }
    
        public void paintComponent(Graphics g) {
    	Graphics2D graphics = (Graphics2D)g;
    	int x0 = this.getX();
    	int y0 = this.getY();
    	int width = this.getWidth();
    	int height = this.getHeight();
    	Turtle turtle = new Turtle(frame, graphics, x0, y0, width, height);
    	turtle.moveTo(0.3, .8, 0.0);
            turtle.spiraMirabilis(60.0,0.001,1.09);
            turtle.moveTo(0.6, .8, 0.0);
            turtle.spiraMirabilis(75.0,0.001,1.09);
            
    
    
    
            //Three Sides
            turtle.moveTo(0.1, 0.1, 0.0);
            turtle.koch(0.1,3);
            turtle.turnLeft(-120);
            turtle.koch(0.1,3);
            turtle.turnLeft(-120);
            turtle.koch(0.1,3);
    
    
    	// Regular polygon with 4 sides
    	turtle.moveTo(0.3, 0.15, 0.0);
            turtle.koch(0.1,3);
            turtle.turnLeft(-90);
            turtle.koch(0.1,3);
            turtle.turnLeft(-90);
            turtle.koch(0.1,3);
            turtle.turnLeft(-90);
            turtle.koch(0.1,3);
            turtle.turnLeft(-90);
            turtle.koch(0.1,3);
    
    
        // Regular polygon with 5 sides
        	turtle.moveTo(0.5, 0.18, 0.0);
            turtle.koch(0.1,3);
            turtle.turnLeft(-72);
            turtle.koch(0.1,3);
            turtle.turnLeft(-72);
            turtle.koch(0.1,3);
            turtle.turnLeft(-72);
            turtle.koch(0.1,3);
            turtle.turnLeft(-72);
            turtle.koch(0.1,3);
            turtle.turnLeft(-72);
            turtle.koch(0.1,3);
    
    //regular polygon with 6 sides
              	turtle.moveTo(0.7, 0.22, 0.0);
            turtle.koch(0.1,3);
            turtle.turnLeft(-60);
            turtle.koch(0.1,3);
            turtle.turnLeft(-60);
            turtle.koch(0.1,3);
            turtle.turnLeft(-60);
            turtle.koch(0.1,3);
            turtle.turnLeft(-60);
            turtle.koch(0.1,3);
            turtle.turnLeft(-60);
            turtle.koch(0.1,3);
            turtle.turnLeft(-60);
            turtle.koch(0.1,3);
    
    
           //polygon with 7 sides
            turtle.moveTo(0.7, 0.5, 0.0);
            turtle.koch(0.1,3);
            turtle.turnLeft(-51.428571429);
            turtle.koch(0.1,3);
            turtle.turnLeft(-51.428571429);
            turtle.koch(0.1,3);
            turtle.turnLeft(-51.428571429);
            turtle.koch(0.1,3);
            turtle.turnLeft(-51.428571429);
            turtle.koch(0.1,3);
            turtle.turnLeft(-51.428571429);
            turtle.koch(0.1,3);
            turtle.turnLeft(-51.428571429);
            turtle.koch(0.1,3);
            turtle.turnLeft(-51.428571429);
            turtle.koch(0.1,3);
    
    
            //polygon with 8 sides
            turtle.moveTo(0.3, 0.5, 0.0);
            turtle.koch(0.1,3);
            turtle.turnLeft(-45);
            turtle.koch(0.1,3);
            turtle.turnLeft(-45);
            turtle.koch(0.1,3);
            turtle.turnLeft(-45);
            turtle.koch(0.1,3);
            turtle.turnLeft(-45);
            turtle.koch(0.1,3);
            turtle.turnLeft(-45);
            turtle.koch(0.1,3);
            turtle.turnLeft(-45);
            turtle.koch(0.1,3);
            turtle.turnLeft(-45);
            turtle.koch(0.1,3);
            turtle.turnLeft(-45);
            turtle.koch(0.1,3);
    
        }
    
        
    
        public static void main(String[] args) {
    	new TurtleDriver();
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Can you explain what your problem is with coding a for loop?
    That is one of the more commonly used statements.
    What does your textbook or tutorial say about how to code a for loop?

Similar Threads

  1. Help with loop
    By Scatty in forum New To Java
    Replies: 4
    Last Post: 05-18-2010, 07:23 AM
  2. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  3. do while loop?
    By shroomiin in forum New To Java
    Replies: 2
    Last Post: 11-13-2009, 10:32 AM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. while loop help
    By kathyla18 in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 06:49 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
  •