Source of MediaTest.java


  1: // Fig. 21.7: MediaTest.java
  2: // A simple media player
  3: import java.io.File;
  4: import java.net.MalformedURLException;
  5: import java.net.URL;
  6: import javax.swing.JFileChooser;
  7: import javax.swing.JFrame;
  8: 
  9: public class MediaTest
 10: {
 11:    // launch the application
 12:    public static void main( String args[] )
 13:    {
 14:       // create a file chooser
 15:       JFileChooser fileChooser = new JFileChooser();
 16:       
 17:       // show open file dialog
 18:       int result = fileChooser.showOpenDialog( null );
 19: 
 20:       if ( result == JFileChooser.APPROVE_OPTION ) // user chose a file
 21:       {
 22:          URL mediaURL = null;
 23:          
 24:          try
 25:          {
 26:             // get the file as URL
 27:             mediaURL = fileChooser.getSelectedFile().toURL();
 28:          } // end try
 29:          catch ( MalformedURLException malformedURLException )
 30:          {
 31:             System.err.println( "Could not create URL for the file" );
 32:          } // end catch
 33: 
 34:          if ( mediaURL != null ) // only display if there is a valid URL
 35:          {
 36:             JFrame mediaTest = new JFrame( "Media Tester" );
 37:             mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
 38:             
 39:             MediaPanel mediaPanel = new MediaPanel( mediaURL );
 40:             mediaTest.add( mediaPanel );
 41:             
 42:             mediaTest.setSize( 300, 300 );
 43:             mediaTest.setVisible( true );
 44:          } // end inner if
 45:       } // end outer if
 46:    } // end main
 47: } // end class MediaTest