mport java.awt.Graphics; import java.io.File; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.SourceDataLine; public class Musicline extends javax.swing.JFrame implements Runnable { private byte[] audioData=null; private int intBytes = 0; private byte[] ml=new byte[1]; private int[] drawl=null; /** Creates new form Musicline */ public Musicline() { initComponents(); Graphics g; g=this.getGraphics(); } public void paint(Graphics g) { g.clearRect(0,0,900,900); //System.out.print(drawl.length); if(audioData!=null) { drawl=new int[audioData.length]; for(int i=0;i<audioData.length;i++) { ml[0]=audioData; //String s=new String(ml); drawl=Math.abs((int)ml[0]); } System.out.println(drawl[0]); for(int i=0;i<drawl.length-1;i++) { g.drawLine(i*this.getWidth()/256,drawl+100,(i+1)*this.getWidth()/256,drawl[i+1]+100); } } } public void run() { while(intBytes!=-1) { try { synchronized(this) { this.wait(10); } } catch (InterruptedException ex) { ex.printStackTrace(); } repaint(); } } public void play() { try { AudioInputStream ais = AudioSystem.getAudioInputStream(new File( "E:/a.wav"));// 获得音频输入流 ais=AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED,ais); AudioFormat baseFormat = ais.getFormat();// 指定声音流中特定数据安排 System.out.println("baseFormat="+baseFormat); DataLine.Info info = new DataLine.Info(SourceDataLine.class, baseFormat); System.out.println("info="+info); SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); // 从混频器获得源数据行 System.out.println("line="+line); line.open(baseFormat);// 打开具有指定格式的行,这样可使行获得所有所需的系统资源并变得可操作。 line.start();// 允许数据行执行数据 I/O int BUFFER_SIZE = 256; audioData = new byte[BUFFER_SIZE]; while (intBytes != -1) { intBytes = ais.read(audioData, 0, BUFFER_SIZE);// 从音频流读取指定的最大数量的数据字节,并将其放入给定的字节数组中。 if (intBytes >= 0) { int outBytes = line.write(audioData, 0, intBytes);// 通过此源数据行将音频数据写入混频器。 } } } catch (Exception e) { e.printStackTrace(); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc=" 生成的代码 "> private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE)); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { Musicline msl=new Musicline(); msl.setVisible(true); Thread yh=new Thread(msl); yh.start(); msl.play(); } } package com.xiaoer.service; import java.awt.*; import javax.swing.*; public class Func extends JFrame { /** *<br>方法说明:主方法 *<br>输入参数: *<br>返回类型: */ public static void main(String[] args) { Func db = new Func(); db.update(); } /** *<br>方法说明:构造器,显示窗体 *<br>输入参数: *<br>返回类型: */ Func() { super("Function"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setSize(310,310); show(); } /** *<br>方法说明:更新画面 *<br>输入参数: *<br>返回类型: */ public void update() { repaint(); } /** *<br>方法说明:转换坐标点 *<br>输入参数: *<br>返回类型: */ double f(double x) { return (Math.sin(2*x)+Math.cos(3*x)); } /** *<br>方法说明:绘制坐标图 *<br>输入参数: *<br>返回类型: */ public void paint(Graphics g) { double x0,x1,y0,y1,xd,yd; double max=5.0; int w,h,hw,hh; //获取屏幕的尺寸 w=getSize().width; h=getSize().height; hw=w/2; hh=h/2; //在屏幕上输出字符 g.drawString("Sin[2x]+Cos[3x]",10,40); g.setColor(Color.red); g.drawString("0",hw+5,hh+12); g.drawString(""+max,hw-20,40); g.drawString(""+max,w-20,hh+12); //绘制X轴坐标 g.drawLine(0,hh,w,hh); //绘制Y轴坐标 g.drawLine(hw,0,hw,h); xd=2*max/w;//计算X增量 yd=hh/max; //计算y增量 g.setColor(Color.blue); //使用增量绘制一小段线,最终组成曲线图形。 for (int x=0 ; x<w-1; x++) { x0=-max+x*xd; y0=f(x0)*yd; x1=x0+xd; y1=f(x1)*yd; g.drawLine(x,(int)(hh-y0),x+1,(int)(hh-y1)); } } }