java字体难看,java字体显示不正确

when i import a font from a file, it displays really weirdly. it was supposed to say "TEST", but it just said "_".

here is my code:

public void paint(Graphics g2){

Graphics2D g = (Graphics2D)g2;

//

Font f = null;

try {

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

ge.registerFont((f = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("Triforce.ttf"))));

} catch (IOException e) {

e.printStackTrace();

}

catch(FontFormatException e)

{

e.printStackTrace();

}

//

g.setFont(f.deriveFont(2));

g.drawString("sdfsdfetf", 100, 100);

}

解决方案

As specified by the Font#createFont Java Docs

... The new Font is created with a point size of 1 and style PLAIN ...

(emphasis applied by me)

This means that you need to derive a new Font instance from this Font with the required properties, for example

try {

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

Font f = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/Triforce.ttf"));

if (!ge.registerFont(f)) {

System.out.println("Unable to register font");

}

f = f.deriveFont(Font.PLAIN, 24);

setFont(f);

} catch (IOException e) {

e.printStackTrace();

} catch (FontFormatException e) {

e.printStackTrace();

}

TY8gz.png

You should pre-load you font (and apply it's properties) outside of the context of the paint method. Painting should run as fast as possible and will be called multiple times, sometimes in quick succession. Loading resources within any paint method wastes time and resource...

As an example...

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Font;

import java.awt.FontFormatException;

import java.awt.FontMetrics;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GraphicsEnvironment;

import java.io.IOException;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

public class Test1 {

public static void main(String[] args) {

new Test1();

}

public Test1() {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

ex.printStackTrace();

}

JFrame frame = new JFrame("Testing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new TestPane());

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

public class TestPane extends JPanel {

public TestPane() {

try {

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

Font f = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/Triforce.ttf"));

if (!ge.registerFont(f)) {

System.out.println("Unable to register font");

}

f = f.deriveFont(Font.PLAIN, 24);

setFont(f);

} catch (IOException e) {

e.printStackTrace();

} catch (FontFormatException e) {

e.printStackTrace();

}

}

@Override

public Dimension getPreferredSize() {

return new Dimension(200, 200);

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g.create();

FontMetrics fm = g2d.getFontMetrics();

String text = "Hello world";

int x = (getWidth() - fm.stringWidth(text)) / 2;

int y = ((getHeight() - fm.getHeight()) + fm.getAscent()) / 2;

g2d.drawString(text, x, y);

g2d.dispose();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值