Play and Control Flash Movies with JFlashPlayer
VersaEdge Software has created a tool for playing and controlling Flash movies from Java applications and applets that run on Windows platforms. You can download a trial version of this JFlashPlayer: Java Flash Player API For Windows tool by clicking the Download Free Trial link on VersaEdge’s Java APIs: JFlashPlayer Web page. After exploring the trial version, you might want to purchase a license to use JFlashPlayer in your commercial code. Four commercial licenses are available for purchase. Their costs, purchase links, and other details are specified on the JFlashPlayer Web page.
After downloading and unzipping JFlashPlayer’s trial version distribution file (jflashtrial.zip), install this tool by moving its home directory to a more appropriate directory (c:\jflashtrial, for example) by adding jflashtrial’s jflash.jar Jar file to your CLASSPATH (set classpath=%classpath%;c:\jflashtrial\jflash.jar;) and by making sure that jflashtrial’s atl2k.dll (a required library for Windows NT/2000/XP), atl98.dll (a required library for Windows 95/98/ME), and jflash.dll DLL files are accessible via your PATH environment variable (set path=%path%;c:\jflashtrial accomplishes that task). In addition to being properly installed on your platform, JFlashPlayer needs Macromedia’s flash.ocx ActiveX control. If this file has not been installed, JFlashPlayer cannot play Flash movies. I’ll talk about flash.ocx later in this article.
JFlashPlayer includes an example SampleFrame application that shows you how to integrate a Flash movie player with a Java application. Furthermore, this application demonstrates some of the features that the Java Flash Player API makes available, such as listening for Flash events, setting a Flash variable, playing and stopping a Flash movie, and determining whether or not the movie should run in a loop. Run this application by invoking the runsample.bat file, which locates in jflashtrial’s sample subdirectory. (After including the DLL files in the PATH, that batch file executes java -classpath ..\jflash.jar;. SampleFrame.) The SampleFrame application first reveals a dialog box that plays a "Welcome To JFlashPlayer" Flash movie. After you click the dialog box’s OK button, the window shown in Figure 2 appears. The top portion of this window displays various controls; the rest of the window presents the current frame in a sample Flash movie.

