1

Actually I have these methods to get the width/height size of a string, both requires the command inside a paint component. But I want to get this values inside a class constructor. This is possible?

public int getStringWidth(Graphics g){
    g2d = (Graphics2D) g;
    metrics = g2d.getFontMetrics(this.font);
    return metrics.stringWidth(this.string);
}

public int getStringHeight(Graphics g){
    g2d = (Graphics2D) g;
    metrics = g2d.getFontMetrics(this.font);
    int height = (int)font.createGlyphVector(metrics.getFontRenderContext(), this.string).getVisualBounds().getHeight();    
    return height;
}

font.createGlyphVector(metrics.getFontRenderContext(), this.string).getVisualBounds().getHeight() it's the best command that I got to precisely calculate the string height size and it needs Graphics g too.

user3437460
  • 17,253
  • 15
  • 58
  • 106
Forsaiken
  • 324
  • 3
  • 12
  • I'm using FontMetrics in this code and needs `Graphics g` to work – Forsaiken May 14 '18 at 17:02
  • I deleted my previous comment which was not clear. See https://stackoverflow.com/questions/1524855/how-to-calculate-the-fonts-width – c0der May 14 '18 at 17:07
  • Yes you can get FontMetrics objects from other objects than Graphics: swing components for example. See https://docs.oracle.com/javase/7/docs/api/java/awt/class-use/FontMetrics.html – Jean-Baptiste Yunès May 14 '18 at 20:13
  • 1
    *"But **I want to get this values inside a class constructor**."* Why? See [What is the XY problem?](http://meta.stackexchange.com/q/66377) – Andrew Thompson May 14 '18 at 22:31
  • @AndrewThompson, the problem is that I have to wait 1 repaint to get the Sprites values, and paintComponent is to draw graphics on the panel and not to initialize graphics, I simplily want to initialize the graphics on the class constructor – Forsaiken May 17 '18 at 00:19
  • Sprites? What sprites? For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 17 '18 at 02:01
  • @AndrewThompson I think that is not needed my code about Sprite to this question, in question about getting properties values of a graphic, Strings is the only one who needs the paintComponent to get width and height and this is the problem, if I initialize a square in class constructor its simples to get the width and height inside the class constructor because I define the numbers, the properties of the strings is generated by the string itself and Java forces me to use the paintComponent to get the properties, got it? – Forsaiken May 17 '18 at 11:11
  • *"I think that is not needed.."* Do you need an ***answer?*** Do you need the close attention of people who ***can*** answer this? Your answers so far do not give me the answer to my question, at least, not enough to motivate me to look closely at the problem. So .. good luck with it. – Andrew Thompson May 17 '18 at 11:40

1 Answers1

0

You can try to create a BufferedImage with the right size and use createGraphics() on it to get an actual Graphics object that is drawable.

BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.createGraphics();
Wolforce
  • 79
  • 4