Software7

Personal Developer Notebook

Embedding Flash Into Java Applications

To be able to embed Flash into a Java application one needs a third party library. ComfyJ from TeamDev is such a library.

ComfyJ is a bidirectional Java-COM bridge which allows me to use ActiveX/COM objects from Java.

Here a screenshot with a SWF running inside a Java Swing user interface:

FlashItExample

In Java the initialization looks like:

private static final String FLASH_PROGID = "ShockwaveFlash.ShockwaveFlash";
    private OleContainer _container = new OleContainer();

    …

        OleFunctions.oleInitialize();


        getContentPane().add(_container, BorderLayout.CENTER);
        createOleObject();
        addWindowListener(new WindowAdapter()
        {
            public void windowOpened(WindowEvent e)
            {
                showOleObject();
            }

            public void windowClosing(WindowEvent e)
            {
                destroyOleObject();
            }
        });


Some simple helper methods:

private void createOleObject()
    {
        _container.createObject(FLASH_PROGID);
    }

    private void showOleObject()
    {
        _container.doVerb(OleVerbs.SHOW);
    }

    private void destroyOleObject()
    {
        _container.destroyObject();
    }

To open a SWF file:

OleMessageLoop.invokeMethod(this, "openFile", new Object[]{filename});

And finally here is the openFile method:

public void openFile(String filePath)
    {
        File file = new File(filePath);

        if(!file.exists()) {
            throw new RuntimeException("Couldn't find file with movie: " + filePath);
        }

        IDispatchImpl flash = new IDispatchImpl(_container.getOleObject());
        Automation flashAutomation = new Automation(flash);

        // load movie
        flashAutomation.setProperty("Movie", filePath);
        flashAutomation.invoke("Play");
    }


The class path should look similar to:

flashIt.jar;lib/comfyj-2.7.jar;lib/jniwrap-3.8.jar;lib/log4j-1.2.15.jar;lib/slf4j-api-1.5.8.jar;lib/slf4j-log4j12-1.5.8.jar;lib/winpack-3.8.jar

And don’t forget to pass a parameter to the Java VM with the (relative) path to the folder with the native DLLs:

-Djava.library.path=dll

BTW, I’m not associated with TeamDev other than being a mostly happy user. Here’s the link to the library:

http://www.teamdev.com/comfyj/