-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFonts.java
More file actions
106 lines (93 loc) · 2.83 KB
/
Fonts.java
File metadata and controls
106 lines (93 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Line2D;
import java.awt.font.LineMetrics;
import java.awt.font.FontRenderContext;
import java.awt.Font;
import java.awt.Color;
import java.awt.Dimension;
public class Main{
public static void main(String args[]){
EventQueue.invokeLater(()->{
JFrame frame=new FontFrame();
frame.setTitle("Testing Font");
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 f=new Font("Serif",Font.BOLD,36);
g2.setFont(f);
FontRenderContext context=g2.getFontRenderContext();
Rectangle2D bounds=f.getStringBounds(message,context);
double x=(getWidth() - bounds.getWidth())/2; //help you to set the start and end coordinate
double y=(getHeight() - bounds.getHeight())/2;// here I will get the text in the middle of my gui centers string with the surrounding component
double ascent=-bounds.getY();
double baseY=y+ascent;
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 getPreferredSize(){
return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
}
/*
program to list all the font present in the users system
import java.awt.*;
public class ListFont{
public static void main(String args[]){
String[] fontNames=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for(String fontName:fontNames){
System.out.println(fontName);
}
}
font faces can get copyrighted font foundary gets royalty payments
but some companies have made he copy so they don't have to pay royalties
For example,
Helvetics is shipped to Arial
Common baselines,
five logical font names:
Sans Serif
Serif
Monospaced
Dialog
DialogInput
You will always find these fonts in client machine
values
Font.PLAIN
Font.BOLD
Font.ITALIC
Font.BOLD+Font.Italic
Mapping from logical to physical font names
jre/lib subdirectory
fontconfig.properties files
Java fonts contain the usual ASCII characters as well as symbols
\u2297 gets a unique character
Typesetting terms
ascent
descent
leading
baseline
height
baseline
height=ascent+descent+leading
*/