2013-10-24 34 views
1

我試圖做一個類,使得一個類似的jfreechart的演示撥號。 但是,如果窗口小於打印刻度盤所需的大小,我希望帶撥號的框架可以滾動。我使用滾動窗格實現了這一點,但是會發生的是,由於錶盤具有重量級組件,因此它們會在視圖端口和滾動條之外進行打印,而且我不能那樣做。的Java jfreechart的剪裁問題

我寫來測試此功能的代碼是:

編輯:我做的代碼較小試圖使它SSCCE(這是我第一次張貼問題,很抱歉,如果它仍然過大)

編輯2:我已經意識到這是我使用(並有能力使用)的jfreechart的分配的問題這是1.0.9。此錯誤對於當前版本的jfreechart無法重現。

import java.awt.*; 
import javax.swing.JPanel; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.plot.dial.*; 
import org.jfree.data.general.DefaultValueDataset; 
import org.jfree.ui.*; 

public class DialDoubleNeedle extends JPanel{ 

    private DefaultValueDataset dataset1; 
    private DefaultValueDataset dataset2; 
    private JFreeChart chart; 
    public DialDoubleNeedle() { 
     super(); 

     this.dataset1 = new DefaultValueDataset(0.10); 
     this.dataset2 = new DefaultValueDataset(0.50); 

     // get data for diagrams 
     DialPlot plot = new DialPlot(); 
     plot.setView(0.0, 0.0, 1.0, 1.0); 
     plot.setDataset(0, this.dataset1); 
     plot.setDataset(1, this.dataset2); 
     StandardDialFrame dialFrame = new StandardDialFrame(); 
     dialFrame.setBackgroundPaint(Color.lightGray); 
     dialFrame.setForegroundPaint(Color.darkGray);   
     plot.setDialFrame(dialFrame); 

     GradientPaint gp = new GradientPaint(new Point(), 
       new Color(255, 255, 255), new Point(), 
       new Color(170, 170, 220)); 

     DialBackground db = new DialBackground(gp); 
     db.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL)); 
     plot.setBackground(db); 
     DialTextAnnotation annotation1 = new DialTextAnnotation("m/s"); 
     annotation1.setFont(new Font("Dialog", Font.BOLD, 14)); 
     annotation1.setRadius(0.7); 

     plot.addLayer(annotation1); 

     DialValueIndicator dvi = new DialValueIndicator(0); 
     dvi.setFont(new Font("Dialog", Font.PLAIN, 10)); 
     dvi.setOutlinePaint(Color.darkGray); 
     dvi.setRadius(0.60); 
     dvi.setAngle(-103.0); 
     dvi.setNumberFormat(new java.text.DecimalFormat("0.00")); 
     plot.addLayer(dvi); 

     DialValueIndicator dvi2 = new DialValueIndicator(1); 
     dvi2.setFont(new Font("Dialog", Font.PLAIN, 10)); 
     dvi2.setOutlinePaint(Color.red); 
     dvi2.setRadius(0.60); 
     dvi2.setAngle(-77.0); 
     dvi2.setNumberFormat(new java.text.DecimalFormat("0.00")); 
     plot.addLayer(dvi2); 

     StandardDialScale scale = new StandardDialScale(0, 1, -120, -300, 0.1, 5); 
     scale.setTickRadius(0.88); 
     scale.setTickLabelOffset(0.15); 
     scale.setTickLabelFont(new Font("Dialog", Font.PLAIN, 14)); 
     plot.addScale(0, scale); 

     StandardDialScale scale2 = new StandardDialScale(0, 1, -120, -300, 0.1, 5); 
     scale2.setTickRadius(0.50); 
     scale2.setTickLabelOffset(0.15); 
     scale2.setTickLabelFont(new Font("Dialog", Font.PLAIN, 10)); 
     scale2.setMajorTickPaint(Color.red); 
     plot.addScale(1, scale2); 
     plot.mapDatasetToScale(1, 1); 

     DialPointer needle2 = new DialPointer.Pin(1); 
     needle2.setRadius(0.55); 
     plot.addLayer(needle2); 

     DialPointer needle = new DialPointer.Pointer(0); 
     plot.addLayer(needle); 

     DialCap cap = new DialCap(); 
     cap.setRadius(0.10); 
     plot.setCap(cap); 

     chart = new JFreeChart(plot); 
     chart.setTitle("Title"); 
     ChartPanel cp1 = new ChartPanel(chart); 
     add(cp1); 


    } 

    void setTitle(String title){ 
     chart.setTitle(title); 
     return; 
    } 


    public static void main(final String args[]) { 
     final javax.swing.JFrame test = new javax.swing.JFrame(); 
     test.addWindowListener(new java.awt.event.WindowAdapter() { 
      @Override 
      public void windowClosing(final java.awt.event.WindowEvent e) { 
       System.exit(0); 
      } 
     }); 

     final JPanel jpanel1 = new JPanel(); 

     DialDoubleNeedle app1 = new DialDoubleNeedle(); 
     app1.setTitle("Tittle 1"); 

     jpanel1.add(app1); 

     final javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane(); 

     scrollPane.setViewportView(jpanel1); 

     test.getContentPane().add(scrollPane); 
     test.pack(); 
     test.setVisible(true); 


     int i; 
     Number v; 
     for(i=0; i<1000000000; i++){ 
      if(i==(1000000000-1)){ 
       i=0; 
       v=app1.dataset1.getValue(); 
       app1.dataset1.setValue(v.doubleValue()+0.01); 
      } 
     } 

    } 
} 

