How to Annotate a PNG File with Axes and Labels in Java?
How to Annotate a PNG File with Axes and Labels in Java
Adding axes and labels to an existing PNG image can be challenging. Rather than attempting modifications that could lead to errors and inconsistencies, it's recommended to integrate the annotations during the chart creation process.
Customizing Axes and Labels Using JFreeChart
JFreeChart provides a comprehensive API for customizing the look of charts, including axes and labels. The following example illustrates how to create a custom chart with tailored annotations:
<code class="java">import java.awt.Color; import java.awt.EventQueue; import java.awt.Shape; import java.awt.geom.Ellipse2D; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JFrame; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; public class ResponseTime { private static final int N = 600; private static final String title = "ResponseTime"; private static final Random random = new Random(); private static final Shape circle = new Ellipse2D.Double(-3, -3, 6, 6); private static final Color line = Color.gray; private ChartPanel createPanel() { JFreeChart chart = ChartFactory.createXYLineChart( title, "Elapsed Time (secs)", "Response Time (secs)", createDataset(), PlotOrientation.VERTICAL, true, true, false); XYPlot plot = chart.getXYPlot(); MyRenderer renderer = new MyRenderer(true, true, N); plot.setRenderer(renderer); renderer.setSeriesShape(0, circle); renderer.setSeriesPaint(0, line); renderer.setUseFillPaint(true); renderer.setSeriesShapesFilled(0, true); renderer.setSeriesShapesVisible(0, true); renderer.setUseOutlinePaint(true); renderer.setSeriesOutlinePaint(0, line); ValueAxis range = plot.getRangeAxis(); range.setLowerBound(0.5); return new ChartPanel(chart); } private static class MyRenderer extends XYLineAndShapeRenderer { private List<Color> clut; public MyRenderer(boolean lines, boolean shapes, int n) { super(lines, shapes); clut = new ArrayList<>(n); for (int i = 0; i < n; i++) { clut.add(Color.getHSBColor((float) i / n, 1, 1)); } } @Override public Paint getItemFillPaint(int row, int column) { return clut.get(column); } } private XYDataset createDataset() { XYSeriesCollection result = new XYSeriesCollection(); XYSeries series = new XYSeries("Series 1"); for (double x = 0; x < N - 1; x++) { series.add(x, f(x)); } series.add(25, 1.75); // outlier result.addSeries(series); return result; } private double f(double x) { double y = 0.004 * x + .75; return y + random.nextGaussian() * y / 10; } private void display() { JFrame f = new JFrame(title); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(createPanel()); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ResponseTime().display(); } }); } }</code>
This approach allows you to control every aspect of the chart, including the style and placement of the axes, labels, and plot elements.
Color-Coding Individual Items
To color-code individual items, JFreeChart provides the getItemFillPaint() method in the XYLineAndShapeRenderer class. Here's how to use it:
<code class="java">private static class MyRenderer extends XYLineAndShapeRenderer { private List<Color> clut; public MyRenderer(boolean lines, boolean shapes, int n) { super(lines, shapes); clut = new ArrayList<>(n); for (int i = 0; i < n; i++) { clut.add(Color.getHSBColor((float) i / n, 1, 1)); } } @Override public Paint getItemFillPaint(int row, int column) { return clut.get(column); } }</code>
By overriding this method, you can specify a custom color for each item in the chart. The Color.getHSBColor() method is used to generate a full spectrum of colors.
By following these techniques, you can create customized charts with annotations that meet your specific requirements.
The above is the detailed content of How to Annotate a PNG File with Axes and Labels in Java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
