-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.java
More file actions
86 lines (84 loc) · 2.98 KB
/
Text.java
File metadata and controls
86 lines (84 loc) · 2.98 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
/**
* Final Game Text Class
* @Author Ilya Kononov
* @Date = January 22 2023
* This class represents a text field that can then be drawn onto the screen
*/
import java.awt.*;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.font.FontRenderContext;
public class Text{
private String text;
private Font font;
private Color color;
private int fontSize;
private int centerX;
private int x;
private int y;
private int width;
private int height;
public Text(String text, Font font, Color color, int centerX, int y) {
this.text = text;
this.font = font;
this.color = color;
this.fontSize = this.font.getSize();
this.centerX = centerX;
this.y = y;
this.width = this.getTextWidth();
this.x = (this.centerX - (this.width / 2));
this.height = this.getTextHeight();
}
//-----------------------------------------------------------------------------
// Getters and Setters
public int getWidth(){
return this.width;
}
public int getHeight(){
return this.height;
}
public String getText(){
return this.text;
}
public void setX(int x){
this.x = x;
}
public void setCenterX(int centerX){
this.centerX = centerX;
this.x = centerX - this.width/2;
}
public void setText(String newText){
this.text = newText;
this.width = this.getTextWidth();
this.x = (this.centerX - (this.width / 2));
}
//------------------------------------------------------------------------------
// Methods
// Using AffineTransform and FontRenderContext to get the dimensions of the font
private int getTextWidth() {
AffineTransform affineTransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affineTransform,true,true);
int textWidth = (int)(this.font.getStringBounds(this.text, frc).getWidth());
return textWidth;
}
private int getTextHeight() {
AffineTransform affineTransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affineTransform,true,true);
int textHeight = (int)(this.font.getStringBounds(this.text, frc).getHeight());
return textHeight;
}
public void draw(Graphics graphics) {
((Graphics2D)graphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setFont(this.font);
graphics.setColor(this.color);
graphics.drawString(this.text, this.x, this.y + this.fontSize);
}
public void draw(Graphics graphics, int centerX, int y) {
((Graphics2D)graphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setFont(this.font);
graphics.setColor(this.color);
graphics.drawString(this.text, centerX - this.width/2, y + this.fontSize);
}
}