Turtlegraphics for Smartphones and Tablets

Bern University of Teacher Education
HomeStart Online EditorPrintAndroid GamesJava-Online

for-loop (Iteration)

Instead of a while-loop a for-loop can be used. Edit source with online-compiler Install App on smartphone or tablet download sources (Tu3.zip)

 // Tu3.java package app.tu3; import turtle.*; public class Tu3 extends Playground {   public void main()   {     st();     for (int i = 0; i < 9; i++)     {       fd(160);       rt(160);        }   } } Explanations to the program code:

 for (int i = 0; i < 9; i++) Initialisation (int i = 0), running condition (i < 9) and value change (i++) is, separated by semicolons, typed in a line

Beautiful pictures are created by changing the rotation angle. In the example Tu3a we leave the turtle hidden on purpose. (The line st()) is missing). Therefore the drawing of the graphic is faster. Install App on smartphone or tablet download sources (Tu3a.zip)

 // Tu3a.java package app.tu3a; import turtle.*; public class Tu3a extends Playground {   public void main()   {     for (int i = 0; i < 250; i++)       fd(i).rt(89);   } } Explanations to the program code:

 fd(i) The drawn straight lenght initially is 0, after the first loop run 1, than 2, 3, .. to 250 fd(i).rt(89) Multiple drawing-methods, separated by full stops, can be written on the same line

Banked for-loops

The loops can be banked. In the example Tu4.java a square is drawn with the inner loop. With the outer loop, the drawing of the square is repeated 10 times. Install App on smartphone or tablet

 // Tu4.java package app.tu4; import turtle.*; public class Tu4 extends Playground {   public void main()   {     st();     setSpeed(10);     for (int k = 0; k < 10; k++)     {       for (int i = 0; i < 4; i++)       {         fd(100);         rt(90);       }       lt(36);     }   } } Explanations to the program code:

 setSpeed(10) Turtle is moving faster (standard value 5)

coordinate graphic

By using the method setPos(x, y) and moveTo(x, y) line graphics, that are known from the coordinate graphic, can be drawn. Install App on smartphone or tablet

 // Tu4a.java package app.tu4a; import turtle.*; public class Tu4a extends Playground {   public void main()   {     for (int i = -160; i <= 160; i = i + 40)     {       for (int k = -160; k <= 160; k = k + 40)       {         setPos(i, -160);         moveTo(k, 160);       }     }          for (int i = -160; i <= 160; i =  i + 40)     {       for (int k = -160; k <= 160; k = k + 40)       {         setPos(-160, i);         moveTo(160, k);       }     }   } } Explanations to the program code:

 setPos(x, y) Sets the turtle on the position with the coordinates (x, y) moveTo(x, y) Moves the turtle from the current position to the position (x, y)