Javafx 用维修工具更换螺纹
我使用JavaFX线程来更新JavaFX图表Javafx 用维修工具更换螺纹,javafx,javafx-2,javafx-8,Javafx,Javafx 2,Javafx 8,我使用JavaFX线程来更新JavaFX图表 import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import javafx.animation.AnimationTimer; import ja
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import javafx.animation.AnimationTimer;
import javafx.scene.Node;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
/**
* A chart that fills in the area between a line of data points and the axes. Good for comparing accumulated totals over time.
*
* @see javafx.scene.chart.Chart
* @see javafx.scene.chart.Axis
* @see javafx.scene.chart.NumberAxis
* @related charts/line/LineChart
* @related charts/scatter/ScatterChart
*/
public class ChartTestOne
{
private static final int MAX_DATA_POINTS = 50;
private Series series;
private int xSeriesData = 0;
private final ConcurrentLinkedQueue<Number> dataQ = new ConcurrentLinkedQueue<>();
private ExecutorService executor;
private AddToQueue addToQueue;
//private Timeline timeline2;
private NumberAxis xAxis;
public AreaChart<Number, Number> init()
{
xAxis = new NumberAxis(0, MAX_DATA_POINTS, MAX_DATA_POINTS / 10);
xAxis.setForceZeroInRange(false);
xAxis.setAutoRanging(false);
xAxis.setTickLabelsVisible(false);
xAxis.setTickMarkVisible(false);
xAxis.setMinorTickVisible(false);
NumberAxis yAxis = new NumberAxis();
yAxis.setAutoRanging(true);
//-- Chart
final AreaChart<Number, Number> sc = new AreaChart<Number, Number>(xAxis, yAxis)
{
// Override to remove symbols on each data point
@Override
protected void dataItemAdded(Series<Number, Number> series, int itemIndex, Data<Number, Number> item)
{
}
};
sc.setAnimated(false);
sc.setId("liveAreaChart");
sc.setTitle("Animated Area Chart");
//-- Chart Series
series = new AreaChart.Series<>();
series.setName("Area Chart Series");
sc.getData().add(series);
// look up first series fill
Node node = sc.lookup(".default-color0.chart-series-area-fill");
// set the first series fill to translucent pale green
node.setStyle("-fx-fill: linear-gradient(#f2f2f2, #d4d4d4);"
+ " -fx-background-insets: 0 0 -1 0, 0, 1, 2;"
+ " -fx-background-radius: 3px, 3px, 2px, 1px;");
Node nodew = sc.lookup(".chart-series-area-line");
// set the first series fill to translucent pale green
nodew.setStyle("-fx-stroke: #989898; -fx-stroke-width: 1px; ");
executor = Executors.newCachedThreadPool(new ThreadFactory()
{
@Override
public Thread newThread(Runnable r)
{
Thread thread = new Thread(r);
thread.setDaemon(true);
return thread;
}
});
addToQueue = new AddToQueue();
executor.execute(addToQueue);
//-- Prepare Timeline
prepareTimeline();
return sc;
}
private class AddToQueue implements Runnable
{
@Override
public void run()
{
try
{
// add a item of random data to queue
dataQ.add(Math.random());
Thread.sleep(1150);
executor.execute(this);
}
catch (InterruptedException ex)
{
//Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
// Timeline gets called in the JavaFX Main thread
private void prepareTimeline()
{
// Every frame to take any data from queue and add to chart
new AnimationTimer()
{
@Override
public void handle(long now)
{
addDataToSeries();
}
}.start();
}
private void addDataToSeries()
{
for (int i = 0; i < 20; i++)
{ // Add 20 numbers to the plot+
if (dataQ.isEmpty())
break;
series.getData().add(new AreaChart.Data(xSeriesData++, dataQ.remove()));
}
// remove points to keep us at no more than MAX_DATA_POINTS
if (series.getData().size() > MAX_DATA_POINTS)
{
series.getData().remove(0, series.getData().size() - MAX_DATA_POINTS);
}
// update
xAxis.setLowerBound(xSeriesData - MAX_DATA_POINTS);
xAxis.setUpperBound(xSeriesData - 1);
}
}
import java.util.concurrent.ConcurrentLinkedQueue;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.ThreadFactory;
导入javafx.animation.AnimationTimer;
导入javafx.scene.Node;
导入javafx.scene.chart.AreaChart;
导入javafx.scene.chart.NumberAxis;
导入javafx.scene.chart.XYChart.Data;
导入javafx.scene.chart.XYChart.Series;
/**
*填充数据点线和轴之间区域的图表。适用于比较一段时间内的累计总数。
*
*@see javafx.scene.chart.chart
*@see javafx.scene.chart.Axis
*@see javafx.scene.chart.NumberAxis
*@相关图表/折线图/折线图
*@相关图表/散点图/散点图
*/
公共类ChartTestOne
{
私有静态最终整数最大数据点=50;
私人系列;
私有int xSeriesData=0;
私有最终ConcurrentLinkedQueue数据Q=新ConcurrentLinkedQueue();
私人遗嘱执行人;
私有AddToQueue AddToQueue;
//私人时间线2;
私人号码xis xAxis;
公共区域图初始化()
{
xAxis=新的数字轴(0,最大数据点,最大数据点/10);
xAxis.setForceZeroInRange(假);
xAxis.setAutoRanging(假);
xAxis.setTickLabelsVisible(假);
xAxis.setTickMarkVisible(false);
xAxis.setMinorTickVisible(false);
NumberAxis yAxis=新的NumberAxis();
yAxis.setAutoRanging(真);
//--图表
最终面积图sc=新面积图(X轴、Y轴)
{
//替代以删除每个数据点上的符号
@凌驾
已添加受保护的无效dataItemAdded(系列、int itemIndex、数据项)
{
}
};
sc.1(假);
sc.setId(“liveAreaChart”);
sc.setTitle(“动画面积图”);
//--图表系列
series=新的面积图。series();
系列名称(“面积图系列”);
sc.getData().add(系列);
//查找第一个系列填充
Node Node=sc.lookup(“.default-color0.图表系列区域填充”);
//将第一个系列填充设置为半透明淡绿色
node.setStyle(“-fx填充:线性渐变(#f2f2f2,#d4d4);”
+-fx背景插图:0-10,0,1,2
+“-fx背景半径:3px,3px,2px,1px;”;
节点nodew=sc.lookup(“.chart-series-area-line”);
//将第一个系列填充设置为半透明淡绿色
nodew.setStyle(“-fx笔划:#989898;-fx笔划宽度:1px;”);
executor=Executors.newCachedThreadPool(newThreadFactory())
{
@凌驾
公共线程newThread(可运行的r)
{
螺纹=新螺纹(r);
setDaemon(true);
返回线程;
}
});
addToQueue=新的addToQueue();
executor.execute(addToQueue);
//--准备时间表
制备美林();
返回sc;
}
私有类AddToQueue实现可运行
{
@凌驾
公开募捐
{
尝试
{
//将一项随机数据添加到队列
add(Math.random());
睡眠(1150);
执行人。执行(本);
}
捕获(中断异常例外)
{
//Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
//在JavaFX主线程中调用Timeline
私有无效制备单()
{
//从队列中获取任何数据并添加到图表的每个帧
新的AnimationTimer()
{
@凌驾
公共无效句柄(长)
{
addDataToSeries();
}
}.start();
}
私有void addDataToSeries()
{
对于(int i=0;i<20;i++)
{//在绘图中添加20个数字+
if(dataQ.isEmpty())
打破
series.getData().add(新的AreaChart.Data(xSeriesData++,dataQ.remove());
}
//删除点以使我们保持不超过最大数据点
if(series.getData().size()>最大数据点)
{
series.getData().remove(0,series.getData().size()-最大数据点);
}
//更新
xAxis.setLowerBound(xSeriesData-最大数据点);
xAxis.setUpperBound(xSeriesData-1);
}
}
我想使用JavaFX服务来生成图表值。你能帮我重构代码吗?我的建议:
public class ChartTestOne
{
private static final int MAX_DATA_POINTS = 50;
private Series series;
private int xSeriesData = 0;
private final ConcurrentLinkedQueue<Number> dataQ = new ConcurrentLinkedQueue<>();
private NumberAxis xAxis;
public AreaChart<Number, Number> init()
{
xAxis = new NumberAxis(0, MAX_DATA_POINTS, MAX_DATA_POINTS / 10);
xAxis.setForceZeroInRange(false);
xAxis.setAutoRanging(false);
xAxis.setTickLabelsVisible(false);
xAxis.setTickMarkVisible(false);
xAxis.setMinorTickVisible(false);
NumberAxis yAxis = new NumberAxis();
yAxis.setAutoRanging(true);
// Chart
final AreaChart<Number, Number> sc = new AreaChart<Number, Number>(xAxis, yAxis)
{
// Override to remove symbols on each data point
@Override
protected void dataItemAdded(Series<Number, Number> series, int itemIndex, Data<Number, Number> item)
{
}
};
sc.setAnimated(false);
sc.setId("liveAreaChart");
sc.setTitle("Animated Area Chart");
// Chart Series
series = new AreaChart.Series<>();
series.setName("Area Chart Series");
sc.getData().add(series);
// Look up first series fill
Node node = sc.lookup(".default-color0.chart-series-area-fill");
// Set the first series fill to translucent pale green
node.setStyle("-fx-fill: linear-gradient(#f2f2f2, #d4d4d4);"
+ " -fx-background-insets: 0 0 -1 0, 0, 1, 2;"
+ " -fx-background-radius: 3px, 3px, 2px, 1px;");
Node nodew = sc.lookup(".chart-series-area-line");
// Set the first series fill to translucent pale green
nodew.setStyle("-fx-stroke: #989898; -fx-stroke-width: 1px; ");
boolean run = true;
Task task = new Task<Void>()
{
@Override
public Void call() throws InterruptedException
{
while (run)
{
dataQ.add(Math.random());
Thread.sleep(1150);
}
return null;
}
};
new Thread(task).start();
// Prepare Timeline
prepareTimeline();
return sc;
}
// Timeline gets called in the JavaFX Main thread
private void prepareTimeline()
{
// Every frame to take any data from queue and add to chart
new AnimationTimer()
{
@Override
public void handle(long now)
{
addDataToSeries();
}
}.start();
}
private void addDataToSeries()
{
for (int i = 0; i < 20; i++)
{ // Add 20 numbers to the plot+
if (dataQ.isEmpty())
break;
series.getData().add(new AreaChart.Data(xSeriesData++, dataQ.remove()));
}
// remove points to keep us at no more than MAX_DATA_POINTS
if (series.getData().size() > MAX_DATA_POINTS)
{
series.getData().remove(0, series.getData().size() - MAX_DATA_POINTS);
}
// update
xAxis.setLowerBound(xSeriesData - MAX_DATA_POINTS);
xAxis.setUpperBound(xSeriesData - 1);
}
}
公共类ChartTestOne
{
私有静态最终整数最大数据点=50;
私人系列;
私有int xSeriesData=0;
私有最终ConcurrentLinkedQueue数据Q=新ConcurrentLinkedQueue();
私人号码xis xAxis;
公共区域图初始化()
{
xAxis=新的数字轴(0,最大数据点,最大数据点/10);
xAxis.setForceZeroInRange(假);
xAxis.setAutoRanging(假);
xAxis.setTickLabelsVisible(假);
xAxis.setTickMarkVisible(false);
xAxis.setMinorTickVisible(false);
NumberAxis yAxis=新的NumberAxis();
yAxis.setAutoRanging(真);
//图表
最终面积图sc=新面积图(X轴、Y轴)
{
//替代以删除每个数据点上的符号
@凌驾
已添加受保护的无效dataItemAdded(系列、int itemIndex、数据项)
{
}
};
sc.1(假);
sc.setId(“liveAreaChart”);
sc.setTitle(“动画面积图”);
//图表系列
series=新的面积图。series();
系列名称(“面积图系列”);
sc.getData().add(系列);
//查找第一个系列填充
Node Node=sc.lookup(“