Figure 2 The SampleFrame application demonstrates various classes and methods in the Java Flash Player API.
The SampleFrame application’s SampleFrame.java source code—which locates in the sample subdirectory—references two of the Java Flash Player API’s three classes and that API’s solitary interface. These classes and interface are stored in package jflashplayer, corresponding to the jflash.jar Jar file:
- FlashPanel is the package’s main class. This java.awt.Panel subclass identifies a component that integrates a Flash movie player into a Java-based GUI and makes it possible to play Flash movies within the component’s bounds. There are eight constructors to choose from when creating a FlashPanel.
- FlashPanelListener is the package’s solitary interface. This interface presents a single public void FSCommand(String command, String arg) method that is called every time the Flash player invokes the ActionGetURL action, and that action has a URL beginning with FSCommand. For example, the movie.swf file played by SampleFrame identifies two buttons. Each button, when clicked, invokes an ActionGetURL action. For one of these actions, its URL is set to FSCommand:javaExecute, and its target window is set to the empty string. When this action executes, the FSCommand() method is called where the value of command is set to javaExecute and the value of arg is set to the empty string. For the other action, its URL is set to FSCommand:javaLink, and its target window is set to http://www.javaapis.com/. When this action executes, FSCommand() is called: command is set to javaLink and arg is set to http://www.javaapis.com/.
- JFlashInvalidFlashException describes an exception that is thrown from one of FlashPanel’s constructors when you call FlashPanel’s public static void setRequiredFlashVersion(String ver) method with a specific version number as the String argument prior to invoking the FlashPanel constructor, and the required version of Flash is not available.
- JFlashLibraryLoadFailedException describes an exception that is thrown from FlashPanel’s public static boolean hasFlashVersion(String version) method if one or more of atl2k.dll, atl98.dll, and jflash.dll is missing.
A JFlashPlayer-based Java application requires that a FlashPanel component be added to the GUI. But before you create the FlashPanel object, you need to make sure that a flash.ocx ActiveX control (the Flash movie player) is present—JFlashPlayer’s DLLs ultimately communicate with flash.ocx—and that flash.ocx is capable of playing your Flash movie(s). If there is no appropriate flash.ocx on your platform, FlashPanel lets you install that control and even uninstall the control before the application exits. (It’s a good idea to ask the user for permission before installing a specific version of flash.ocx.) The following code fragment shows you how to determine the existence of a specific version of flash.ocx and install that file (which you would presumably distribute with your application) if the required flash.ocx file does not exist:
if (!FlashPanel.hasFlashVersion ("7")) if (!FlashPanel.installFlash (new File ("flash.ocx"))) { System.err.println ("Appropriate flash.ocx file not available"); return; }
The code fragment calls hasFlashVersion() to determine whether flash.ocx version 7 (or higher) is present. That method returns true if at least version 7 is present. However, if a lower version of flash.ocx is present, or if flash.ocx is not present, that method returns false and FlashPanel’s public static boolean installFlash(java.io.File flashocx) method is called to install the proper flash.ocx file. That method returns true if it successfully installs flash.ocx. But if it fails, false returns, an error message outputs to the console, and the currently executing method is terminated.
After executing the previous code fragment, you are guaranteed that the version of flash.ocx is present. However, if you want additional assurance or if you choose not to test installFlash()’s return value, you would call setRequiredFlashVersion(), specifying the desired version of your Flash movie. If you call setRequiredFlashVersion() with a version number that is not supported by flash.ocx, the FlashPanel constructors throw JFlashInvalidFlashException objects. The code fragment below demonstrates a call to this method, which indicates that you require version 7 of the flash.ocx file to run your Flash movie(s):
FlashPanel.setRequiredFlashVersion ("7");
At this point, you can go ahead and create your FlashPanel component. For example, you could call the public FlashPanel(File f, java.awt.Image image) constructor to create a FlashPanel and start playing the Flash movie identified by the f-referenced File object. If any of JFlashPlayer’s DLL files is missing, if the appropriate flash.ocx file is not present, or if the Flash movie file cannot be found, FlashPanel displays the alternate image, stretched to the size of its boundaries. The following code fragment, which I excerpted from SampleFrame.java, returns a custom alternate Image and invokes this constructor to establish a FlashPanel for the welcome.swf movie:
Image imageWelcome = getImageOfString ("Welcome", Color.blue); FlashPanel welcomePanel = new FlashPanel (new File ("welcome.swf"), imageWelcome);
After creating the FlashPanel component, you’ll want to add it to your GUI. The code fragment below, which I also excerpted from SampleFrame.java, adds this component to a JPanel and then passes that JPanel to one of javax.swing.JOptionPane’s dialogs:
JPanel jPanelWelcome = new JPanel (new BorderLayout ()); jPanelWelcome.add (welcomePanel, BorderLayout.CENTER); jPanelWelcome.setPreferredSize (new Dimension (200, 150)); JOptionPane.showMessageDialog (null, jPanelWelcome, "Welcome To JFlashPlayer", JOptionPane.PLAIN_MESSAGE);
In response to your application’s GUI events, you can now call FlashPanel’s public void stop() method to stop the movie, public void back() method to move back one frame in the movie, public void forward() method to move ahead one frame in the movie, public void rewind() method to rewind the movie, public void play() method to restart the movie from its new location, public void setLoop(boolean loop) method to keep playing the movie in a loop (if loop is true), and so on.
If you installed a flash.ocx file, you will want to remove it before your application exits. FlashPanel makes this possible by providing the public static boolean uninstallFlash(File flashocx) method. Just as installFlash() returns true if installation succeeds, uninstallFlash() returns true if uninstallation is successful. You should set a Boolean flag to true when installing flash.ocx, and check this flag before uninstalling that file, to make certain that you do not accidentally uninstall what you did not install. The following code fragment demonstrates setting and testing this flag:
if (!FlashPanel.hasFlashVersion ("7")) if (!FlashPanel.installFlash (new File ("flash.ocx"))) { System.err.println ("Appropriate flash.ocx file not available"); return; } else fInstalled = true; // ... if (fInstalled) FlashPanel.uninstallFlash (new File ("flash.ocx"));
At some point, you will want to experiment with the aforementioned methods. To help get you started, I have put together a Java application that presents a framework for integrating a Flash movie player into the application’s Swing-based GUI. Listing 3 presents that application’s Flasher.java source code.
Listing 3 Flasher.java
// Flasher.java import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.File; import javax.swing.*; import javax.swing.event.*; import javax.swing.filechooser.*; import jflashplayer.*; public class Flasher extends JFrame { FlashPanel fp; JFileChooser fc; public Flasher (String title) { // Pass title to superclass. super (title); // Terminate application when user clicks x button in titlebar. setDefaultCloseOperation (EXIT_ON_CLOSE); // Create a new file chooser and make sure it starts up displaying SWF // files in the current directory. fc = new JFileChooser (); fc.setCurrentDirectory (new File (".")); // Make sure that only directories and files ending with the .swf file // extension are displayed. fc.setFileFilter (new SWFFilter ()); // Make sure that the user cannot select the "All Files" entry in the // "Files of Type" drop-down listbox. fc.setAcceptAllFileFilterUsed (false); // Create a File menu with menu items to open movies and exit this // application. Because FlashPanel is a heavyweight component, and // because JMenus typically associate with lightweight popup menus that // are hidden behind heavyweight components, we must disable the File // JMenu’s lightweight popup. As a result, the popup will be heavyweight, // so that it is no hidden behind the FlashPanel. JMenu menuFile = new JMenu ("File"); menuFile.getPopupMenu ().setLightWeightPopupEnabled (false); // Install the Open menu item on the File menu and register the action // listener to open the user-selected movie. ActionListener al; JMenuItem mi = new JMenuItem ("Open..."); al = new ActionListener () { public void actionPerformed (ActionEvent e) { if (fc.showOpenDialog (Flasher.this) == JFileChooser.APPROVE_OPTION) try { fp.setMovie (fc.getSelectedFile ()); } catch (Exception e2) { JOptionPane.showMessageDialog (Flasher.this, e2.getMessage (), "Flasher", JOptionPane. ERROR_MESSAGE); } } }; mi.addActionListener (al); menuFile.add (mi); // Separate the Open menu item from the Exit movie item. menuFile.addSeparator (); // Install the Exit menu item on the File menu and register the action // listener to terminate the application. mi = new JMenuItem ("Exit"); mi.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { dispose (); } }); menuFile.add (mi); // Create a menu bar to hold the file menu, add the file menu to the // menu bar, and register the menu bar as this application’s menu bar. JMenuBar mb = new JMenuBar (); mb.add (menuFile); setJMenuBar (mb); try { // Attempt to create a FlashPanel component. fp = new FlashPanel (new File (".")); // Add component to application’s main window. getContentPane ().add (fp); } catch (Exception e) { e.printStackTrace (); } // Establish a default size for viewing Flash movies. User can always // drag the main window’s borders or maximize the window to make the // window bigger. setSize (400, 400); // Display the GUI. setVisible (true); } public static void main (String [] args) { try { // Verify that at least version 7 of flash.ocx is present. if (!FlashPanel.hasFlashVersion ("7")) { System.err.println ("Required version of Flash not available"); return; } } catch (JFlashLibraryLoadFailedException e) { System.err.println ("One or more JFlashPlayer DLLs not found"); return; } // If we make it here, the required flash.ocx is present, and the // JFlashPlayer DLLs are also present. Go ahead and create the GUI. new Flasher ("Flasher"); } } class SWFFilter extends FileFilter { // Accept all directories and swf files. public boolean accept (File f) { if (f.isDirectory ()) return true; // Check extension for swf. String s = f.getName (); int i = s.lastIndexOf (’.’); if (i > 0 && i < s.length () - 1) if (s.substring (i + 1).toLowerCase ().equals ("swf")) return true; return false; } public String getDescription () { return "Accepts swf files only."; } }
Because Listing 3 is extensively commented, I haven’t much to say about the source code. However, take a look at fp = new FlashPanel (new File ("."));. That line of code creates a FlashPanel without loading a Flash movie. (You could also substitute a single space for the period character to accomplish the same task. Anything else would probably result in a java.io.FileNotFoundException.) Whenever you need to create a FlashPanel and don’t want to immediately load a Flash movie into that component, you can use the previous line of code to accomplish that task.
After compiling Listing 3, run this application. Select Open from the File menu and choose a SWF file. The Flash movie stored in that SWF file will start to play. For example, suppose you previously ran UpdateSquareCircle to modify squarecircle.swf and you now select the modified squarecircle.swf file. In response, you will see the blue square that appears in Figure 3.

Figure 3 A blue square moves in a circle over a green background.
I purposely minimized Listing 3’s functionality to give you some work to do. For example, you could introduce a Player menu with options for stopping, playing, rewinding, moving forward, moving backward, and choosing whether or not the movie plays in a loop. I’m sure you can think of other features to add to this application, as well.