添加自定义字体JFreeChart [英] Adding custom font JFreeChart

查看:260
本文介绍了添加自定义字体JFreeChart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 JFreeChart 标题中添加下面的 Font
http://www.urbanfonts.com/fonts/Back_to_Bay_6.htm



尝试使用代码实现此目的:

pre $ 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){
//创建图表...
JFreeChart图表= ChartFactory.createXYLineChart(Function2DDemo1,/ / chart
// title
X,// x轴标签
Y,// y轴标签
dataset,//数据
PlotOrientation。 VERTICAL,true,//包含图例
true,//工具提示
false //网址
);

//设置一个CUSTOM TITLE字体
尝试{
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);
//打印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);
回报图;


public static XYDataset createDataset(){
XYDataset result = DatasetUtilities.sampleFunction2D(new X2(),-4.0,4.0,40,f(x)) ;
返回结果;


public static JPanel createDemoPanel(){
JFreeChart chart = createChart(createDataset());
返回新的ChartPanel(图表);


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



$ div $解析方案

对于许多较老的,行为良好的字体,我们会得到预期的结果,如下所示。有很多方法会出现这种情况。例如,我在 createFont()中找到 java.awt.FontFormatException:未找到字体名称新的 .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;
导入org.jfree.chart.ChartPanel;
导入org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
导入org.jfree.chart.plot.XYPlot;
import org.jfree.data.function.Function2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;
导入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){
//创建图表...
JFreeChart图表= ChartFactory.createXYLineChart(Function2DDemo1,
X,// x轴标签
Y,// y轴标签
dataset,//数据
PlotOrientation.VERTICAL,true,//包含图例
true,//工具提示
false //网址
);

//设置自定义标题字体
尝试{
文件f =新建文件(/ Library / Fonts / Microsoft / Perpetua.ttf);
字体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);
回报图;


public static XYDataset createDataset(){
XYDataset result = DatasetUtilities.sampleFunction2D(new X2(),-4.0,4.0,40,f(x)) ;
返回结果;


public static JPanel createDemoPanel(){
JFreeChart chart = createChart(createDataset());
返回新的ChartPanel(图表);


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


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

解决方案

I'm getting the expected result for many older, well-behaved fonts, as shown below. There are a lot of ways for this to go awry. For example, I'm getting java.awt.FontFormatException: Font name not found in createFont() for a lot of the newer .ttf fonts in /Library/Fonts. You might try to validate the font in another context.

As tested:

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

这篇关于添加自定义字体JFreeChart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