【问题标题】:How can I display a chromatogram as an image?如何将色谱图显示为图像?
【发布时间】:2016-05-25 01:25:15
【问题描述】:

我正在使用 Netbeans 上的 biojava 1.8.1 版本,并使用 ChromatogramGraphic 类来尝试创建色谱图的图像。 (http://www.biojava.org/docs/api1.8/)

我制作了一个文件选择器来访问色谱图,我使用 ChromatogramFactory(来自 biojava)类从文件中创建色谱图对象。

显然,这是:

http://biojava.org/pipermail/biojava-l/2003-June/003896.html

代码可以完成我想要的。我不明白它的作用,我认为我不能使用类似的语法在我的 JFrame 上绘制图像。

任何帮助将不胜感激。

[到目前为止我所拥有的。我不知道它大部分是做什么的。]

    private void renderTrace() throws IOException, UnsupportedChromatogramFormatException {
    ABIFChromatogram abiChrom = new ABIFChromatogram();

    File abi = new File(textarea.getText());

    ABITrace abiTrace = new ABITrace(abi);
    ABIFParser abiParse = new ABIFParser(abi);
    ChromatogramFactory chromFactory = new ChromatogramFactory();


    Chromatogram chrom = ChromatogramFactory.create(abi);


    ChromatogramGraphic gfx = new ChromatogramGraphic(chrom);

    gfx.setHeight(240);
    gfx.setHorizontalScale(2.0f);
     // set some options that affect the output
    // turn off filled-in "callboxes"
    gfx.setOption(ChromatogramGraphic.Option.DRAW_CALL_A,
            Boolean.FALSE);
    gfx.setOption(ChromatogramGraphic.Option.DRAW_CALL_C,
            Boolean.FALSE);
    gfx.setOption(ChromatogramGraphic.Option.DRAW_CALL_G,
            Boolean.FALSE);
    gfx.setOption(ChromatogramGraphic.Option.DRAW_CALL_T,
            Boolean.FALSE);
    gfx.setOption(ChromatogramGraphic.Option.DRAW_CALL_OTHER,
            Boolean.FALSE);
     // this option controls whether each trace/callbox/etc is scaled/positioned
    // individually, or whether the scaling is done on all shapes at the level
    // of the graphics context
    // enabling this option is recommended for higher-quality output

    gfx.setOption(ChromatogramGraphic.Option.USE_PER_SHAPE_TRANSFORM,
            Boolean.TRUE);

    BufferedImage bi = new BufferedImage(
                               gfx.getWidth(), 
                               gfx.getHeight(), 
                               BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = bi.createGraphics();
    g2.setBackground(Color.white);
    g2.clearRect(0, 0, bi.getWidth(), bi.getHeight());
    if (g2.getClip() == null) {
        g2.setClip(new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
    }
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // the main event
    gfx.drawTo(g2);
    // work-around an OS X bug where sometimes the last Shape drawn
    // doesn't show up in the output
    g2.draw(new java.awt.Rectangle(-10, -10, 5, 5));


    gfx.drawTo();


}

【问题讨论】:

标签: java image swing jframe biojava


【解决方案1】:

如果这可行,那么您可以使用 JFrame 的 paint() 方法绘制图像。首先你需要确保这个

gfx.drawTo(g2);

工作,从 gfx 方面。尝试将图像保存到文件中,看看是否存在

try {
  ImageIO.write(bi, "png", new File("gfx-image.png"));
} catch (IOException ex) { ex.printStackTrace(); }

导入 javax.imageio.*;在您的导入语句中。

如果可行并且你可以看到图像,那么在 JFrame 中你需要有类似的东西

public void paint(Graphics g) {
  g.drawImage(bi, 0, 0, this);
}

【讨论】:

  • 我添加了以下try { ImageIO.write(bi, "png", new File("gfx-image.png")); } catch (IOException ex) { ex.printStackTrace(); },但我不知道它的作用。我如何知道图片是否已创建?
  • 它应该将图像保存到当前目录的“gfx-image.png”文件中;检查是否已创建具有该名称的图像 - 运行程序后
  • 成功了! link我有照片。很抱歉打扰你,但我对此很陌生。如何在我的 JFrame 上绘制它?另外,图片很长(2018 个核苷酸长),那么我怎样才能让它左右滚动呢? @gpasch nvm,编辑问题已得到解答。我会在别处查找第二部分的答案,谢谢!
  • 如果您将 JFrame 代码发布到您的问题中,我可能会回答它 - 但可滚动的东西有点麻烦 - 在这个问题中无法完成 - 一旦我们有就打开一个新问题这完成了
  • 我将提出一个新问题,因为我找不到相关问题。我还会贴一张我的 JFrame 和相关源代码的图片(很难在 cmets 中添加大量代码)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-27
  • 2014-04-21
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多