📄 javamedia.java
字号:
/* * @(#)JavaMedia.java 1.21 99/08/16 * * Copyright (c) 1998, 1999 by 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. */package demos.Mix;import java.applet.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.border.*;import java.awt.image.BufferedImage;import java.net.URL;import java.util.Hashtable;import java.util.Enumeration;import CustomControls;import Java2DemoApplet;import DemoImages;/* UNCOMMENT TO USE JMFimport javax.media.*;import com.sun.media.*;END UNCOMMENT*//** JavaMedia example to show effects of sound and jmf with 2D animation. Java Media Framework 1.02 extension or later required. http://java.sun.com/products/java-media/jmf/index.html Running requires editing this file to comment out some source and uncomment the JMF specific source. Look for the UNCOMMENT TO USE JMF text. Look for the REMOVE TO USE JMF text For media use the Sample1.mov & Sample2.mpg in the JMF/samples/media directory and then copy the two sample files to the Java2D/media directory. Setup to use JMF. After editing this file : Extract the contents of Java2Demo.jar, run this command from the Java2D directory : jar xvf Java2Demo.jar For win32 : javac src\demos\Mix\JavaMedia.java -d . For solaris : javac src/demos/Mix/JavaMedia.java -d . Run without the Java2Demo.jar JarFile context : java demos.Mix.JavaMedia java DemoGroup Mix java Java2Demo */public class JavaMedia extends JPanel implements ChangeListener, CustomControls, Runnable { ImageIcon stopIcon, startIcon, loopIcon, loopingIcon; String playType[] = {"mix1", "mix2", "rmf"}; String playList[][] = { {"sfx-medley.rmf", "trippygaia1.mid", "spacemusic.au"}, {"bong.wav", "bark.aiff", "trance.rmf"}, {"ambient.rmf", "classical.rmf", "modern-rock.rmf"}}; JavaSound jsound[] = new JavaSound[9]; private Font f1 = new Font("serif", Font.BOLD + Font.ITALIC, 18); private Hashtable jmftable = new Hashtable(2); private Thread thread; private JTabbedPane tabbedPane; public JavaMedia() { setLayout(new BorderLayout()); setBackground(Color.gray); stopIcon = new ImageIcon(DemoImages.getImage("stop2.gif",this)); startIcon = new ImageIcon(DemoImages.getImage("start2.gif",this)); loopIcon = new ImageIcon(DemoImages.getImage("loop.gif",this)); loopingIcon = new ImageIcon(DemoImages.getImage("looping.gif",this)); tabbedPane = new JTabbedPane(); tabbedPane.setBorder(new BevelBorder(BevelBorder.RAISED)); tabbedPane.setFont(new Font("Times New Roman", Font.PLAIN, 10)); Color c[] = {Color.cyan, Color.yellow, Color.pink}; for (int i = 0, k = 0; i < playType.length; i++) { JPanel jp = new JPanel(new GridLayout(0,1)); for (int j = 0; j < playList[i].length; j++) { jp.add(jsound[k++] = new JavaSound(playList[i][j],c[j%3])); } tabbedPane.addTab(playType[i], jp); } JMF jmf = new JMF("Sample1.mov"); jmftable.put("mov", jmf); tabbedPane.addTab("mov", jmf); jmftable.put("mpg", jmf = new JMF("Sample2.mpg")); tabbedPane.addTab("mpg", jmf); tabbedPane.addChangeListener(this); add(tabbedPane); JPanel jp1 = new JPanel(new GridLayout(0,1)); String s1[] = { "J", "A", "V", "A" }; for (int i = 0; i < s1.length; i++) { jp1.add(addLabel(s1[i])); } add("West", jp1); JPanel jp2 = new JPanel(new GridLayout(0,1)); String s2[] = { "M", "E", "D", "I", "A" }; for (int i = 0; i < s2.length; i++) { jp2.add(addLabel(s2[i])); } add("East", jp2); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (thread == null) start(); else stop(); } }); } public String[] getCustomControlsConstraints() { return new String[] { BorderLayout.NORTH }; } public Component[] getCustomControls() { // do nothing panel JPanel p = new JPanel(); p.setVisible(false); return new Component[] { (Component) p }; } public void customControlsThread(int state) { if (state == CustomControls.START) { start(); } else if (state == CustomControls.STOP) { stop(); } } public void start() { if (thread != null) { return; } thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("Mix.JavaMedia Thread"); thread.start(); } public synchronized void stop() { if (thread != null) { thread.interrupt(); } thread = null; } public void run() { Thread me = Thread.currentThread(); try { thread.sleep(333); } catch (Exception e) { return; } while (thread == me) { for (int i = 0; i < tabbedPane.getTabCount() && thread == me; i++) { Object obj = tabbedPane.getComponent(i); tabbedPane.setSelectedIndex(i); if (obj instanceof Panel) {/* UNCOMMENT TO USE JMF JMF jmf = (JMF) jmftable.get(tabbedPane.getTitleAt(i)); while (!jmf.rewoundToggle) { try { thread.sleep(250); } catch (InterruptedException e) { break; } } jmf.stop(); jmf.rewoundToggle = false;END UNCOMMENT*/ } else if (obj instanceof JPanel) { JPanel p = (JPanel) obj; for (int j = 0; j < p.getComponentCount() && thread == me; j++) { JavaSound js = (JavaSound) p.getComponent(j); js.loopB.doClick(); try { thread.sleep(7777); } catch (InterruptedException e) { } js.startStopB.doClick(); } } } } thread = null; } public JLabel addLabel(String s) { JLabel l = new JLabel(" " + s + " "); l.setForeground(Color.gray); l.setFont(f1); return l; } public void stateChanged(ChangeEvent e) {/* UNCOMMENT TO USE JMF for (Enumeration enum = jmftable.elements(); enum.hasMoreElements();) { ((JMF) enum.nextElement()).stop(); } JTabbedPane tabbedPane = (JTabbedPane) e.getSource(); int index = tabbedPane.getSelectedIndex(); String title = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex()); if (title == "mov" || title == "mpg") { stopJavaSound(); ((JMF) jmftable.get(title)).start(); } END UNCOMMENT */ } public void stopJavaSound() { for (int i = 0; i < jsound.length; i++) { if (jsound[i].audioState != jsound[i].STOP ) { jsound[i].stopAudio(); } } } public URL getURL(String dir, String name) { URL url = null; if (Java2DemoApplet.applet != null) { try { url = new URL(Java2DemoApplet.applet.getCodeBase(), dir + name); } catch (Exception ex) { ex.printStackTrace(); } } else { try { dir = System.getProperty("user.dir") + "/" + dir; url = new URL("file:" + dir + name); } catch (Exception ex) { ex.printStackTrace(); } } return url; } public static void main(String args[]) { JFrame f = new JFrame("Java2D Demo - JavaMedia"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -