Font font = new Font("Verdana", Font.PLAIN, 10); FontMetrics metrics = new FontMetrics(font) { }; Rectangle2D bounds = metrics.getStringBounds("Hello World!", null); int widthInPixels = (int) bounds.getWidth();
Font myFont = new Font("Arial", Font.PLAIN, 10); FontMetrics myMetrics = new FontMetrics(myFont) {}; Rectangle2D boundsOfString = myMetrics.getStringBounds("Measure this String", null); int width = (int) boundsOfString.getWidth(); int height = (int) boundsOfString.getHeight();