java.awt.Font.createFont()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(538)

本文整理了Java中java.awt.Font.createFont()方法的一些代码示例,展示了Font.createFont()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Font.createFont()方法的具体详情如下:
包路径:java.awt.Font
类名称:Font
方法名:createFont

Font.createFont介绍

暂无

代码示例

代码示例来源:origin: skylot/jadx

@Nullable
public static Font openFontTTF(String name) {
  String fontPath = "/fonts/" + name + ".ttf";
  try (InputStream is = Utils.class.getResourceAsStream(fontPath)) {
    Font font = Font.createFont(Font.TRUETYPE_FONT, is);
    return font.deriveFont(12f);
  } catch (Exception e) {
    LOG.error("Failed load font by path: {}", fontPath, e);
    return null;
  }
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

public static Font loadTtf( String resource ) {
  try {
    return Font.createFont(Font.TRUETYPE_FONT, 
                TestBitmapFontLayout.class.getResourceAsStream(resource));            
  } catch( FontFormatException | IOException e ) {
    throw new RuntimeException("Error loading resource:" + resource, e);
  }
}

代码示例来源:origin: looly/hutool

/**
 * 根据文件创建字体<br>
 * 首先尝试创建{@link Font#TRUETYPE_FONT}字体,此类字体无效则创建{@link Font#TYPE1_FONT}
 * 
 * @param fontStream 字体流
 * @return {@link Font}
 * @since 3.0.9
 */
public static Font createFont(InputStream fontStream) {
  try {
    return Font.createFont(Font.TRUETYPE_FONT, fontStream);
  } catch (FontFormatException e) {
    // True Type字体无效时使用Type1字体
    try {
      return Font.createFont(Font.TYPE1_FONT, fontStream);
    } catch (Exception e1) {
      throw new UtilException(e1);
    }
  } catch (IOException e) {
    throw new IORuntimeException(e);
  }
}

代码示例来源:origin: looly/hutool

/**
 * 根据文件创建字体<br>
 * 首先尝试创建{@link Font#TRUETYPE_FONT}字体,此类字体无效则创建{@link Font#TYPE1_FONT}
 * 
 * @param fontFile 字体文件
 * @return {@link Font}
 * @since 3.0.9
 */
public static Font createFont(File fontFile) {
  try {
    return Font.createFont(Font.TRUETYPE_FONT, fontFile);
  } catch (FontFormatException e) {
    // True Type字体无效时使用Type1字体
    try {
      return Font.createFont(Font.TYPE1_FONT, fontFile);
    } catch (Exception e1) {
      throw new UtilException(e);
    }
  } catch (IOException e) {
    throw new IORuntimeException(e);
  }
}

代码示例来源:origin: looly/hutool

/**
 * 根据文件创建字体<br>
 * 首先尝试创建{@link Font#TRUETYPE_FONT}字体,此类字体无效则创建{@link Font#TYPE1_FONT}
 * 
 * @param fontStream 字体流
 * @return {@link Font}
 * @since 3.0.9
 */
public static Font createFont(InputStream fontStream) {
  try {
    return Font.createFont(Font.TRUETYPE_FONT, fontStream);
  } catch (FontFormatException e) {
    // True Type字体无效时使用Type1字体
    try {
      return Font.createFont(Font.TYPE1_FONT, fontStream);
    } catch (Exception e1) {
      throw new UtilException(e1);
    }
  } catch (IOException e) {
    throw new IORuntimeException(e);
  }
}

代码示例来源:origin: looly/hutool

/**
 * 根据文件创建字体<br>
 * 首先尝试创建{@link Font#TRUETYPE_FONT}字体,此类字体无效则创建{@link Font#TYPE1_FONT}
 * 
 * @param fontFile 字体文件
 * @return {@link Font}
 * @since 3.0.9
 */
public static Font createFont(File fontFile) {
  try {
    return Font.createFont(Font.TRUETYPE_FONT, fontFile);
  } catch (FontFormatException e) {
    // True Type字体无效时使用Type1字体
    try {
      return Font.createFont(Font.TYPE1_FONT, fontFile);
    } catch (Exception e1) {
      throw new UtilException(e);
    }
  } catch (IOException e) {
    throw new IORuntimeException(e);
  }
}

代码示例来源:origin: libgdx/libgdx

/** @param ttfFileRef The file system or classpath location of the TrueTypeFont file. */
static private Font createFont (String ttfFileRef) {
  try {
    return Font.createFont(Font.TRUETYPE_FONT, Gdx.files.absolute(ttfFileRef).read());
  } catch (FontFormatException ex) {
    throw new GdxRuntimeException("Invalid font: " + ttfFileRef, ex);
  } catch (IOException ex) {
    throw new GdxRuntimeException("Error reading font: " + ttfFileRef, ex);
  }
}

代码示例来源:origin: libgdx/libgdx

/** @param ttfFileRef The file system or classpath location of the TrueTypeFont file. */
static private Font createFont (String ttfFileRef) {
  try {
    return Font.createFont(Font.TRUETYPE_FONT, Gdx.files.absolute(ttfFileRef).read());
  } catch (FontFormatException ex) {
    throw new GdxRuntimeException("Invalid font: " + ttfFileRef, ex);
  } catch (IOException ex) {
    throw new GdxRuntimeException("Error reading font: " + ttfFileRef, ex);
  }
}

代码示例来源:origin: magefree/mage

private static Font loadFont(String name) {
  try (InputStream in = ModernCardRenderer.class.getResourceAsStream("/cardrender/" + name + ".ttf")) {
    return Font.createFont(
        Font.TRUETYPE_FONT, in);
  } catch (IOException e) {
    LOGGER.info("Failed to load font `" + name + "`, couldn't find resource.");
  } catch (FontFormatException e) {
    LOGGER.info("Failed to load font `" + name + "`, bad format.");
  }
  return new Font("Arial", Font.PLAIN, 1);
}
public static final Font BASE_BELEREN_FONT = loadFont("beleren-bold");

代码示例来源:origin: winder/Universal-G-Code-Sender

public static Font createFont(InputStream is, String fontName) {
    try {
      Font font = Font.createFont(Font.TRUETYPE_FONT, is);
      is.close();
      return font;
    } catch (Exception exc) {
      exc.printStackTrace();
      System.err.println(fontName + " not loaded.  Using serif font.");
      return new Font("sans-serif", Font.PLAIN, 24);
    }
  }
}

