Tuesday, May 8, 2012

A Turtle Drawing in java

//use the Turtle class to draw a large multifaceted geometrical shape onto a picture. These modifications are
 //to add "something" to the image. Normally this would be to draw something in the foreground of an image
//that is just a scene.
//*** a specific image was used for this project.



import java.awt.Color;

  public class proj1

{


public static void main(String[] args)

{

// file chooser to pick an image.

String filename = FileChooser.pickAFile();
Picture p = new Picture(filename);

//creat turtles.

Turtle t = new Turtle(p);
Turtle t1 = new Turtle(p);

// set pen width for both turtles.

t.setPenWidth(2);
p.show();
t1.setPenWidth(3);
t1.penUp();
t1.turnLeft();
t1.forward(400);
t1.turn(-90);
t1.forward(265);
t1.penDown();

// locating the turtles usind penUp.

t.penUp();
t.turnRight();
t.forward(230);
t.turn(-90);
t.forward(22);
t.penDown();



// call a method to draw a rectangle
t1.penUp();
t1.turn(-90);
t1.forward(300);
t1.turn(-90);
t1.forward(300);
t1.turn(180);
t1.penDown();
drawRectangle (t1,100,500);



// Drawing a flag for the ship.
t1.turnRight();
t1.forward(100);
t1.turn(90);
t1.forward(300);
t1.turn(135);
t1.forward(80);
t1.turnRight();
t1.forward(80);
t1.penUp();
t1.turn(45);
t1.forward(300);

t1.penDown();

int i1=0;
while (i1<10)
{


// draw a star
t1.turn(36);
t1.forward(130);
t1.turn(144);
t1.forward(130);
t1.turn(144);
t1.forward(130);
t1.turn(144);
t1.forward(130);
t1.turn(144);
t1.forward(130);


i1=i1+1;
} // end while loop.





int i2 = 1;
int len;

while(i2 < 8)
{
len = i2 * 25 ;

// Call a method
drawHexagon (t, len);

t.penUp();
t.turn(-90);
t.forward(20);
t.turn(-90);
t.forward(15);
t.turn(180);
t.penDown();

i2=i2+1;
} // End of while loop


}

public static void drawHexagon ( Turtle t, int size)
{
int i;
i = 1;
// Meathod to draw a Hexagon.
while (i <= 6)
{

t.forward(size);
t.turn(360/6);

i=i+1;
}

}


public static void drawRectangle(Turtle t1,int sides, int length )
{

int i = 1;
while(i <= 4)
{
t1.forward(sides);
t1.turn(90);
t1.forward(length);
t1.turn(90);

i=i+1;
} // End of while Loop

}
}





No comments:

Post a Comment