日期:2014-05-20 浏览次数:20860 次
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<Integer> values; // 保存接收到的数据的容器.
private static final int MAX_VALUE = 200; // 接收到的数据的最大值.
private static final int MAX_COUNT_OF_VALUES = 50; // 最多保存数据的个数.
public DataReceiver() {
values = Collections.synchronizedList(new ArrayList<Integer>());
// 使用一个线程模拟产生数据.
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 (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();