/* * @(#)TVApplet.java 1.3 01/03/13 * * Copyright (c) 1997-2001 Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.lang.String; import java.net.URL; import java.net.MalformedURLException; import java.io.IOException; import java.util.Properties; import javax.media.*; import com.sun.media.*; /** * This is a Java Applet that demonstrates how to create a simple * media player with a media event listener. It will play the * media clip right away and continuously loop. * * */ public class TVApplet extends Applet implements ControllerListener { // media Player Player player = null; // component in which video is playing Component visualComponent = null; // controls gain, position, start, stop Component controlComponent = null; // displays progress during download Component progressBar = null; long CachingSize = 0L; Panel panel = null; Panel vPanel = null; int controlPanelHeight = 0; Image [] showmeImage = null; Image [] zoomImageUp = null; Image [] zoomImageDn = null; CPanel cPanel = null; ZoomButton zoomButton; final int [] VLEFT = {59, 120}; final int [] VTOP = {33, 67}; final int [] VRIGHT = {59 + 175, 120 + 351}; final int [] VBOTTOM = {173, 67 + 287 + 11}; final int [] WIDTH = {176, 352}; final int [] HEIGHT = {138, 288}; final int HALF = 0; final int FULL = 1; int tSize = 0; /** * Read the applet file parameter and create the media * player. */ public void init() { setLayout(null); setBackground(Color.white); cPanel = new CPanel( ); add(cPanel); // Figure out what size to use // The applet tag takes an optional parameter "SIZE" whose value // can be "half" or "full" // Eg. String szSize = getParameter("SIZE"); if (Toolkit.getDefaultToolkit().getScreenSize().width > 800) tSize = 1; else tSize = 0; if (szSize != null) { if (szSize.toLowerCase().equals("full")) tSize = 1; else if (szSize.toLowerCase().equals("half")) tSize = 0; } cPanel.setBounds(VLEFT[tSize], VTOP[tSize], WIDTH[tSize], HEIGHT[tSize]); // Get the images showmeImage = new Image[FULL + 1]; zoomImageUp = new Image[FULL + 1]; zoomImageDn = new Image[FULL + 1]; showmeImage[HALF] = getImage(getDocumentBase(), "ShowMeS2.jpg"); showmeImage[FULL] = getImage(getDocumentBase(), "ShowMeS.jpg"); zoomImageUp[HALF] = getImage(getDocumentBase(), "InUp.gif"); zoomImageDn[HALF] = getImage(getDocumentBase(), "InDn.gif"); zoomImageUp[FULL] = getImage(getDocumentBase(), "OutUp.gif"); zoomImageDn[FULL] = getImage(getDocumentBase(), "OutDn.gif"); MediaTracker mt = new MediaTracker(this); mt.addImage(showmeImage[HALF], 0); mt.addImage(showmeImage[FULL], 1); mt.addImage(zoomImageUp[HALF], 2); mt.addImage(zoomImageUp[FULL], 3); mt.addImage(zoomImageDn[HALF], 4); mt.addImage(zoomImageDn[FULL], 5); try { mt.waitForAll(); } catch (Exception e) { System.err.println("Could not load ShowMeS.jpg"); showmeImage[HALF] = null; showmeImage[FULL] = null; } addZoomButton(); // input file name from html param String mediaFile = null; // URL for our media file URL url = null; // URL for doc containing applet URL codeBase = getDocumentBase(); // Get the media filename info. // The applet tag should contain the path to the // source media file, relative to the html page. if ((mediaFile = getParameter("FILE")) == null) Fatal("Invalid media file parameter"); try { // Create an url from the file name and the url to the // document containing this applet. if ((url = new URL(codeBase, mediaFile)) == null) Fatal("Can't build URL for " + mediaFile); // Create an instance of a player for this media try { player = Manager.createPlayer(url); } catch (NoPlayerException e) { System.out.println(e); Fatal("Could not create player for " + url); } // Add ourselves as a listener for a player's events player.addControllerListener(this); } catch (MalformedURLException e) { Fatal("Invalid media file URL!"); } catch (IOException e) { Fatal("IO exception creating player for " + url); } } /** * Start media file playback. This function is called the * first time that the Applet runs and every * time the user re-enters the page. */ public void start() { if (player != null) player.realize(); } /** * Stop media file playback and release resource before * leaving the page. */ public void stop() { if (player != null) { player.stop(); player.deallocate(); } } public void destroy() { player.close(); } public void paint(Graphics g) { if (showmeImage[tSize] != null) g.drawImage(showmeImage[tSize], 0, 0, this); super.paint(g); } public synchronized void reSize() { cPanel.setBounds(VLEFT[tSize], VTOP[tSize], WIDTH[tSize], HEIGHT[tSize]); if (visualComponent != null) { Dimension size = visualComponent.getPreferredSize(); int width = size.width; int height = size.height; while (true) { // Scale to fit if (width > WIDTH[tSize] || height > HEIGHT[tSize]) { width /= 2; height /= 2; } else if (width < WIDTH[tSize] && height < HEIGHT[tSize]) { if (width * 2 <= WIDTH[tSize] && height*2 <= HEIGHT[tSize]) { width *= 2; height *= 2; } else break; } else break; } visualComponent.setBounds((WIDTH[tSize] - width) / 2, (HEIGHT[tSize] - height) / 2, width, height); } if (controlComponent != null) { controlComponent.setBounds(VLEFT[tSize], VBOTTOM[tSize], WIDTH[tSize], 24); controlComponent.invalidate(); } remove(zoomButton); addZoomButton(); repaint(); } public void addZoomButton() { zoomButton = new ZoomButton(zoomImageUp[tSize], zoomImageDn[tSize], 1 - tSize); add(zoomButton); zoomButton.setBounds(showmeImage[tSize].getWidth(this) - 24, showmeImage[tSize].getHeight(this) - 24, 24, 24); } /** * This controllerUpdate function must be defined in order to * implement a ControllerListener interface. This * function will be called whenever there is a media event */ public synchronized void controllerUpdate(ControllerEvent event) { // If we're getting messages from a dead player, // just leave if (player == null) return; // When the player is Realized, get the visual // and control components and add them to the Applet if (event instanceof RealizeCompleteEvent) { if (( visualComponent = player.getVisualComponent())!= null) { cPanel.add(visualComponent); } if (( controlComponent = player.getControlPanelComponent()) != null) { add(controlComponent); controlComponent.setBounds(VLEFT[tSize], VBOTTOM[tSize], WIDTH[tSize], 24); controlComponent.invalidate(); controlComponent.repaint(); } reSize(); player.prefetch(); } else if (event instanceof EndOfMediaEvent) { // We've reached the end of the media; rewind and // start over player.setMediaTime(new Time(0)); player.prefetch(); } else if (event instanceof ControllerErrorEvent) { player = null; Fatal(((ControllerErrorEvent)event).getMessage()); } else if (event instanceof PrefetchCompleteEvent) { if (visualComponent != null) { reSize(); } player.start(); } else if (event instanceof SizeChangeEvent) { reSize(); } } void Fatal (String s) { // Applications will make various choices about what // to do here. We print a message and then exit System.err.println("FATAL ERROR: " + s); throw new Error(s); // Invoke the uncaught exception // handler System.exit() is another // choice. } /************************************************************************* * INNER CLASSES *************************************************************************/ public class CPanel extends Panel { public CPanel() { super(); setBackground(Color.black); setLayout( null ); } } public class ZoomButton extends Component { Image up, down; int newSize; boolean mouseUp = true; public ZoomButton(Image up, Image down, int newSize) { this.up = up; this.down = down; this.newSize = newSize; setSize(24, 24); addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent me) { mouseUp = false; repaint(); } public synchronized void mouseReleased(MouseEvent me) { mouseUp = true; TVApplet.this.tSize = ZoomButton.this.newSize; reSize(); } } ); } public void paint(Graphics g) { if (mouseUp) g.drawImage(up, 0, 0, this); else g.drawImage(down, 0, 0, this); } } }