2016-07-21 10 views
3

paintComponent()を500ミリ秒ごとに呼び出して、更新されたチャートを表示するとレンダリングの問題に直面しています。私はJFreeChartPanelにつけて作成した約30のバサルトを持っています。スイングコンポーネントは500ミリ秒ごとにスムーズにレンダリングされます

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)MyObjectの情報を使用してPanel上のチャートの建物関数です。

+0

あなたはちらつきとはどういう意味ですか? – Javant

+0

JTabbedPane(doubleBuffered)は、PaintComponent()を使用して再描画され、ちらつきが発生します(最近見つかった)。また、それを再描画するとき。エラーで再描画します。どのように私は間違いなく再塗りつぶしできますか? –

+1

関連する質問:関連コードなしで問題に寄与する問題を特定するにはどうすればよいですか? [mcve]または[sscce](http://sscce.org)を作成して投稿してください。 –

答えて

4

Do not ビューコンポーネントを置き換えます。代わりに、対応するモデルを更新すると、リスニング・ビューはそれに応答して更新されます。次の例では、の番号がcreatePane()で返されます。のスウィングTimerは、5003msごとにXYSeriesに更新されます。

​​

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

すてきな答えをありがとう。すべての変更があります。皆さん大好きです。 –

関連する問題