我目前正在使用時間序列數據,而且我正在使用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
* 「這是創建縮略圖的正確方法嗎?」*它看起來不錯嗎?一張圖片描繪了一千個單詞,所以縮略圖至少繪製了一段。請考慮每個問題詢問一個問題。 – 2012-08-09 14:49:29