代码示例来源:origin: stackoverflow.com

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class LoadFont {
  public static void main(String[] args) throws Exception {
    // This font is < 35Kb.
    URL fontUrl = new URL("http://www.webpagepublicity.com/" +
      "free-fonts/a/Airacobra%20Condensed.ttf");
    Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
    GraphicsEnvironment ge = 
      GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(font);
    JList fonts = new JList( ge.getAvailableFontFamilyNames() );
    JOptionPane.showMessageDialog(null, new JScrollPane(fonts));
  }
}

代码示例来源:origin: stackoverflow.com

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class DisplayFont {
  public static void main(String[] args) throws Exception {
    URL fontUrl = new URL("http://www.webpagepublicity.com/" +
      "free-fonts/a/Airacobra%20Condensed.ttf");
    Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
    font = font.deriveFont(Font.PLAIN,20);
    GraphicsEnvironment ge =
      GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(font);

    JLabel l = new JLabel(
      "The quick brown fox jumped over the lazy dog. 0123456789");
    l.setFont(font);
    JOptionPane.showMessageDialog(null, l);
  }
}

代码示例来源:origin: geotools/geotools

/** Configure the Bistream Vera Sans font so that it's available to the JVM */
  public static void setupVeraFonts() throws IOException, FontFormatException {
    FontCache.getDefaultInstance()
        .registerFont(
            java.awt.Font.createFont(
                java.awt.Font.TRUETYPE_FONT,
                TestData.getResource(RendererBaseTest.class, "Vera.ttf")
                    .openStream()));
  }
}

代码示例来源:origin: geotools/geotools

@Before
public void setUp() throws IOException, FontFormatException {
  FontCache.getDefaultInstance()
      .registerFont(
          Font.createFont(
              Font.TRUETYPE_FONT,
              TestData.getResource(LineTest.class, "Vera.ttf").openStream()));
  FontCache.getDefaultInstance()
      .registerFont(
          Font.createFont(
              Font.TRUETYPE_FONT,
              TestData.getResource(LineTest.class, "DroidSansFallback.ttf")
                  .openStream()));
}

代码示例来源:origin: geotools/geotools

@Override
protected void setUp() throws Exception {
  // setup data
  File property = new File(TestData.getResource(this, "point.properties").toURI());
  PropertyDataStore ds = new PropertyDataStore(property.getParentFile());
  pointFS = ds.getFeatureSource("point");
  lineFS = ds.getFeatureSource("line");
  bounds = new ReferencedEnvelope(0, 10, 0, 10, DefaultGeographicCRS.WGS84);
  // load font
  Font f =
      Font.createFont(
          Font.TRUETYPE_FONT,
          TestData.getResource(this, "recreate.ttf").openStream());
  FontCache.getDefaultInstance().registerFont(f);
  //        System.setProperty("org.geotools.test.interactive", "true");
}

