I am trying to add the following Font
to my JFreeChart
title:
http://www.urbanfonts.com/fonts/Back_to_Bay_6.htm
Trying to achieve this with the code:
InputStream is = new FileInputStream("backtobay.ttf");
java.awt.Font customFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is);
customFont = customFont.deriveFont(24f);
chart.getTitle().setFont(customFont);
Ends up with a normal font:
Any ideas why? Is it possible it has something to do that I am running Mac?
public class Function2DDemo1 extends ApplicationFrame {
public Function2DDemo1(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
private static JFreeChart createChart(XYDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createXYLineChart("Function2DDemo1 ", // chart
// title
"X", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL, true, // include legend
true, // tooltips
false // urls
);
// SET A CUSTOM TITLE FONT
try {
InputStream is = new FileInputStream("backtobay.ttf");
java.awt.Font customFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is);
customFont = customFont.deriveFont(24f);
chart.getTitle().setFont(customFont);
// This prints "Back to Bay 6"
System.out.println(customFont.getFontName());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (FontFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
XYPlot plot = (XYPlot) chart.getPlot();
plot.getDomainAxis().setLowerMargin(0.0);
plot.getDomainAxis().setUpperMargin(0.0);
return chart;
}
public static XYDataset createDataset() {
XYDataset result = DatasetUtilities.sampleFunction2D(new X2(), -4.0, 4.0, 40, "f(x)");
return result;
}
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
static class X2 implements Function2D {
public double getValue(double x) {
return x * x + 2;
}
}
public static void main(String[] args) {
Function2DDemo1 demo = new Function2DDemo1("JFreeChart: Function2DDemo1.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}