Download MediaDemo.java source file |
/* * @(#)MediaDemo.java 1.2 12/0/2009 */ package com.wpollock.mediademo; import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.URL; /** * MediaDemo shows how to show images and play simple audio from * a standalone application (as opposed to an applet). Applets * have many builtin media features; applications are harder. * * The Java Media Framework ("JMF") includes many classes for * media, including MIDI, recording and editing sound, mixing, etc. * Here we use the simple AudioClip class (as of 1.3 this can be used * in standalone applications too). * * @version 1.2 * @author Wayne Pollock */ public class MediaDemo extends Frame { private Toolkit tk = Toolkit.getDefaultToolkit(); private Image pic; private AudioClip snd; private int picWidth = 250, picHeight = 400; // defaults /* * @throws Exception * Exceptions in a top-level class should be caught and handled. * They are ignored here for the sake of clarity in demo programs. * @param args The command line arguments passed from the OS. */ public static void main ( String [] args ) throws Exception { // Creating images in swing is 3 easy steps: // URL url = getClass().getResource( "image.gif" ); // JLabel pic = new JLabel( new ImageIcon( url ) ); // getContentPane().add( pic ); // But it is harder in AWT, so that is shown below: MediaDemo frame = new MediaDemo(); // Window closing event handler: frame.addWindowListener( new WindowAdapter() { public void windowClosing ( WindowEvent we ) { System.exit( 0 ); } } ); frame.centerWin(); frame.setBackground( Color.cyan ); frame.add ( new Panel().add( new Label( "Mona Lisa", Label.CENTER ) ), "South" ); frame.setVisible( true ); } public MediaDemo () { setTitle( "AWT Media Demo" ); // Media are loaded from URLs, which are constructed // relative to the location of this class file: URL sndUrl = getClass().getResource( "Empire.au" ); snd = Applet.newAudioClip( sndUrl ); URL picUrl = getClass().getResource( "Mona.gif" ); pic = tk.getImage( picUrl ); MediaTracker mt = new MediaTracker( this ); mt.addImage( pic, 0 ); try { mt.waitForAll(); } catch ( InterruptedException ie ) {} if ( mt.isErrorAny() ) pic = null; else { // Image is fully loaded when we get to here. picWidth = pic.getWidth( this ); picHeight = pic.getHeight( this ); } Insets insets = getInsets(); setSize( picWidth + insets.left + insets.right + 50, picHeight + insets.top + insets.bottom + 100 ); } public void paint ( Graphics g ) { snd.play(); Insets insets = getInsets(); // draw backgroud image: if ( pic == null ) { g.drawString( "Error loading Image!", insets.left + 10, insets.top + 25 ); } else { g.setColor( Color.red ); g.fillRect( insets.left + 10, insets.top + 10, picWidth+20, picHeight+20 ); g.drawImage( pic, insets.left + 20, insets.top + 20, this ); } super.paint( g ); // Now draw components (on top of image). } /** This method centers a Frame on the screen. */ private void centerWin () { // Center the Frame on the screen: int screenWidth = tk.getScreenSize().width; int screenHeight = tk.getScreenSize().height; int frameWidth = getSize().width; int frameHeight = getSize().height; setLocation( ( screenWidth - frameWidth ) / 2, ( screenHeight - frameHeight ) / 2 ); } } |
Main-Class: com.wpollock.mediademo.MediaDemo |
C:\Temp>mkdir com\wpollock\mediademo C:\Temp>notepad com\wpollock\mediademo\MediaDemo.java C:\Temp>notepad MediaDemo.mf C:\Temp>javac com\wpollock\mediademo\MediaDemo.java C:\Temp>copy Mona.gif com\wpollock\mediademo C:\Temp>copy Empire.au com\wpollock\mediademo C:\Temp>jar -cvfe MediaDemo.jar com.wpollock.mediademo.MediaDemo com OR you can do this: jar -cvfm MediaDemo.jar MediaDemo.mf com added manifest adding: com/(in = 0) (out= 0)(stored 0%) adding: com/wpollock/(in = 0) (out= 0)(stored 0%) adding: com/wpollock/mediademo/(in = 0) (out= 0)(stored 0%) adding: com/wpollock/mediademo/Empire.au(in = 310062) (out= 280711)(deflated 9%) adding: com/wpollock/mediademo/Mona.gif(in = 72909) (out= 72435)(deflated 0%) adding: com/wpollock/mediademo/MediaDemo$1.class(in = 406) (out= 294)(deflated 27%) adding: com/wpollock/mediademo/MediaDemo.class(in = 3106) (out= 1752)(deflated 43%) C:\Temp>jar -i MediaDemo.jar this step is not necessary! C:\Temp>jar -tvf MediaDemo.jar 78 Mon Jul 22 23:08:22 EDT 2002 META-INF/INDEX.LIST 0 Mon Jul 22 23:08:12 EDT 2002 META-INF/ 119 Mon Jul 22 23:08:12 EDT 2002 META-INF/MANIFEST.MF 0 Fri Aug 03 14:48:24 EDT 2001 com/ 0 Fri Aug 03 15:09:12 EDT 2001 com/wpollock/ 0 Mon Jul 22 23:01:08 EDT 2002 com/wpollock/mediademo/ 310062 Mon Apr 02 19:05:40 EDT 2001 com/wpollock/mediademo/Empire.au 72909 Tue May 26 23:17:20 EDT 1998 com/wpollock/mediademo/Mona.gif 406 Mon Jul 22 23:04:06 EDT 2002 com/wpollock/mediademo/MediaDemo$1.class 3115 Mon Jul 22 23:04:06 EDT 2002 com/wpollock/mediademo/MediaDemo.class C:\Temp>java -jar MediaDemo.jar C:\Temp> (You could also double-click MediaDemo.jar, from the GUI) |