Flasher Archive
// Using this class you can add a trivial main program to any Applet // and run it directly, as well as from a browser or the appletviewer. // And unlike some versions of this concept, MainFrame implements both // images and sound. //
// Sample main program: //
// The only methods you need to know about are the constructors. //// public static void main( String[] args ) // { // new Acme.MainFrame( new ThisApplet(), args, 400, 400 ); // } //
// You can specify Applet parameters on the command line, as name=value. // For instance, the equivalent of: //
// would just be: //// <PARAM NAME="pause" VALUE="200"> //
// You can also specify three special parameters: //// pause=200 //
//// width=N Width of the Applet. // height=N Height of the Applet. // barebones=true Leave off the menu bar and status area. //
// Fetch the software.
// Fetch the entire Acme package.
public class FlashPlayer extends Panel implements Runnable, AppletStub, AppletContext
{
private String[] args = {"MOVIE=http://www.macromedia.com/uber.swf", "WIDTH=640", "HEIGHT=480", "FORCERGB=FALSE", "LOOP=TRUE", "PLAY=TRUE", "QUALITY=BEST", "SALIGN=TL", "SCALE=SHOWALL"};
// private static int instances = 0;
private String name;
// private boolean barebones = false;
private Applet applet;
// private Label label = null;
private Dimension appletSize;
private Vector listeners;
private static final String PARAM_PROP_PREFIX = "parameter.";
/// Constructor with everything specified.
public FlashPlayer( String[] args, int width, int height )
{
build( args, width, height );
}
/// Constructor with no default width/height.
public FlashPlayer( String[] args )
{
build( args, -1, -1 );
}
/// Constructor with no arg parsing.
public FlashPlayer( int width, int height )
{
build( null, width, height );
}
/// Constructor with no args at all--unfortunately necessary to be a JavaBean.
public FlashPlayer()
{
build( args, -1, -1);
}
// Internal constructor routine.
private void build( String[] args, int width, int height )
{
// ++instances;
this.applet = (Applet)new Flash();
this.args = args;
applet.setStub( this );
name = applet.getClass().getName();
// setTitle( name );
// Set up properties.
Properties props = System.getProperties();
props.put( "browser", "Acme.MainFrame" );
props.put( "browser.version", "11jul96" );
props.put( "browser.vendor", "Acme Laboratories" );
props.put( "browser.vendor.url", "http://www.acme.com/" );
// Turn args into parameters by way of the properties list.
if ( args != null )
parseArgs( args, props );
// If width and height are specified in the parameters, override
// the compiled-in values.
String widthStr = getParameter( "width" );
if ( widthStr != null )
width = Integer.parseInt( widthStr );
String heightStr = getParameter( "height" );
if ( heightStr != null )
height = Integer.parseInt( heightStr );
// Were width and height specified somewhere?
if ( width == -1 || height == -1 )
{
System.err.println( "Width and height must be specified." );
return;
}
/*
// Do we want to run bare-bones?
String bonesStr = getParameter( "barebones" );
if ( bonesStr != null && bonesStr.equals( "true" ) )
barebones = true;
if ( ! barebones )
{
// Make menu bar.
MenuBar mb = new MenuBar();
Menu m = new Menu( "Applet" );
m.add( new MenuItem( "Restart" ) );
m.add( new MenuItem( "Clone" ) );
m.add( new MenuItem( "Close" ) );
m.add( new MenuItem( "Quit" ) );
mb.add( m );
setMenuBar( mb );
}
*/
// Lay out components.
setLayout( new BorderLayout() );
add( "Center", applet );
/*
if ( ! barebones )
{
Panel borderPanel =
new Acme.Widgets.BorderPanel( Acme.Widgets.BorderPanel.RAISED );
borderPanel.setLayout( new BorderLayout() );
label = new Label( "" );
borderPanel.add( "Center", label );
add( "South", borderPanel );
}
*/
// Set up size.
// pack();
validate();
appletSize = applet.size();
// appletSize = applet.getSize();
applet.resize( width, height );
// applet.setSize( width, height );
show();
// Start a separate thread to call the applet's init() and start()
// methods, in case they take a long time.
(new Thread( this )).start();
}
// Turn command-line arguments into Applet parameters, by way of the
// properties list.
private static void parseArgs( String[] args, Properties props )
{
for ( int i = 0; i < args.length; ++i )
{
String arg = args[i];
int ind = arg.indexOf( '=' );
if ( ind == -1 )
props.put( PARAM_PROP_PREFIX + arg.toLowerCase(), "" );
else
props.put( PARAM_PROP_PREFIX + arg.substring( 0, ind ).toLowerCase(), arg.substring( ind + 1 ) );
}
}
/*
/// Event handler for the menu bar.
public boolean handleEvent( Event evt )
{
switch ( evt.id )
{
case Event.ACTION_EVENT:
if ( evt.arg.equals( "Restart" ) )
{
applet.stop();
applet.destroy();
Thread thread = new Thread( this );
thread.start();
}
else if ( evt.arg.equals( "Clone" ) )
{
try
{
new MainFrame( (Applet) applet.getClass().newInstance(), args, appletSize.width, appletSize.height );
}
catch ( IllegalAccessException e )
{
showStatus( e.getMessage() );
}
catch ( InstantiationException e )
{
showStatus( e.getMessage() );
}
}
else if ( evt.arg.equals( "Close" ) )
{
setVisible( false );
remove( applet );
applet.stop();
applet.destroy();
if ( label != null )
remove( label );
dispose();
--instances;
if ( instances == 0 )
System.exit( 0 );
}
else if ( evt.arg.equals( "Quit" ) )
System.exit( 0 );
break;
case Event.WINDOW_DESTROY:
System.exit( 0 );
break;
}
return super.handleEvent( evt );
}
*/
// Methods from Runnable.
/// Separate thread to call the applet's init() and start() methods.
public void run()
{
showStatus( name + " initializing..." );
applet.init();
validate();
showStatus( name + " starting..." );
applet.start();
validate();
showStatus( name + " running..." );
}
/// We need to be able to stop in order to serialize ourselves.
public void stop()
{
showStatus( name + " stopping..." );
applet.stop();
validate();
}
// Methods from AppletStub.
public boolean isActive()
{
return true;
}
public URL getDocumentBase()
{
// Returns the current directory.
String dir = System.getProperty( "user.dir" );
String urlDir = dir.replace( File.separatorChar, '/' );
try
{
return new URL( "file:" + urlDir + "/");
}
catch ( MalformedURLException e )
{
return null;
}
}
public URL getCodeBase()
{
// Hack: loop through each item in CLASSPATH, checking if
// the appropriately named .class file exists there. But
// this doesn't account for .zip files.
String path = System.getProperty( "java.class.path" );
Enumeration st = new StringTokenizer( path, ":" );
while ( st.hasMoreElements() )
{
String dir = (String) st.nextElement();
String filename = dir + File.separatorChar + name + ".class";
File file = new File( filename );
if ( file.exists() )
{
String urlDir = dir.replace( File.separatorChar, '/' );
try
{
return new URL( "file:" + urlDir + "/" );
}
catch ( MalformedURLException e )
{
return null;
}
}
}
return null;
}
public String getParameter( String name )
{
// Return a parameter via the munged names in the properties list.
return System.getProperty( PARAM_PROP_PREFIX + name.toLowerCase() );
}
public void appletResize( int width, int height )
{
// Change the frame's size by the same amount that the applet's
// size is changing.
Dimension frameSize = size();
// Dimension frameSize = getSize();
frameSize.width += width - appletSize.width;
frameSize.height += height - appletSize.height;
resize( frameSize );
// setSize( frameSize );
appletSize = applet.size();
// appletSize = applet.getSize();
}
public AppletContext getAppletContext()
{
return this;
}
// Methods from AppletContext.
public AudioClip getAudioClip( URL url )
{
// This is an internal undocumented routine. However, it
// also provides needed functionality not otherwise available.
// I suspect that in a future release, JavaSoft will add an
// audio content handler which encapsulates this, and then
// we can just do a getContent just like for images.
return new sun.applet.AppletAudioClip( url );
}
public Image getImage( URL url )
{
Toolkit tk = Toolkit.getDefaultToolkit();
try
{
ImageProducer prod = (ImageProducer) url.getContent();
return tk.createImage( prod );
}
catch ( IOException e )
{
return null;
}
}
public Applet getApplet( String name )
{
// Returns this Applet or nothing.
if ( name.equals( this.name ) )
return applet;
return null;
}
public Enumeration getApplets()
{
// Just yields this applet.
Vector v = new Vector();
v.addElement( applet );
return v.elements();
}
/// This would be a really short method if it didn't have to catch six exceptions! Still, it *is* kinda black magic, soooo...
/// This was supposed to be more general than it turned out to be. Ideally, the incoming URL could encode all of the information
/// to construct an arbitrary Event. As of this writing, though, the URL had better be of the form:
/// ActionEvent:ID=ACTION_PERFORMED&Command=String
/// where the ID String is the NAME of the Event ID (since right now we only handle ActionEvents, this must be "ACTION_PERFORMED")
/// and the Command String can be anything, which could serve as a mechanism for determining which URL fired the event.
/// The first thing the method does, of course, is to break the URL down into eventClassName, id, and command strings.
/// Then there's a try block that relies heavily on Java 1.1's reflection API to actually generate an ActionEvent.
/// First, we call Class.forName on the fully-qualified event class name. A more clever version of this would catch the exception
/// that might be thrown and try again, looking in "com.sun.java.swing.event.*" instead.
/// Next we get the integer value of the id name from the resulting event class by asking for the field by name, then asking for
/// its integer value.
/// Finally and most complicatedly, we ask the event class for the constructor that takes an Object, an Integer, and a String as
/// arguments, then ask for a new instance of the event class, passing ourself as the source, eventID as the integer, and command
/// as the String. If I've been careful and the person who wrote the URL was careful, we get an ActionEvent out of this.
/// If either I or the URL author aren't careful, we can get one of six exceptions, and catching them constitutes the bulk of the
/// code below.
/// Finally, the bit that makes this non-general, the most important part! I iterate across my vector of listeners and invoke
/// actionPerformed() on them, but doing this means I need to be passing in an ActionEvent, which dictates how I declare eventObject
/// below and how I cast the results of newInstance() also. I suppose the general solution would be to parse the "Event" away from
/// the event class name, add "Listener", and work from there to determine what method to invoke on the contents of the vector (and,
/// for that matter, what vector to use)! That's all left as an exercise for the reader.
public void showDocument( URL url )
{
String urlString = url.toString();
String eventClassName = urlString.substring(0, urlString.indexOf(':'));
String id = urlString.substring(urlString.indexOf("ID=") + 3, urlString.indexOf('&'));
String command = urlString.substring(urlString.indexOf("Command=") + 8, urlString.indexOf('&'));
Class eventClass;
int eventID;
ActionEvent eventObject;
try
{
eventClass = Class.forName("java.awt.event." + eventClassName);
eventID = eventClass.getField(id).getInt(eventClass);
eventObject = (ActionEvent)eventClass.getConstructor(new Class[]{Object.class, Integer.class, String.class}).newInstance(new Object[] {this, new Integer(eventID), command});
}
catch (ClassNotFoundException cnfEx)
{
System.err.println("Failed to find the class " + eventClassName);
return;
}
catch (IllegalAccessException iaEx)
{
System.err.println("Tried to access field \"" + id + "\" as an integer, which didn't work for some reason.");
return;
}
catch (NoSuchFieldException nsfEx)
{
System.err.println("Tried to access field \"" + id + ",\" but it doesn't exist!");
return;
}
catch (InstantiationException iEx)
{
System.err.println("Tried to instantiate class \"" + eventClassName + ",\" but couldn't for some reason.");
return;
}
catch (InvocationTargetException itEx)
{
System.err.println("Tried to instantiate class \"" + eventClassName + ",\" but Paul the Boneheaded got the arguments to newInstance() wrong!");
return;
}
catch (NoSuchMethodException nsmEx)
{
System.err.println("Tried to get the constructor for class \"" + eventClassName + ",\" but Paul the Boneheaded got the arguments to getConstructor() wrong!");
return;
}
Enumeration index = listeners.elements();
while (index.hasMoreElements())
{
((ActionListener)index.nextElement()).actionPerformed(eventObject);
}
}
public void showDocument( URL url, String target )
{
// Ignore.
}
public void showStatus( String status )
{
/*
if ( label != null )
label.setText( status );
*/
}
/// We also need property getters and setters to be a JavaBean. In our case, this means two things:
/// 1) For getters, call getParameter() on the property name and typecast the String to the appropriate type, handling exceptions
/// 2) For setters, call parseArgs() on a String array that contains property name "=" value-cast-to-String
public URL getMovie()
{
try
{
return new URL(getParameter("MOVIE"));
}
catch (MalformedURLException ex)
{
return null;
}
}
public void setMovie(URL value)
{
Properties props = System.getProperties();
parseArgs(new String[]{"MOVIE=" + value.toString()}, props);
}
public String getQuality()
{
String result = getParameter("QUALITY");
return result;
}
public void setQuality(String value)
{
Properties props = System.getProperties();
parseArgs(new String[]{"QUALITY=" + value}, props);
}
public String getScale()
{
String result = getParameter("SCALE");
return result;
}
public void setScale(String value)
{
Properties props = System.getProperties();
parseArgs(new String[]{"SCALE=" + value}, props);
}
public String getSAlign()
{
String result = getParameter("SALIGN");
return result;
}
public void setSAlign(String value)
{
Properties props = System.getProperties();
parseArgs(new String[]{"SALIGN=" + value}, props);
}
public boolean getLoop()
{
boolean result = getParameter("LOOP").equalsIgnoreCase("TRUE");
return result;
}
public void setLoop(boolean value)
{
Properties props = System.getProperties();
parseArgs(new String[]{"LOOP=" + (value ? "TRUE" : "FALSE")}, props);
}
public boolean getPlay()
{
boolean result = getParameter("PLAY").equalsIgnoreCase("TRUE");
return result;
}
public void setPlay(boolean value)
{
Properties props = System.getProperties();
parseArgs(new String[]{"PLAY=" + (value ? "TRUE" : "FALSE")}, props);
}
public boolean getForceRGB()
{
boolean result = getParameter("FORCERGB").equalsIgnoreCase("TRUE");
return result;
}
public void setForceRGB(boolean value)
{
Properties props = System.getProperties();
parseArgs(new String[]{"FORCERGB=" + (value ? "TRUE" : "FALSE")}, props);
}
/// We also want to be a source of ActionEvents. The following two methods just wrap a vector.
public void addActionListener(ActionListener l)
{
listeners.addElement(l);
}
public void removeActionListener(ActionListener l)
{
listeners.removeElement(l);
}
/// The next two methods are so layout managers don't Cronenberg:
public Dimension getPreferredSize()
{
return new Dimension(Integer.parseInt(getParameter("WIDTH")), Integer.parseInt(getParameter("HEIGHT")));
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
/// Finally, we want to be serializable, but in order to be, we need to stop.
private void writeObject(java.io.ObjectOutputStream out) throws IOException
{
stop();
out.defaultWriteObject();
}
}