java绘制坐标和波形图_java绘制波形图

这篇博客展示了如何使用Java Swing组件创建一个动态波形图。通过DataReceiver类,程序模拟接收数据并实时更新画布,用Graphics2D进行抗锯齿设置,绘制线段来展示波形变化。代码中设置了数据的最大值、最大显示数量,并通过normalizeValueForYAxis方法将接收到的值映射到画布的高度范围内。
摘要由CSDN通过智能技术生成

package com.tur.demo;

import javax.swing.*;

import java.awt.*;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Random;

class DataReceiver extends JPanel {

private List values; // 保存接收到的数据的容器.

private static final int MAX_VALUE = 200; // 接收到的数据的最大值.

private static final int MAX_COUNT_OF_VALUES = 50; // 最多保存数据的个数.

public DataReceiver() {

values = Collections.synchronizedList(new ArrayList());

// 使用一个线程模拟产生数据.

new Thread(new Runnable() {

@Override

public void run() {

Random rand = new Random();

try {

while (true) {

addValue(rand.nextInt(MAX_VALUE)); // 产生一个数据,并模拟接收并放到容器里.

repaint();

Thread.sleep(100);

}

} catch (Interru

使用jdk16编译 import java.awt.Graphics; import java.awt.GridLayout; 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; import javax.swing.GroupLayout; import javax.swing.JFrame; /** * */ /** * @author Administrator * */ public class Musicline extends 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[i]; // String s=new String(ml); drawl[i] = 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[i] + 100, (i + 1) * this.getWidth() / 256, drawl[i + 1] + 100); } } } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ public void run() { // TODO Auto-generated method stub 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( "F:/perl/key2.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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值