我正在尝试将以下Font
添加到我的JFreeChart
标题中:
http://www.urbanfonts.com/fonts/Back_to_Bay_6.htm
尝试使用代码实现此目的:
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);
以普通字体结束:
任何想法为什么? 有可能它有事情要做我运行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);
}
}
答案 0 :(得分:2)
我得到了许多旧的,表现良好的字体的预期结果,如下所示。有很多方法可以解决这个问题。例如,我在java.awt.FontFormatException: Font name not found
中获得了createFont()
中.ttf
中许多较新的/Library/Fonts
字体的import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.function.Function2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class Function2DDemo1 extends ApplicationFrame {
public Function2DDemo1(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private static JFreeChart createChart(XYDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createXYLineChart("Function2DDemo1 ",
"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 {
File f = new File("/Library/Fonts/Microsoft/Perpetua.ttf");
Font customFont = Font.createFont(Font.TRUETYPE_FONT, f);
customFont = customFont.deriveFont(36f);
chart.getTitle().setFont(customFont);
System.out.println(customFont.getFontName());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (FontFormatException | 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) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Function2DDemo1 demo = new Function2DDemo1("JFreeChart: Function2DDemo1.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
});
}
}
。您可能会尝试在另一个上下文中验证字体。
经测试:
{{1}}