Lab3 - Turtle Graphics
1.Create a directory called Star in your private folder. Change directory to the Star directory. Using pico or your favorite text-editor, create and save the Java program Star.java shown below (do not attempt to compile it yet). Download the TG.jar file from the course website and save it in the Star directory. TG.jar contains several classes that support programming in a TurtleGraphicsWindow. See the link http://www.bfoit.org/Intro_to_Programming/TurtleGraphicsWindowJavaDoc.html for information about the commands.
public class Star{
public static void main(String[] args){
TurtleGraphicsWindow t = new TurtleGraphicsWindow();
int x, y;
t.setpensize(5); //initialize settings and position turtle
t.penup();
t.setxy(-100,50);
t.right(90);
t.pendown(); //ready for serious work
for(int k=1; k<=5; k++){
t.forward(200);
t.right(144);
}
t.penup();
t.hideturtle();
t.setxy(120,50);
t.setlabelheight(40);
t.label("Star");
}
}

2. Extract all the files from TG.jar by entering the command
jar –xf TG.jar

3. To compile Star.java enter the command
javac Star.java

4. To run this program, enter the command listed below at the system prompt $ (unix) or C:\>: (Windows).
java Star
The TurtleGraphicsWindow displays the work done by the turtle.

5. Create a Turtle Graphics program that produces the output shown below. Demo this program for the lab instructor.

6. Run the program below.
public class Curves{
public static void main(String[] args){
TurtleGraphicsWindow t = new TurtleGraphicsWindow();
double A = 5.0; //Experiment, change parameters A and B
double B = 2.0;
double C = 200.0;//Experiment, change parameters C and D
double D = 200.0;
t.penup();
t.setpencolor(4);
t.setpensize(1);
t.setxy((int)(C*Math.cos(0.0)),(int)(D*Math.sin(0.0)));
t.pendown();
for(int k=0; k<=360; k++){
t.setxy((int)(C*Math.cos(A*k)),(int)(D*Math.sin(B*k)));
}
t.penup();
t.hideturtle();
}
}

Experiment by changing the parameters A, B, C and D or changing pen colors to come up with other interesting diagrams.
7. Create a program to display the following.

Since this up-down image is produced by repeatedly drawing

Modify your program so that the main() calls a function which uses a for-loop to draw the up-down image.
|