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

Rendering with error and
我怎么解决这个问题?

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)是使用Panel的信息在MyObject上构建图表的功能。

最佳答案

不要更换视图组件。而是,更新相应的模型,并且侦听视图将作为响应进行更新。在下面的示例中,ChartPanel返回的每个createPane()都有一个Swing Timer,每500 ms更新一次其XYSeries

java - 每500毫秒平滑地渲染一次Swing组件-LMLPHP

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

关于java - 每500毫秒平滑地渲染一次Swing组件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38509824/

10-10 11:17