Java 每500毫秒平滑渲染一次Swing组件
当我每隔500毫秒调用Java 每500毫秒平滑渲染一次Swing组件,java,swing,charts,rendering,jfreechart,Java,Swing,Charts,Rendering,Jfreechart,当我每隔500毫秒调用paintComponent()以显示更新的图表时,我面临渲染问题。我使用面板上的JFreeChart创建了大约30个条形图 我怎样才能解决这个问题 private void ShowGraphs() { FirstChart.removeAll(); SecondChart.removeAll(); ThirdChart.removeAll(); FirstChart.add(Label1); SecondChart.add(Label2
paintComponent()
以显示更新的图表时,我面临渲染问题。我使用面板上的JFreeChart
创建了大约30个条形图
我怎样才能解决这个问题
private void ShowGraphs() {
FirstChart.removeAll();
SecondChart.removeAll();
ThirdChart.removeAll();
FirstChart.add(Label1);
SecondChart.add(Label2);
ThirdChart.add(Label3);
ChartUpdate(P1,FirstChart);
ChartUpdate(P2,SecondChart);
ChartUpdate(P3,ThirdChart);
//FirstChart, SecondChart, ThirdChart is JPanels
//Tabb is JTabbedPane
paintComponents(Tabb.getGraphics());
}
此代码每500毫秒调用一次,ChartUpdate(MyObject,Panel)
是使用MyObject
的信息在Panel
上构建图表的功能 不要替换视图组件。相反,更新相应的模型,侦听视图将更新自身作为响应。在下面的示例中,由createPane()
返回的每个ChartPanel
都有一个Swing计时器,该计时器每500毫秒更新一次其XYSeries
导入java.awt.BorderLayout;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.Random;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.Timer;
导入org.jfree.chart.ChartFactory;
导入org.jfree.chart.ChartPanel;
导入org.jfree.chart.JFreeChart;
导入org.jfree.chart.plot.PlotOrientation;
导入org.jfree.data.xy.XYSeries;
导入org.jfree.data.xy.XYSeriesCollection;
/**
*@见http://stackoverflow.com/a/38512314/230513
*@见http://stackoverflow.com/a/15715096/230513
*@见http://stackoverflow.com/a/11949899/230513
*/
公开课考试{
专用静态最终整数N=128;
私有静态最终随机=新随机();
私有int n=1;
专用void display(){
JFrame f=新JFrame(“表格”);
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=新的JPanel(新的网格布局(0,1));
对于(int i=0;i<3;i++){
p、 添加(createPane());
}
f、 添加(p,BorderLayout.CENTER);
f、 包装();
f、 setLocationRelativeTo(空);
f、 setVisible(真);
}
私有ChartPanel createPane(){
最终XYSeries=新XYSeries(“数据”);
对于(int i=0;i
闪烁是什么意思?JTabbedPane(双缓冲)是使用PaintComponent()重新绘制的,这会导致闪烁(我最近发现)。也当它重新油漆。它重新绘制时出错。如何才能正确地重新绘制。相关问题:如果没有相关代码,我们如何帮助识别导致问题的问题?请创建并发布您的或。paintComponents(Tabb.getGraphics())代码>这不是如何做自定义绘制!有关详细信息,请参阅。一般提示:1)为了更快地获得更好的帮助,请发布一个或。2) 请学习常见的Java命名法(命名约定-例如EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
,除非它是大写常量
),并一致使用它。FirstChart.removeAll()。。FirstChart.add(标签1)代码>可能有比删除和添加组件更好的方法。一旦你发布了MCVE/SSCCE,我就会知道这种方法是什么。谢谢大家的精彩回答。我得到了所有的改变。我爱你们所有人。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* @see http://stackoverflow.com/a/38512314/230513
* @see http://stackoverflow.com/a/15715096/230513
* @see http://stackoverflow.com/a/11949899/230513
*/
public class Test {
private static final int N = 128;
private static final Random random = new Random();
private int n = 1;
private void display() {
JFrame f = new JFrame("TabChart");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new GridLayout(0, 1));
for (int i = 0; i < 3; i++) {
p.add(createPane());
}
f.add(p, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private ChartPanel createPane() {
final XYSeries series = new XYSeries("Data");
for (int i = 0; i < random.nextInt(N) + N / 2; i++) {
series.add(i, random.nextGaussian());
}
XYSeriesCollection dataset = new XYSeriesCollection(series);
new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
series.add(series.getItemCount(), random.nextGaussian());
}
}).start();
JFreeChart chart = ChartFactory.createXYLineChart("Test", "Domain",
"Range", dataset, PlotOrientation.VERTICAL, false, false, false);
return new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(480, 240);
}
};
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().display();
}
});
}
}