2012-08-09 71 views
2

我目前正在使用時間序列數據,而且我正在使用JFreeCharts XYLineChart來顯示我的數據。對於我的用戶界面,我想創建這些圖表的可點擊縮略圖(然後顯示真正的大圖)。在JPanel上顯示JFreeChart的縮略圖

我試過這種方法來創建圖表的縮略圖,但我不知道如何使用這個BufferedImage來顯示縮略圖。

XYSeriesCollection coll = new XYSeriesCollection(); 
coll.addSeries(rw.getT1().getCurMktCapSeries()); 
coll.addSeries(rw.getT2().getCurMktCapSeries());    
JFreeChart chart = ChartFactory.createXYLineChart(rw.getT1().getName() + " - " + rw.getT2().getName(), 
              "Position", 
              "Course", 
              coll, 
              PlotOrientation.VERTICAL, 
              true, 
              true, 
              false); 

BufferedImage bi = chart.createBufferedImage(1000, 1000, 100, 100, null); 

我試着在網上搜索,但我發現的唯一的事情是上面的方法來創建縮略圖,而不是如何顯示它。

所以我的問題是:

  • 這是創建縮略圖的正確方法?
  • 如何在我的GUI上顯示此縮略圖?

解決方案

我剛剛創建自己的自定義的JPanel

public class ImagePanel extends JPanel 

,然後添加以下的paintComponent方法來繪製縮略圖

protected void paintComponent(Graphics g) { 
    super.paintComponents(g); 
    //Create Image 
    BufferedImage bi = this.createBufferedImage(this.rw); 
    //Draw Background 
    Graphics2D g2d = (Graphics2D)g; 
    g2d.setColor(this.backgroundColor); 
    g2d.fillRect(0, 0, this.getWidth(), this.getHeight()); 
    //Draw Image 
    g2d.drawImage(bi.getScaledInstance(this.getWidth()-10, this.getHeight()-10, 0), 5, 5, this.backgroundColor, null); 
} 

要創建我用的BufferedImage以下方法

private BufferedImage createBufferedImage(ResultWrapper rw2) { 
    //Create JFreeChart 
    XYSeriesCollection coll = new XYSeriesCollection(); 
    coll.addSeries(rw.getT1().getCurMktCapSeries()); 
    coll.addSeries(rw.getT2().getCurMktCapSeries());    
    JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, coll, PlotOrientation.VERTICAL, false, true, false); 

    //Hide Axis 
    XYPlot plot = chart.getXYPlot(); 
    plot.getRangeAxis().setVisible(false); 
    plot.getDomainAxis().setVisible(false); 

    return chart.createBufferedImage(500, 500, 100, 100, null); 
} 

因爲我得到了我想要的東西,JFreeChart的小縮略圖。爲了使它可點擊,只需添加一個MouseListener。

感謝@MadProgrammer使用自定義組件和方法的paintComponent

+0

* 「這是創建縮略圖的正確方法嗎?」*它看起來不錯嗎?一張圖片描繪了一千個單詞,所以縮略圖至少繪製了一段。請考慮每個問題詢問一個問題。 – 2012-08-09 14:49:29

回答

3

根據AndrewThompon的建議,您可以提供具有繪製圖表功能的自定義組件。

看看

這是目前爲止更多地參與則已經被提出,但併爲您提供最大的靈活性

+0

謝謝 我剛剛創建了自己的自定義JPanel,並在paintCompoment方法中繪製了BufferedImage。 工程就像一個魅力。 – Sturmi12 2012-08-09 15:22:56

3

如何顯示在我的GUI這個縮略圖?

有多種方式,但考慮到用例,你可能看使用它作爲一個JButton/JRadioButton的圖標或JTabbedPane的標籤,或在JListJComboBox

查看A Visual Guide to Swing Components以瞭解可能性的快速瀏覽。

+0

或JLabel以及;) – MadProgrammer 2012-08-09 14:48:30

+0

@Mad我很高興你添加了一個;)或者我可能不得不鼓勵你提出一個默認情況下不可聚焦的組件,當有許多其他可能性時可聚焦的,但也有直接掛鉤事件檢測。我打算將JTree添加到原始列表中,但認爲它擴展了'適合用例'的範圍,但是'JLabel'?哈克.. ptui ..;) – 2012-08-09 14:55:46

+0

這個問題是有點fuage在那個方向。 :J拉貝爾是第一個想到的,但公平點關於可調焦的 – MadProgrammer 2012-08-09 14:59:46