【发布时间】:2013-10-30 11:32:01
【问题描述】:
我正在使用 JFreeChart 库来实时绘制遗传算法的进度。
我在 UI 中使用 Swing。我有一个面板,我在其中绘制算法的所有各种参数,以及一个 ChartPanel 对象。这个对象是在我调用算法的搜索方法(它在每一代更新图表的 XYSeries 对象)之前绘制的,并且在搜索结束时,所有的值都被准确地绘制出来。
根据文档,ChartPanel 对象在其各自的图表更新时被重绘。显然,Swing 面板本身直到搜索完成后才被重绘,我调用了 repaint(),但是我能做些什么来解决这个问题呢?
这是图表代码:
public class XYSeriesDemo {
private static final long serialVersionUID = 1L;
private XYSeries series;
private JFreeChart chart;
public XYSeriesDemo(String str) {
series = new XYSeries(str);
XYSeriesCollection data = new XYSeriesCollection(series);
chart = createChart(data);
}
public XYSeries getSeries() {
return series;
}
public ChartPanel getChartPanel() {
return new ChartPanel(chart);
}
private JFreeChart createChart(final XYDataset data) {
JFreeChart chart = ChartFactory.createXYLineChart(
"Best fitness across generations",
"Generation",
"Fitness",
data,
PlotOrientation.VERTICAL,
true,
true,
false
);
XYPlot plot = chart.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis = plot.getRangeAxis();
axis.setAutoRange(true);
return chart;
}
}
在我的面板构造函数中,我正在执行以下操作(这会绘制一个空图表):
demo = new XYSeriesDemo("Best fitness");
this.add(demo.getChartPanel());
这是当用户订购搜索时 Swing 框架在我的 JPanel 对象中调用的方法:
public void solve() {
gen = new Random(seed);
XYSeries s = demo.getSeries();
GeneticAlgorithm ga = new GeneticAlgorithm(pop, crossoverP, mutationP,
eliteSize, maxGens, gen, s);
best = ga.search();
state = State.SOLVED;
time = ga.getTime() / 1E9;
}
在每一代,算法中的搜索方法只是简单地做:
series.add(generation, pop.getBestFitness());
感谢您的阅读。
【问题讨论】:
-
您的问题似乎很清楚,但是提供一些代码会很好。谢谢。
-
完成。因为这是大学工作,所以我补充了我能提供的所有内容。
-
优秀。好问题,赞成:)
标签: java swing jfreechart