如果你編譯你上面的代碼可以看到,它運行時,它看起來不錯,但如果它收縮它最終得到了一些在滾動條的錶盤部分(重量級的)的。 我能做些什麼來解決這個問題?

+0

請使用SSCCE:http://sscce.org/ – StormeHawke

+0

+1 [SSCCE(http://sscce.org/),雖然@StormeHawke是正確的,它可能是一個短一點。 :-) – trashgod

+0

是真的,即時學習。這個問題的一部分是由於我得到的視覺錯誤不是所有的組件都只有幾個,因此我幾乎發佈了我的代碼的所有圖形部分。 – jonny

回答

1

據我所知,沒有JFreechart也不org.jfree.chart.plot.dial.*含有重量級組件。您的示例通常會爲我調整大小。有幾個注意事項:

  • 不要使用setPreferredSize()當你真的要覆蓋getPreferredSize(),如圖here和討論here

  • Swing GUI對象應該在event dispatch thread上構建和操縱只有

  • 取代在initial thread上循環播放,請使用javax.swing.Timer來爲動畫播放速度。

  • 你在不恰當的使用GridBagConstraints。一個簡單的垂直GridLayout運作良好,並避免了在面板中的任何set*Size()或滾動窗格中的需要。

    final JPanel jpanel1 = new JPanel(new GridLayout(0, 1)); 
    
  • JFrame可以設置默認的關閉操作,從而消除了WindowListener的需要。

    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    
+0

我真的認爲它確實有很重的組件,即漸變背景和錶盤形狀等。注意,如果縮小它,只會覆蓋一些東西。 感謝您的所有建議。我要補充一點,在上面的代碼主要是隻是一個快速和骯髒的測試爲主,不以任何方式涉及到如何使用它除了視覺錯誤仍然 – jonny

+0

對不起,我不能重現你描述這有利於同步的效果,問題;在你的平臺上嘗試'invokeLater()'。 – trashgod

+0

那麼,我試圖獲得足夠的聲望來發布圖片,那麼它應該很容易顯示出問題。對我而言,這與我選擇的平臺無關。 我沒有嘗試過的是使用JFreeChart的最新發行版,因爲這是一個商業產品,我們只有jfreechart的舊版本的許可證 – jonny