gpt4 book ai didi

java - 自定义 JFreeChart 标题字体太小

转载 作者:太空宇宙 更新时间:2023-11-04 07:08:02 37 4
gpt4 key购买 nike

我正在尝试使用以下代码为 JFreeChart 中的标题设置自定义Font:

    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);

最终的标题字体非常小(几乎看不见):

enter image description here

有什么想法如何将自定义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);
}
}

最佳答案

deriveFont 方法返回您忘记存储的 Font 对象。将代码更改为:

customFont = customFont.deriveFont(24f);

我通过此修复测试了您的代码,使用 http://www.urbanfonts.com 中的免费字体它似乎工作得很好(在 Windows 上)。我的猜测是,当您加载字体文件时,默认大小是 1。

关于java - 自定义 JFreeChart 标题字体太小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21039397/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com