使用 JFreeChart 库在 awt.Image 上绘制轴

发布于 2024-12-05 18:12:59 字数 817 浏览 1 评论 0原文

我有一个类需要能够接收图像并添加一组轴。我不想自己编写代码,而宁愿偷懒并利用 JFreeChart 库。

这是我到目前为止所拥有的,据我所知,它没有做任何事情:

public void addAxis(Image sourceImage, double min, double max)
   {
      NumberAxis numAxis = new NumberAxis();
      numAxis.setRange(min, max);

      int width = sourceImage.getWidth(null);
      int height = sourceImage.getHeight(null);
      Rectangle2D size = new Rectangle(width, height);
      Graphics2D graphics = (Graphics2D) sourceImage.getGraphics();
      numAxis.draw(graphics, 0, size, size, RectangleEdge.LEFT, null);

      return;
   }

我传递给它的 Image 是使用 BufferedImage 创建的代码>TYPE_INT_ARGB。

可能还有其他更适合此目的的库,但不幸的是,很难获得批准将库添加到我的项目中,而 JFreeChart 已经获得批准。为了其他读者的利益,请随意提及替代库。

编辑:由于各种原因,我需要在图像上绘制轴,我无法在图表上绘制图像或执行任何会改变其大小的操作。

I have a class that needs to be able to take in an image and add a set of axis. I don't feel like writing out the code myself and would rather be lazy and utilize the JFreeChart library.

Here's what I have so far, which isn't doing anything as far as I can tell:

public void addAxis(Image sourceImage, double min, double max)
   {
      NumberAxis numAxis = new NumberAxis();
      numAxis.setRange(min, max);

      int width = sourceImage.getWidth(null);
      int height = sourceImage.getHeight(null);
      Rectangle2D size = new Rectangle(width, height);
      Graphics2D graphics = (Graphics2D) sourceImage.getGraphics();
      numAxis.draw(graphics, 0, size, size, RectangleEdge.LEFT, null);

      return;
   }

The Image that I'm passing into it is created as a BufferedImage using TYPE_INT_ARGB.

There may be other libraries that are better suited for this, but unfortunately it is difficult to get approval to add a library to my project and JFreeChart is already approved. Please feel free to mention alternative libraries anyways for the benefit of other readers.

Edit: for various reasons, I need to draw the axis on the image, I cannot draw the image on a chart or do anything that would change it's size.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2024-12-12 18:12:59

想通了,为任何想做同样事情的人发帖。

该线

numAxis.draw(graphics, 0, size, size, RectangleEdge.LEFT, null);

应该是

numAxis.draw(graphics, 20, size, size, RectangleEdge.LEFT, null);

否则轴将从图像中绘制出来。我没有弄清楚这一点(我正在为此而自责),因为轴正在获取我在图形对象上使用的最后一种颜色,而该颜色恰好正在绘制背景。

Figured it out, posting for anyone who wants to do the same thing.

The line

numAxis.draw(graphics, 0, size, size, RectangleEdge.LEFT, null);

should be

numAxis.draw(graphics, 20, size, size, RectangleEdge.LEFT, null);

Otherwise the axis is drawn off the image. I didn't figure this out (and I'm kicking myself for this one) because the axis was getting the last color I'd used on the graphics object, which happened to be drawing the background.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文