Im In this code, I have a window with a blue box in one of the corners.
I need to get text center aligned on this box.
public class drawComponent extends JComponent {
public void paintComponent(Graphics g){ //called on window update
int clueHeightDiff= 0;
int gap = 5;
int border = 10;
Font font = new Font("Ariel", Font.PLAIN, 30);
Color blue = new Color(0,0,255);
Color white = new Color(255,255,255);
int winH = Jeopardy.window.getBounds().getSize().height;
int winW = Jeopardy.window.getBounds().getSize().width;
int width = (winW - ((2 * border) + (5 * gap))) / 6 ;
int height = ((winH - ((4*border) + (5 * gap) + clueHeightDiff ))) / 6;
int clueHeight = height + clueHeightDiff;
int Fx = border + (5* width) + (5*gap);
int foY = border + (2*gap) + (2*height) + clueHeightDiff;
Rectangle F2 = new Rectangle(Fx,foY, width , height);
Graphics2D g2f2 = (Graphics2D) g;
g2f2.draw(F2);
g2f2.fill(F2);
g2f2.setColor(blue);
FontMetrics metrics = g2f2.getFontMetrics(font);
int height = metrics.getHeight();
int width = metrics.stringWidth(text);
Dimension size = new Dimension(width+2, height+2);
Given the X and Y bounds of this box, I need the text to fit comfortably inside. I just can't find a way to do this effectively, as according to the documentation, stringwidth() returns the value of only the first character.