Mercurial > hg > Members > sugi > javafx
view src/alice/topology/manager/keeparive/SchedulerViewer.java @ 26:650c0f1270a3
add package alice.topology.manager.keeparive
author | sugi |
---|---|
date | Tue, 05 Nov 2013 03:57:09 +0900 |
parents | |
children | fe96dd350d9d |
line wrap: on
line source
package alice.topology.manager.keeparive; /** * Copyright (c) 2008, 2012 Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. */ import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.util.Duration; /** * A simulated stock line chart. * * @see javafx.scene.chart.Chart * @see javafx.scene.chart.LineChart * @see javafx.scene.chart.NumberAxis * @see javafx.scene.chart.XYChart */ public class SchedulerViewer extends Application { private PingScheduler ps; private XYChart.Series<Number,Number> hourDataSeries; private NumberAxis xAxis; private Timeline animation; private double hours = 0; private double minutes = 0; private double timeInHours = 0; private int count; private void init(Stage primaryStage) { Group root = new Group(); primaryStage.setScene(new Scene(root)); root.getChildren().add(createChart()); // create timeline to add new data every 60th of second animation = new Timeline(); animation.getKeyFrames().add(new KeyFrame(Duration.millis(1000/60), new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent actionEvent) { // 6 minutes data per frame for(int count=0; count < 6; count++) { nextTime(); plotTime(); plotGraph(); } } })); animation.setCycleCount(Animation.INDEFINITE); } protected LineChart<Number, Number> createChart() { xAxis = new NumberAxis(0,200,10); final NumberAxis yAxis = new NumberAxis(0,100,10); final LineChart<Number,Number> lc = new LineChart<Number,Number>(xAxis,yAxis); // setup chart lc.setId("lineStockDemo"); lc.setCreateSymbols(false); lc.setAnimated(false); lc.setLegendVisible(false); lc.setTitle("Task Viewer"); xAxis.setLabel("Time"); xAxis.setForceZeroInRange(false); yAxis.setLabel("Task Name"); yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis,"$",null)); // add starting data hourDataSeries = new XYChart.Series<Number,Number>(); lc.getData().add(hourDataSeries); return lc; } private void plotGraph() { if (ps.updateFlag){ if(!hourDataSeries.getData().isEmpty()) hourDataSeries.getData().clear(); count = (int) (ps.getTotalTime()/1000); hourDataSeries.getData().add(new XYChart.Data<Number, Number>(count, ps.nowTask.getTaskNum())); count +=ps.nowTask.getTime()/1000; hourDataSeries.getData().add(new XYChart.Data<Number, Number>(count, ps.nowTask.getTaskNum())); for (TaskInfo info : ps.getTaskList()){ hourDataSeries.getData().add(new XYChart.Data<Number, Number>(count, info.getTaskNum())); count += info.getTime()/1000; hourDataSeries.getData().add(new XYChart.Data<Number, Number>(count, info.getTaskNum())); } ps.updateFlag = false; } } private void nextTime() { if (minutes == 59) { hours ++; minutes = 0; } else { minutes ++; } timeInHours = hours + ((1d/60d)*minutes); } private void plotTime() { if ((timeInHours % 1) == 0) { xAxis.setLowerBound(xAxis.getLowerBound()); xAxis.setUpperBound(xAxis.getUpperBound()); } } public void StartScheduler(){ ps = new PingScheduler(); TaskInfo a = new TaskInfo("a", 10 * 1000); TaskInfo b = new TaskInfo("b", 20 * 1000); TaskInfo c = new TaskInfo("c", 60* 1000); TaskInfo d = new TaskInfo("d", 40 * 1000); TaskInfo e = new TaskInfo("e", 80 * 1000); ps.addTask(a); ps.addTask(b); ps.addTask(c); ps.addTask(d); ps.addTask(e); ps.execute(); } public void play() { animation.play(); } @Override public void stop() { animation.pause(); } @Override public void start(Stage primaryStage) throws Exception { StartScheduler(); init(primaryStage); primaryStage.show(); play(); } }