java绘制默认字体
程序员文章站
2024-01-04 18:32:10
...
import java.awt.Color;
import java.awt.Font;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
/**
* @Author zyh
* @Date 2020/10/9 18:36
*/
public class SwingUtil {
private static AffineTransform atf = new AffineTransform();
private static FontRenderContext frc = new FontRenderContext(atf, true,
true);
public static int getStringHeight(String str, Font font) {
if (str == null || str.isEmpty() || font == null) {
return 0;
}
return (int) font.getStringBounds(str, frc).getHeight();
}
public static int getStringWidth(String str, Font font) {
if (str == null || str.isEmpty() || font == null) {
return 0;
}
return (int) font.getStringBounds(str, frc).getWidth();
}
/**
* 将形如“#FFFFFF”的颜色转换成Color
*
* @param hex
* @return
*/
public static Color getColorFromHex(String hex) {
if (hex == null || hex.length() != 7) {
try {
throw new Exception("不能转换这种类型的颜色");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
int r = Integer.valueOf(hex.substring(1, 3), 16);
int g = Integer.valueOf(hex.substring(3, 5), 16);
int b = Integer.valueOf(hex.substring(5), 16);
return new Color(r, g, b);
}
}
import javax.swing.*;
import java.awt.*;
/**
* 自定义字体,读取pdf中的字体
* @Author zyh
* @Date 2020/10/9 16:17
*/
public class Window extends JFrame {
//private String name="Helvetica";
//private String name="黑体";
private String name="Consolas";
private String string="你好,世界!";
private String title="示例窗口";
public Window(){
}
/*public Window(String name){
this.name=name;
}*/
public Window(String name,String string){
this.name=name;
this.string=string;
}
public Window(String string){
this.string=string;
}
public Window(String name,String string,String title){
this.name=name;
this.string=string;
this.title=title;
}
public void init(){
this.setTitle(title);
this.setSize(800,400);
this.setLocation(800,200);
this.setVisible(true);
//this.setVisible(false);
Font f1 = new Font(name,Font.PLAIN,15);
this.setFont(f1);
}
public void paint(Graphics graphics){
super.paint(graphics);
graphics.drawString(string,100,100);
//graphics.drawString(string,100,100);
Font f1 = new Font(name,Font.PLAIN,15);
int stringWidth = SwingUtil.getStringWidth(string, f1);
int stringHeight = SwingUtil.getStringHeight(string, f1);
System.out.println("name:"+name+",w:"+stringWidth+",h:"+stringHeight);
/*int stringWidth = graphics.getFontMetrics().stringWidth(string);
System.out.println("name:"+name+",w:"+stringWidth);*/
}
public void destroy(){
this.dispose();//关闭窗口
}
public static void main(String[] args) throws InterruptedException {
/*Window w=new Window();
w.init();*/
String s="Beijing * Gate ကို ကျွန်မ ချစ်တယ်၊ * Gate မှာ နေထွက်တယ်။";
//String s="hello,world!";
/*GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontName = e.getAvailableFontFamilyNames();
System.out.println("fontName.length:"+fontName.length);
for (int i = 0; i < fontName.length; i++) {
System.out.println("i:"+i+","+fontName[i]);
Thread.sleep(1000*1);
Window window=new Window(fontName[i],s,fontName[i]);
window.init();
Thread.sleep(1000*1);
window.destroy();
}*/
String a="Jag älskar Pekings Himmelska Port, solen går upp mot Himmelska fridens port!";
String b="hello";
Window window=new Window("Myanmar Text",a);
window.init();
Thread.sleep(1000*60);
window.destroy();
/*GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontName = e.getAvailableFontFamilyNames();
System.out.println("fontName.length:"+fontName.length);
for (int i = 0; i < fontName.length; i++) {
System.out.println("i:"+i+","+fontName[i]);
}*/
}
}
推荐阅读