Font and draw baselines and string borders in the computer

1. To know the fonts allowed on a particular calculator, you need to call the GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames method, which returns a character array containing all available font names. The following program will print out the names of all fonts on the system:

import java.awt.*;
public class LocalGrapgics {
public static void main(String[] args)
{
    String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
    for(String fontName : fontNames)
        System.out.println(fontName);
}
}

The results are as follows:
write picture description here

Example of drawing baseline and string border:

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class FontTest {
public static void main(String[] args)
{
    //事件分派线程中的执行代码
    EventQueue.invokeLater(() ->
    {
        JFrame frame = new FontFrame();
        frame.setTitle("FontTest");//确定该框架标题栏的文字
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//让程序简单退出的响应式语句
        frame.setVisible(true);//显示按钮
    });
}
}

class FontFrame extends JFrame
{
    public FontFrame()
    {
        add(new FontComponent());
        pack();//调整窗口大小,要考虑到其组件的首选大小
    }
}

class FontComponent extends JComponent
{
    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 200;
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        String message = "Hello,World!";
        /*要想使用某种字体绘制字符
         * 必须首先利用指定的字体名、字体风格和字体大小来创建Font类对象
         * 第三个参数是以点数目计算的字体大小、
         * 点数目的排版中普遍使用的表示字体大小的单位
         * 每英寸包含72个点*/
        Font f = new Font("Serif",Font.BOLD,36);
        g2.setFont(f);//获取或设置当前的字体

        /*要想得到屏幕设备字体属性的描述对象
         * 需要调用Graphics2D类中的getFontRenderContext方法
         * 它将返回一个FontRenderContext类对象
         * 可以直接将这个对象传递给Font类的getStringBounds方法
         * getStringBounds方法将返回包围字符串的矩形
        */
        //返回字体的字体绘制环境
        FontRenderContext context = g2.getFontRenderContext();

        //返回这个图形文本中,指定字体特征的字体绘制环境
        getFontRenderContext(); 

        /*
        返回包围这个字符串的矩形 
        矩形的起点为基线,矩形顶端的y坐标等于上坡度的负值。矩形的高度等于上坡度
        下坡度和行间距之和,宽度等于字符串的宽度
        */
        Rectangle2D bounds = f.getStringBounds(message, context);


        double x = (getWidth() - bounds.getWidth()) / 2;
        double y = (getHeight() - bounds.getHeight()) / 2;

        double ascent = -bounds.getY();
        double baseY = y + ascent;

        //采用和当前字体和颜色绘制一个字符串,后两个参数分别是字符串开始的x坐标和字符串基线的y坐标
        g2.drawString(message, (int)x,(int)baseY);

        g2.setPaint(Color.LIGHT_GRAY);

        g2.draw(new Line2D.Double(x,baseY,x + bounds.getWidth(),baseY));

        Rectangle2D rect = new Rectangle2D.Double(x,y,bounds.getWidth(),bounds.getHeight());

        g2.draw(rect);


    }
    public Dimension getPreferrdsize()
    {
        return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
    }
}


The results are as follows:
write picture description here

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325742672&siteId=291194637