0

I am reading "Algorithms 4th Edition" by Robert Sedgewick and Kevin Wayne.
Using StdDraw, we can create animation:

package general_test;

import edu.princeton.cs.algs4.StdDraw;

public class AnimationTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       StdDraw.setScale(-2, +2);
       StdDraw.enableDoubleBuffering();

       for (double t = 0.0; true; t += 0.02) {
           double x = Math.sin(t);
           double y = Math.cos(t);
           StdDraw.clear();
           StdDraw.filledCircle(x, y, 0.05);
           StdDraw.filledCircle(-x, -y, 0.05);
           StdDraw.show();
           StdDraw.pause(20);
       }
    }

}

I want to save this animation as a file.
How can I save this animation as a file?
Any easy way?

tchappy ha
  • 185
  • 6

1 Answers1

1

Very easy way

  1. Add counter.
int counter = 0;
  1. Limit the number of iteration steps to let's say 1000.
for (double t = 0.0; t < 20; t += 0.02)
  1. Save every iteration step as an PNG file.
StdDraw.save("image-"+String.format("%03d", counter++)+".png");
  1. Use ffmpeg to convert these files to a video file.
ffmpeg -i image-%03d.png video.webm

Enjoy!

References