klasscho

DrawUtilits class

May 26th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. package com.packag;
  2.  
  3. import java.awt.*;
  4. import java.awt.font.TextLayout;
  5. import java.awt.geom.Rectangle2D;
  6.  
  7. public class DrawUtils {
  8.  
  9.     private DrawUtils(){}
  10.  
  11.     public static int getMessageWidth(String message, Font font, Graphics2D g){
  12.          g.setFont(font);
  13.          Rectangle2D bounds = g.getFontMetrics().getStringBounds(message, g);
  14.          return(int)bounds.getWidth();
  15.      }
  16.  
  17.     public static int getMessageHeight(String message, Font font, Graphics2D g) {
  18.         g.setFont(font);
  19.         if(message.length() == 0) return 0;
  20.         TextLayout tl = new TextLayout(message, font, g.getFontRenderContext());
  21.         return(int)tl.getBounds().getHeight();
  22.  
  23.     }
  24.  
  25. }
Add Comment
Please, Sign In to add comment