« Much faster array sorting ActionScript 2: Why set calls get »

November 14, 2003

SWT Flash

This is kind of old news, but I haven't really seen much about this on the internet. Every example out there demonstrating Flash in a desktop application is usually done in Visual Basic or C#. Thanks to SWT Flash, you can somewhat easily embed Flash into Java applications as well. Sadly, this is a Windows-only solution for now(?), and the Flash plug-in must already be installed.

SWT Flash hasn't been updated since 7/30/2002. It still works though, and it's a great way to integrate Flash into your Java applications. Below is some example code to help you out. You'll need Eclipse, SWT (found under "SWT Binary and Source" on the Eclipse downloads page after selecting a site and then a release) and SWT Flash. You'll also need the Flash plug-in installed, but if you're here I'm assuming you've already got that step taken care of.

After you create a new project in Eclipse, you'll need to import the flash.jar (from SWT Flash) and the swt.jar (from SWT) into the project. To do this, right click on the project name, select "Import", select "Zip File", press "Next", select "Browse", then find "flash.jar" from wherever you extracted SWT Flash to (probably "C:\eclipse\plugins\com.docuverse.swt.flash"). Repeat the process to import swt.jar from "C:\eclipse\plugins\org.eclipse.swt.win32_2.1.2\ws\win32" (your directory may vary). Don't forget to have swt-win32.2135.dll in your classpath as well (an easy way to do this is just copy the .dll from from "C:\eclipse\plugins\org.eclipse.swt.win32_2.1.2" into your project's directory on your disk).

Here is my "SWTFlash_Example" class:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

import com.docuverse.swt.flash.FlashPlayer;
import com.docuverse.swt.flash.FlashPlayerListener;

public class SWTFlash_Example implements FlashPlayerListener {	
 	Shell shell;
 	FlashPlayer player;
 	
 	private void createMenuBar() {
  		Menu menuBar = new Menu(shell, SWT.BAR);
  		shell.setMenuBar(menuBar);
  		
  		MenuItem item = new MenuItem(menuBar, SWT.CASCADE);
  		item.setText("Color");
  		Menu menu = new Menu(shell, SWT.DROP_DOWN);
  		item.setMenu(menu);
  		
  		MenuItem subItem = new MenuItem(menu, SWT.NULL);
  		subItem.setText("Red");
  		subItem.addSelectionListener(new SelectionAdapter() {
   			public void widgetSelected(SelectionEvent event) {
    				player.setVariable("buttonColor", "0xFF0000");
    			}
   		});
  		subItem = new MenuItem(menu, SWT.NULL);
  		subItem.setText("Blue");
  		subItem.addSelectionListener(new SelectionAdapter() {
   			public void widgetSelected(SelectionEvent event) {
    				player.setVariable("buttonColor", "0x0000FF");
    			}
   		});
  		subItem = new MenuItem(menu, SWT.NULL);
  		subItem.setText("Green");
  		subItem.addSelectionListener(new SelectionAdapter() {
   			public void widgetSelected(SelectionEvent event) {
    				player.setVariable("buttonColor", "0x00FF00");
    			}
   		});
  	}
 	
 	private void createFlashApp() {
  		player = new FlashPlayer(shell, SWT.NONE, this);
  		// this line loads the flash movie from disk
  		player.loadMovie(0,"c:\\eclipse\\workspace\\SWTFlashExample\\SwtFlashExample.swf");
  		player.setSize(150, 150);
  		player.activate();
  	}
 	
 	public static void main (String [] args) {
  		Display display = new Display();
  		Shell shell = new SWTFlash_Example().open (display);
  		
  		while (!shell.isDisposed()) {
   			if (!display.readAndDispatch()) display.sleep ();
   		}
  		display.dispose ();
  	}
 	
 	public Shell open (Display display) {
  		shell = new Shell (display);
  			
  		createMenuBar();
  		createFlashApp();
  		
  		shell.setSize(150, 200);
  		shell.open();
  				
  		return shell;
  	}
 	
 	public void onFSCommand(String param, String value) {
  		System.out.println("\nFSCommand:\nParam = " + param + "\nValue = " + value);
  	}
 	
 	public void onReadyStateChange(int arg) {}
 	public void onProgress(int arg) {}
}

I then created a Flash movie - "SwtFlashExample" with a movie clip (arcade_mc) that contains another movie clip (color_mc). The movie's dimensions were 150 x 150 and the code in the Flash movie (on the main timeline) looks like this:

arcade_mc.onPress = function() {
 	fscommand("pressed", ++this.count);
}

function onButtonColorChange(prop, oldval, newval) {
 	var c = new Color(arcade_mc.color_mc);
 	c.setRGB(newval);
}

this.watch("buttonColor", onButtonColorChange);

In theory (hey, nothing usually works the first time), you can then run the Java class (Run -> Run As -> Java Application) and see Flash embedded into your Java application. If you're not seeing anything, double check the .swf location in the player.loadMovie call. Make sure the path correctly points to the .swf file.

The idea in this example is that when you press the arcade_mc, a message is printed to the console. For example purposes, it is just a number that counts up on every press. You can use the "Color" menu in the application to change the color of color_mc inside the arcade_mc symbol as well. This demonstrates both Java -> Flash and Flash -> Java communication.

Hopefully this will give you a head start to using Flash in your Windows-only Java applications. Now if only SWT Flash was ported to other platforms...

Comments

  • Thanks alot for this. It helped me do one of my projects for first year CS at the University of Waterloo in Ontario, Canada.

  • Wow this is an interesting article.
    ... just too bad that SWT Flash hasn't been updated for a long time :-( Support for other platforms would be great too.

  • Thanks for the nice article. I just mentioned this on my post at http://www.beamjive.com/lounge.

  • Huge amount of good info, had no idea you were registered at flashmove, thanks again, Rich

  • Look at this: http://dctc.sjtu.edu.cn:81/wonder/releases/javaflash/
    On linux version. But no java to flash interface. I'm working on this right now.

  • great article

    is there a way to embed java within
    a flash movie with out using a server
    like Jrun?

    tnx ...