代码示例来源:origin: geotools/geotools

@Before
public void setUp() throws Exception {
  // setup data
  File property = new File(TestData.getResource(this, "point.properties").toURI());
  PropertyDataStore ds = new PropertyDataStore(property.getParentFile());
  pointFS = ds.getFeatureSource("point");
  lineFS = ds.getFeatureSource("line");
  pointRotationFS = ds.getFeatureSource("pointRotation");
  arrowBasesFS = ds.getFeatureSource("arrowBases");
  bounds = new ReferencedEnvelope(0, 10, 0, 10, CRS.decode("EPSG:4326"));
  arrowBounds = new ReferencedEnvelope(-1, 5, -1, 11, CRS.decode("EPSG:4326"));
  // load font
  Font f =
      Font.createFont(
          Font.TRUETYPE_FONT,
          TestData.getResource(this, "recreate.ttf").openStream());
  FontCache.getDefaultInstance().registerFont(f);
  // System.setProperty("org.geotools.test.interactive", "true");
}

代码示例来源:origin: geotools/geotools

@Before
public void setUp() throws Exception {
  File property = new File(TestData.getResource(this, "tilerect.properties").toURI());
  PropertyDataStore ds = new PropertyDataStore(property.getParentFile());
  polyfs = ds.getFeatureSource("tilerect");
  property = new File(TestData.getResource(this, "tilelines.properties").toURI());
  ds = new PropertyDataStore(property.getParentFile());
  linefs = ds.getFeatureSource("tilelines");
  leftTileBounds =
      new ReferencedEnvelope(
          0, 10, 0, 10, polyfs.getBounds().getCoordinateReferenceSystem());
  rightTileBounds =
      new ReferencedEnvelope(
          10, 20, 0, 10, polyfs.getBounds().getCoordinateReferenceSystem());
  // load font
  Font f =
      Font.createFont(
          Font.TRUETYPE_FONT,
          TestData.getResource(this, "recreate.ttf").openStream());
  FontCache.getDefaultInstance().registerFont(f);
  // System.setProperty("org.geotools.test.interactive", "true");
}

代码示例来源:origin: geotools/geotools

@Before
public void setUp() throws Exception {
  File property = new File(TestData.getResource(this, "square.properties").toURI());
  PropertyDataStore ds = new PropertyDataStore(property.getParentFile());
  fs = ds.getFeatureSource("square");
  bfs = ds.getFeatureSource("bigsquare");
  bounds = fs.getBounds();
  bounds.expandBy(0.2, 0.2);
  // load font
  Font f =
      Font.createFont(
          Font.TRUETYPE_FONT,
          TestData.getResource(this, "recreate.ttf").openStream());
  FontCache.getDefaultInstance().registerFont(f);
  // System.setProperty("org.geotools.test.interactive", "true");
}

代码示例来源:origin: geotools/geotools

@Before
public void setUp() throws Exception {
  File property = new File(TestData.getResource(this, "square.properties").toURI());
  PropertyDataStore ds = new PropertyDataStore(property.getParentFile());
  fs = ds.getFeatureSource("square");
  bfs = ds.getFeatureSource("bigsquare");
  bounds = fs.getBounds();
  bounds.expandBy(0.2, 0.2);
  // load font
  Font f =
      Font.createFont(
          Font.TRUETYPE_FONT,
          TestData.getResource(this, "recreate.ttf").openStream());
  FontCache.getDefaultInstance().registerFont(f);
  // System.setProperty("org.geotools.test.interactive", "true");
}

代码示例来源:origin: geotools/geotools

@Before
public void setUp() throws Exception {
  File property = new File(TestData.getResource(this, "square.properties").toURI());
  PropertyDataStore ds = new PropertyDataStore(property.getParentFile());
  fs = ds.getFeatureSource("square");
  bfs = ds.getFeatureSource("bigsquare");
  bounds = fs.getBounds();
  bounds.expandBy(0.2, 0.2);
  // load font
  Font f =
      Font.createFont(
          Font.TRUETYPE_FONT,
          TestData.getResource(this, "recreate.ttf").openStream());
  FontCache.getDefaultInstance().registerFont(f);
  // System.setProperty("org.geotools.test.interactive", "true");
}

相关文章