I'm not sure how to ask this, I have a subclass JPanel that draws strings with paintComponent. And I have a class that takes String string, int xPos, int yPos, Font font
as arguments, the object is then drawn with g.drawString(text.getstring, etc...)
.
However, the problem is that when the text class is constructed I would like to calculate the width and height of the text based on the string and font given, but the way to do that seems to be by doing this:
FontMetrics = g.getFontMetrics(Font);
int height = metrics.getHeight();
int width = metrics.stringWidth(string);
the problem is that g has to be the 'Graphics' object from paintComponent, however that doesn't exist in the scope of the class, should I pass it as a parameter to the text class? Or is there a more elegant way of doing this?