-
Notifications
You must be signed in to change notification settings - Fork 0
/
Writer.java
129 lines (115 loc) · 3.17 KB
/
Writer.java
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.font.TextLayout;
/**
* A set of static methods used to draw text on Graphics.
* The default font is: "standard, size 14, times new roman"
* The default color is: "Color.white"
*
* @author Justin C
*/
public class Writer
{
/**
* The width/height of the screen being drawn to.
*/
public static final int WIDTH = 800, HEIGHT = 800;
/**
* The current font being used for the text.
*/
private static Font font = new Font("times new roman", 0, 14);
/**
* The current color being used for the text.
*/
public static Color color = Color.white;
/**
* Don't let anyone instantiate this class!
*/
private Writer()
{
}
/**
* Resets constants.
*/
public static void reset(){
setMainFont("times new roman", 0, 14);
color = Color.white;
}
/**
* Returns a String[] containing all available fonts for this platform.
*
* All code is attributed to:
* Alvin Alexander at "https://alvinalexander.com/
* blog/post/jfc-swing/swing-faq-list-fonts-current-platform"
*/
public static String[] allFonts()
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
return ge.getAvailableFontFamilyNames();
}
/**
* Draws s on g at the coordinates (x, y).
*
* @param s
* A String that takes up only one line.
*/
public static void showText(Graphics g, String s, int x, int y)
{
g.setFont(font);
g.setColor(color);
g.drawString(s, x, y);
}
/**
* Draws s on g such that s is horizontally centered
* at the elevation y on g.
*/
public static void showText(Graphics g, String s, int y)
{
showText(g, s, xCenter(g, s), y);
}
/**
* Draws s on g such that s is horizontally and
* vertically centered on g.
*/
public static void showText(Graphics g, String s)
{
showText(g, s, HEIGHT / 2);
}
/**
* Draws s on g such that the center of s is
* (WIDTH + xDisplace, y).
*/
public static void showText(int xDisplace, Graphics g, String s, int y)
{
int x = xCenter(g, s) + xDisplace;
if (x < 0)
throw new IllegalArgumentException(
"The x-displacement sets the text off of the screen!");
showText(g, s, x, y);
}
/**
* Returns an integer x_c such that g.drawString(s, x_c, y)
* is horizontally centered. If x_true is the actual center,
* then Math.abs(x_c - x_true) <= 0.5 is always satisfied.
*
* Code is attributed to:
* MadProgrammer at "https://stackoverflow.com/questions/23729944/"
*/
public static int xCenter(Graphics g, String s)
{
Graphics2D g2d = (Graphics2D) g;
TextLayout txt = new TextLayout(s, font, g2d.getFontRenderContext());
double xDist = txt.getBounds().getWidth();
return (int) Math.round((WIDTH - xDist) / 2);
}
/**
* Sets the font used by this class.
*/
public static void setMainFont(String name, int style, int size)
{
font = new Font(name, style, size);
}
}