I want to use packages such as jmathplot to draw function images, but after adding related packages, a problem that I can't solve is thrown.
import javax.swing.*;
import org.math.plot.*;
public class Example {
public static void main(String[] args) {
// define your data
double[] x = { 1, 2, 3, 4, 5, 6 };
double[] y = { 45, 89, 6, 32, 63, 12 };
// create your PlotPanel (you can use it as a JPanel)
Plot2DPanel plot = new Plot2DPanel();
// define the legend position
plot.addLegend("SOUTH");
// add a line plot to the PlotPanel
plot.addLinePlot("my plot", x, y);
// put the PlotPanel in a JFrame like a JPanel
JFrame frame = new JFrame("a plot panel");
frame.setSize(600, 600);
frame.setContentPane(plot);
frame.setVisible(true);
}
}
And the relevant jar files are downloaded on GitHub.
You are apparently using the 1.0 version of the JMathPlot library (either from GitHub or from Maven Central). That version of the JAR is missing the /org/math/plot/icons/
folder that contains the library's standard icons. That is leading to an NPE when Swing attempts to load an icon from the classpath.
The solution is to use the JMathPlot 1.0.1 JAR which has the missing icons. You can download in by hand from Maven Central via this URL:
If you use Maven, Gradle and so on for your Java builds, the correct Maven dependency is
<dependency>
<groupId>com.github.yannrichet</groupId>
<artifactId>JMathPlot</artifactId>
<version>1.0.1</version>
</dependency>
Alternatively, you could get a working JAR by building the library from source.
It is a shame that the repo owner has not uploaded the 1.0.1 JAR file to GitHub ... leaving the broken 1.0 version as a trap for people to fall into.