java 窗体加入折线图,使用JFreeChart< java>将动态折线图添加到现有JFrame中的JPanel中....

I made an application that would show me Max, Minimum, and Average ping when I click "start" in my JFrame application. I made a white box so I could fit a dynamically changing line graph that will be in the same window as my stats, and will update at the same rate as my stats (1 second).

No matter how much I google, everybody seems to understand the JFreeChart sample code. I do not understand how to implement that at all. Other tutorials show just the graph as a standalone in its own window.

I hope somebody can help me. Here is my code for the JFrame as of now:

private void startButtonMouseClicked(java.awt.event.MouseEvent evt) {

String host = hostName.getText();

ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();

exec.scheduleAtFixedRate(new Runnable(){

@Override

public void run(){

try {

NetworkAnalyzer.pingCheck(host);

} catch (IOException ex) {

Logger.getLogger(NetworkAnalyzerwindow.class.getName()).log(Level.SEVERE, null, ex);

}

}

}, 0, 1, TimeUnit.SECONDS);

And here is my pingCheck:

public class NetworkAnalyzer {

static int count=0;

static long max_time = 0;

static long min_time = 0;

static long avg_time = 0;

public static void pingCheck(String host) throws IOException{

String time = "";

//command to execute

String pingCmd = "ping " + host + " -t " + "-n 1";

//gets runtime to execute command

Runtime runtime = Runtime.getRuntime();

try {

Process process = runtime.exec(pingCmd);

//gets inputstream to read the output of the cocmmand

BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));

//read outputs

String inputLine = in.readLine();

while((inputLine != null)) {

if (inputLine.length() > 0 && inputLine.contains("time")){

time = inputLine.substring(inputLine.indexOf("time"));

break;

}

inputLine = in.readLine();

}

/* if(inputLine.contains("timed")){

System.out.print("Request timed out. Try another domain:");

pingCheck(time);

}*/

time = time.replaceAll("[^0-9]+", " ");

time = time.substring(0, Math.min(time.length(), 3));

time = time.replaceAll(" ", "");

System.out.println("ping:" + time);

//ping calcs

count++;

Long ping;

ping = Long.parseLong(time);

//for avg

//max ping

if( count == 1 || ping >= max_time)

max_time = ping;

//min ping

if(count == 1 || ping <= min_time)

min_time = ping;

//avg ping

if (count > 0)

avg_time = (avg_time*(count-1)+ping)/count;

NetworkAnalyzerwindow.maxPing.setText("Max: " + max_time + "ms");

NetworkAnalyzerwindow.minPing.setText("Min: " + min_time + "ms");

NetworkAnalyzerwindow.avgPing.setText("Avg: " + avg_time + "ms");

} catch(IOException | NumberFormatException ex){

JOptionPane.showMessageDialog(null, ex);

}

}

I just want to add a dynamically changing graph that would take the ping values and graph them. Can somebody actually help me and not link me to one of the tutorials that only shows how to make a graph by itself.

Here is what the app looks like when running(I would like the graph in the white box. I could make the box bigger):

aaNnH.png

解决方案

Using ProcessBuilder, shown here, execute the ping command in your doInBackground() implementation of a SwingWorker. Parse the output, and publish() the results. Update the dataset in your process() implementation, and the chart will update itself in response. An example using JFreeChart is shown here.

Can you explain a bit more?

Starting with the first example, replace

ProcessBuilder pb = new ProcessBuilder("ls", "-lR", "/");

with

ProcessBuilder pb = new ProcessBuilder("ping", "-c", "3", "example.com");

to get a convenient display of the standard output of ping. In outline, your process() in the second example might look like this:

@Override

protected void process(java.util.List messages) {

for (String message : messages) {

textArea.append(message + "\n");

// parse x, y from message

series.add(x, y);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值