Recording a Video
It is often useful to record a video of an animation sequence. The VideoRecorder interface, shown in Listing 1, defines methods that make this easy.
Listing 1. VideoRecorder Methods
|
public void createVideo() throws IOException; // displays “save as” dialog |
A video is empty when first created. Frames are added by passing Java images to the recorder. When a video is created without a specified width and height, its dimensions are those of the first image added. Once a video has been saved, no further frames can be added.
Recording an Animation Video
An example that uses VideoRecorder to record a simple animation of a rotating arrow (screenshot below) is shown in Listing 2.
Figure 2. Recording an Animation

Listing 2. VideoRecorderApp
|
import java.io.*; public class GifRecorderApp { public GifRecorderApp() { // create a drawing panel and frame // record and save a video // create a gif video recorder // create an image to use for rendering // add an arrow to animate // define the video file and set the frame duration // get gif encoder to set optional repeat in browser (default is 1) // gif encoder can also set transparent color or quality // add animation frames to the video // save the completed video file // play the new video public static void main(String[] args) { |
In this application, we create a VideoRecorder and use it to record an animated gif video of a rotating arrow that will play at 20 fps. In the animation loop, we render an image of each frame and add it to the video. When all of the frames have been added, the video is saved, then played.
Note that the video is created and saved inside a try-catch block. This is required since these methods may throw an IOException.