【发布时间】:2014-10-10 16:42:27
【问题描述】:
我想通过读取ubuntu中的/proc/net/dev文件来绘制图表,以显示wlan、eth和lo中接收到的字节和发送的字节。目前,这些详细信息以文本格式显示并实时更新。所以我想通过图表展示这些实时更新。此刻,我将我的文本显示为如下代码所示。
package throughput;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class Throughput extends JPanel implements Runnable {
private ArrayList<String> lines;
public Throughput() {
lines = new ArrayList<String>();
new Thread(this).start();
}
@Override
public void run() {
try {
while (true) {
String sContents = readFile();
StringTokenizer tok = new StringTokenizer(sContents, "\n");
lines = new ArrayList<String>();
int x = 0;
while (tok.hasMoreTokens()) {
String line = tok.nextToken();
if (x++ < 2) {//skip headers
continue;
}
lines.add(line);
}
repaint();
Thread.sleep(500);//half a second
}
} catch (Exception exc) {
exc.printStackTrace();
}
}
@Override
public void paint(Graphics gr) {
super.paint(gr);
Graphics2D g = (Graphics2D) gr;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setFont(new Font(Font.DIALOG_INPUT, Font.PLAIN, 14));
g.drawString("Interface: receivedBytes/receivedPackets, sentBytes/sentPackets", 10, 20);
for (int i = 0; i < lines.size(); i++) {
drawLine(g, lines.get(i), i);
}
}
private static final String readFile() {
try {
File file = new File("/proc/net/dev");
StringBuilder sb = new StringBuilder();
byte[] bytes = new byte[1024];
int read = 0;
InputStream in = new FileInputStream(file);
while ((read = in.read(bytes)) != -1) {
sb.append(new String(bytes, 0, read));
}
in.close();
return sb.toString();
} catch (Exception exc) {
exc.printStackTrace();
}
return null;
}
private void drawLine(Graphics2D g, String line, int iLineIndex) {
String[] words = line.split("\\s+");
String sInterface = words[words[0].length() > 0 ? 0 : 1];
int index = sInterface.indexOf(':');
boolean jump = index != sInterface.length()-1;
String receivedBytes = jump ? sInterface.substring(index+1) : words[2];
String receivedPackets = jump ? words[2] : words[3];
String sentBytes = jump ? words[9] : words[10];
String sentPackets = jump ? words[10] : words[11];
if(index > -1) {
sInterface = sInterface.substring(0, index);
}
int x = 10, y = 50 + iLineIndex * 20;
String sReceived = receivedBytes + "/" + receivedPackets;
String sSent = sentBytes + "/" + sentPackets;
g.drawString(sInterface + ":", x, y);
g.drawString(sReceived, x+100, y);
g.drawString(sSent, x+250, y);
}
}
package throughput;
import javax.swing.*;
public class Main extends JFrame{
public static void main(String[] args) {
new Main();
}
public Main() {
setSize(550, 200);
setTitle("Network Monitor");
setContentPane(new Throughput());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
【问题讨论】:
-
有什么问题?你被困在哪里了?
-
我想通过读取ubuntu中的/proc/net/dev文件来绘制图表,以显示wlan、eth和lo中接收到的字节和发送的字节。目前,这些详细信息仅以文本格式显示并实时更新。这就是上面的代码。上面的代码不是问题。我想用上面的输出画一个实时图表。请帮忙!
-
所以您希望有人为您编写代码?这不是 SO 的工作方式。您需要展示您的尝试,然后在遇到困难时提出具体问题。
-
是的,我自己写了上面的代码。我想将其转换为以图形格式显示。我真的不知道如何完成它。我读到了 jfreechart 之类的东西。但不知道用它来绘制图表。如果你熟悉这个,请帮忙,因为我的作业需要这个。
-
谢谢。你真的很有帮助。
标签: java ubuntu graph draw jfreechart