我正在尝试使用以下代码为Font
中的标题设置自定义JFreeChart
:
InputStream is = new FileInputStream("test.ttf");
java.awt.Font customFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is);
customFont.deriveFont(24f);
chart.getTitle().setFont(customFont);
结尾使用非常小的字体标题(几乎不可见):
如何将自定义Font
添加到JFreeChart
标题?
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("test.ttf");
java.awt.Font customFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is);
customFont.deriveFont(24f);
chart.getTitle().setFont(customFont);
} 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);
}
}
答案 0 :(得分:1)
deriveFont
方法返回您忘记存储的Font
对象。将您的代码更改为:
customFont = customFont.deriveFont(24f);
我使用http://www.urbanfonts.com中的免费字体测试了您的代码,使用此修补程序,它似乎工作得很好(在Windows上)。我的猜测是,当您加载字体文件时,默认大小为1。