The example in this article describes a simple method of obtaining string pixels in Java. Share it with everyone for your reference, the details are as follows:
Calculate the pixel length and height of a string:
Graphics2D g = (Graphics2D)Toolkit.getDefaultToolkit().getImage("imgname").getGraphics();//Set a large font Font font = new Font("楷体", Font.ITALIC | Font.BOLD, 72);g .setFont(font);FontRenderContext context = g.getFontRenderContext();// Get the pixel range object of the font Rectangle2D stringBounds = font.getStringBounds("text", context);double fontWidth = stringBounds.getWidth();
I hope this article will be helpful to everyone in Java programming.