sim.util.gui.MovieMaker Maven / Gradle / Ivy
Show all versions of mason Show documentation
/*
Copyright 2006 by Sean Luke and George Mason University
Licensed under the Academic Free License version 3.0
See the file "LICENSE" for more information
*/
package sim.util.gui;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.image.*;
/**
A class which gives a GUI front-end to sim.util.media.MovieEncoder. You create a MovieMaker by passing in some Frame as a parent. Then you call start(image), where the image is a "typical" sized image for the movie frame. MovieEncoder will then show dialog panels allowing the user to specify where to save the movie and what format and frame rate to use. If the user cancels, then start(image) returns false. Else it returns true and the MovieMaker is ready for action.
At this point you can start feeding the MovieMaker frames with add(image). When you are finished, call stop() and the MovieMaker will flush out the remaining movie frames to disk and create the file. Throw your MovieMaker away at this point.
MovieMaker, like MovieEncoder, relies on the Java Media Framework (JMF) to do its magic. If JMF doesn't exist, MovieMaker doesn't produce an error; instead, it will produce a dialog box informing the user of his mistake. MovieMaker is coded in an odd way: no actual direct references are made to MovieEncoder. This is in case the JVM is too smart and tries to load MovieEncoder (and the JMF) immediately rather than lazily as it gets referenced by MovieMaker.
Note: Sun's JMF spawns threads in the background which it never cleans up.
Thus if you use this class, you'll need to call System.exit(0) to quit your program
rather than just dropping out of main().
*/
public class MovieMaker
{
Frame parentForDialogs;
Object encoder;
Class encoderClass;
boolean isRunning;
static final float DEFAULT_FRAME_RATE = 10.0f;
public MovieMaker(Frame parent)
{
this.parentForDialogs = parent;
try
{
encoderClass = Class.forName("sim.util.media.MovieEncoder", true, Thread.currentThread().getContextClassLoader());
}
catch (Throwable e) { encoderClass = null; } // JMF's not installed
}
/** Create a dialog box allowing the user to specify where to save the file, and in what format and frame rate (default = 10 frames per second), and set up the movie encoding process ready to go, using typicalImage as an example image (for size purposes). Return false if failed to start. */
public synchronized boolean start(BufferedImage typicalImage)
{
return start(typicalImage, DEFAULT_FRAME_RATE);
}
/** Create a dialog box allowing the user to specify where to save the file, and in what format and frame rate (default provided), and set up the movie encoding process ready to go, using typicalImage as an example image (for size purposes). Return false if failed to start. */
public synchronized boolean start(BufferedImage typicalImage, float fps)
{
if (isRunning) return false;
int encodeFormatIndex = 0;
try
{
// get the list of supported formats
Object[] f = (Object[]) encoderClass.
getMethod("getEncodingFormats", new Class[] {Float.TYPE, BufferedImage.class}).
invoke(null, new Object[] { new Float(fps), typicalImage });
if (f==null) return false;
// init the dialog panel
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
// Word-wrap the format names for display
String[] fmts = new String[f.length];
// get the standard font
String font = p.getFont().getFamily();
// ultimate classy list mode
for(int i=0; i" + WordWrap.toHTML(WordWrap.wrap(f[i].toString(), 40)) + "