平滑地每500毫秒渲染Swing组件

编程入门 行业动态 更新时间:2024-10-08 18:38:28
本文介绍了平滑地每500毫秒渲染Swing组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我每隔500毫秒调用 paintComponent()来显示更新的图表时,我面临着渲染问题。我使用 JFreeChart 在面板上创建了约30个barcharts。

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 stackoverflow/a/38512314/230513 * @see stackoverflow/a/15715096/ 230513 * @see stackoverflow/a/11949899/230513 * / public class Test { private static final int N = 128 ; private static final随机随机= 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

I am facing rendering problem when I call paintComponent() every 500 millisecond to show updated charts. I have around 30 barcharts created by using JFreeChart on Panel.

Rendering with error and How can I solve this problem?

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()); }

This code is called every 500 milliseconds and ChartUpdate(MyObject, Panel) is chart building function on Panel using MyObject's info.

解决方案

Don't replace the view component. Instead, update the corresponding model and the listening view will update itself in response. In the example below, each ChartPanel returned by createPane() has a Swing Timer that updates its XYSeries every 500 ms.

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 stackoverflow/a/38512314/230513 * @see stackoverflow/a/15715096/230513 * @see stackoverflow/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(); } }); } }

更多推荐

平滑地每500毫秒渲染Swing组件

本文发布于:2023-05-31 14:00:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/393027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平滑   组件   Swing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!