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.
Figured it out, posting for anyone who wants to do the same thing.
The line
should be
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.