Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

DataLine Info GUI Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!

Code:

 
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.sound.sampled.*;


public class DataLineInfoGUI extends JPanel {

    PCMFilePlayer player;
    JButton startButton;

    public DataLineInfoGUI (File f) {
        super();
        try {
            player = new PCMFilePlayer (f);
        } catch (Exception ioe) {
            add (new JLabel ("Error: " +
                             ioe.getMessage()));
            return;
        }
        DataLine line = player.getLine();
        // layout
        // line 1: name
        setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
        add (new JLabel ("File:  " +
                         player.getFile().getName()));
        // line 2: levels
        add (new DataLineLevelMeter (line));
        // line 3: format info as textarea
        AudioFormat format = line.getFormat();
        JTextArea ta = new JTextArea();
        ta.setBorder (new TitledBorder ("Format"));
        ta.append ("Encoding: " +
                   format.getEncoding().toString() + "\n");
        ta.append ("Bits/sample: " +
                   format.getSampleSizeInBits() + "\n");
        ta.append ("Channels: " +
                   format.getChannels() + "\n");
        ta.append ("Endianness: " +
                   (format.isBigEndian() ? " big " : "little") + "\n");
        ta.append ("Frame size: " +
                   format.getFrameSize() + "\n");
        ta.append ("Frame rate: " +
                   format.getFrameRate() + "\n");
        add (ta);

        // now start playing
        player.start();
    }

    public static void main (String[] args) {
        JFileChooser chooser = new JFileChooser();
        chooser.sho*****enDialog(null);
        File file = chooser.getSelectedFile();
        DataLineInfoGUI demo =
            new DataLineInfoGUI (file);

        JFrame f = new JFrame ("JavaSound info");
        f.getContentPane().add (demo);
        f.pack();
        f.setVisible(true);
    }


    class DataLineLevelMeter extends JPanel {
        DataLine line;
        float level = 0.0f;
        public DataLineLevelMeter (DataLine l) {
            line = l;
            Timer timer =
                new Timer (50,
                           new ActionListener (){
                               public void actionPerformed (ActionEvent e) {
                                   level = line.getLevel();
                                   repaint();
                               }
                           });
            timer.start();
        }
        public void paint (Graphics g) {
            Dimension d = getSize();
            g.setColor (Color.green);
            int meterWidth = (int) (level * (float) d.width);
            g.fillRect (0, 0, meterWidth, d.height);
        }

    }

}

/*   Swing Hacks
 *   Tips and Tools for Killer GUIs
 * By Joshua Marinacci, Chris Adamson
 *   First Edition June 2005
 *   Series: Hacks
 *   ISBN: 0-596-00907-0
 *   http://www.oreilly.com/catalog/swinghks/
 */

 
 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy