Turtlegraphics for Smartphones and Tablets

Bern University of Teacher Education  
HomeStart Online EditorPrintAndroid GamesJava-Online

Fling-Events


Smartphones and tablets are often handled with a "wipe movement" where you strike with a finger over the screen. The reaction of the system depends on the direction and speed of the movement. In JTurtleLib such actions are implemented with the method playgroundFlung(). This gives the x- and y- coordinate of the starting point, the x- and Y-coordinate of the ending point, the speed and the direction of the finger movement.

In the following example, with each wipe movement a new turtle is started. The turtle moves according to the speed of the finger movement, slow or fast, turns coincidentally to the left or to the right and draws a clothoid curve.

 

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (Tu28.zip)

// Tu28.java

package app.tu28;

import turtle.*;

public class Tu28 extends Playground
{
  public void playgroundFlung(double xStart, double yStart, 
    double xEnd, double yEnd, double v, double dir)
  {
     Turtle = new Turtle(xEnd, yEnd, RED);
     t.setSpeed(MAXSPEED);
     t.setHeading(dir);
     t.setSpeed((int)(v / 100));
     cloth(t, 10, (Math.random() < 0.5)? true : false);
  }
  
  void cloth(Turtle t, int s, boolean isLeft)
  {
    if (s > 600)
      return;
    t.fd(5);
    if (isLeft)
      t.lt(0.025 * s);
    else
      t.rt(0.025 * s);
    cloth(t, s + 5, isLeft);
  }  
}

Explanations to the program code:

Turtle t = new Turtle(xEnd, yEnd, RED)

The turtle is created at the ending point of the finger movement

t.setHeading(dir)

The turtle moves in the direction of the finger movement

t.setSpeed((int)(v / 100))

turtle-speed is proportional to the speed the finger is moving

cloth(t, 10, (Math.random() < 0.5)? true : false) with the probability of 0.5 a left or right twisted curve is drawn