【问题标题】:Displaying a thumbnail of a JFreeChart on a JPanel在 JPanel 上显示 JFreeChart 的缩略图
【发布时间】:2012-08-06 19:40:13
【问题描述】:

我目前正在处理时间序列数据,并且正在使用 JFreeCharts XYLineChart 来显示我的数据。对于我的用户界面,我想创建这些图表的可点击缩略图(然后显示真正的大图表)。

我尝试了这种方法来创建图表的缩略图,但我不知道如何使用这个 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 方法

【问题讨论】:

  • “这是创建缩略图的正确方法吗?” 看起来对吗?图片描绘了一千个单词,因此缩略图至少描绘了一个段落。还请考虑对每个问题提出一个问题。

标签: java swing jpanel thumbnails jfreechart


【解决方案1】:

如何在我的 GUI 上显示此缩略图?

有多种方法,但考虑到用例,您可能会将其用作JButton/JRadioButton 的图标或JTabbedPane 的选项卡,或用于JListJComboBox

查看A Visual Guide to Swing Components 以快速了解各种可能性。

【讨论】:

  • @Mad 我很高兴你添加了一个 ;) 或者我可能不得不激怒你,因为当有很多其他可能性不仅可以聚焦时,我可能不得不提醒你建议一个默认情况下不可聚焦的组件,但也有直接的事件检测挂钩。我打算将JTree 添加到原始列表中,但认为这超出了“适合用例”的范围,但是JLabel?哈克.. ptui.. ;)
  • 这个问题在那个方向上有点问题。 :p JLabel 是我想到的第一件事,但关于可聚焦的公平点
【解决方案2】:

根据 AndrewThompon 的建议,您可以提供一个能够绘制图表的自定义组件。

看看

这比已经建议的要复杂得多,但确实为您提供了最大的灵活性

【讨论】:

  • 谢谢,我刚刚创建了自己的自定义JPanel,并在paintCompoment 方法中绘制了BufferedImage。像魅力一样工作。
猜你喜欢
  • 1970-01-01
  • 2013-02-15
  • 2016-05-24
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多