Download this source file
Download Mona.gif
Download spacemusic.au
// <APPLET CODE="Smile2" WIDTH="320" HEIGHT="430"> // <PARAM NAME="IMAGE" VALUE="Mona.gif"> // <PARAM NAME="SOUND" VALUE="spacemusic.au"> // </APPLET> // Smile2.java - A Java applet that displays a GIF graphic and plays music. // Note: A non-applet can get an image easily by using: // Image pic = Toolkit.getDefaultToolkit().getImage( "Mona.gif" ); // but this usually won't work for applets. (Also, applets have // their own getImage method.) One way to deal with images in applets // is by using a MediaTacker object, shown here. The only problem is // to say where to find the graphic. If it resides on the server, // in the directory where the .class file resides, you can load the // image as: // Image pic = getImage( getCodeBase(), imageName ); // (or you can use "getImage( getCodeBase(), "../images/" + imageName )" if // the image is in a relative directory. Note this doesn't work if the graphic // (or other media) is in a jar file! The method used here works with or // without jar files. (See "SmileJar.java" for a demo of jar files.) // // Final note: Applets that continuously play sounds are annoying! You // should always provide some way for the user to turn off the sound loop. // A Checkbox menuItem or a small button (shown here) does the trick! // // Written 2000 by Wayne Pollock, Tampa FL USA. import java.awt.*; import java.awt.event.*; import java.applet.*; public class Smile2 extends Applet implements ActionListener { private String imageName, sndName; private Image pic = null; private int picWidth, picHeight; private AudioClip snd = null; private Button btn; // Sound on/off button private boolean isPlaying = true; public void init () { imageName = getParameter( "IMAGE" ); sndName = getParameter( "SOUND" ); if ( imageName != null ) { pic = getImage( getClass().getResource( imageName ) ); showStatus( "Loading Media Files..." ); // Display msg in status bar. MediaTracker mt = new MediaTracker( this ); mt.addImage( pic, 0 ); try { mt.waitForAll(); } catch ( InterruptedException ie ) {} if ( mt.isErrorAny() ) pic = null; // Image is fully loaded when we get to here: picWidth = pic.getWidth( this ); picHeight = pic.getHeight( this ); } if ( sndName != null ) // Note: MediaTrackers only work on images. snd = getAudioClip( getClass().getResource( sndName ) ); setBackground( Color.CYAN ); setLayout( new BorderLayout() ); btn = new Button( "Turn Sound Off" ); btn.addActionListener( this ); Panel p = new Panel(); p.add( btn ); add( p, "South" ); } public void start () { if ( snd != null && isPlaying ) snd.loop(); } public void stop () { if ( snd != null ) snd.stop(); } public void actionPerformed ( ActionEvent e ) { if ( snd == null ) return; if ( isPlaying ) { snd.stop(); btn.setLabel( "Turn Sound On" ); } else { snd.loop(); btn.setLabel( "Turn Sound Off" ); } isPlaying = !isPlaying; this.requestFocus(); // remove focus from button, which I find ugly. } public void paint ( Graphics g ) { if ( pic == null ) g.drawString( "Error loading Image!", 10, 10 ); else { g.setColor( Color.red ); g.fillRect( 7, 7, picWidth+6, picHeight+6 ); g.drawImage( pic, 10, 10, this ); } } }
Send comments and questions to
pollock@acm.org. |