Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java swing draw centered text


public void centerString(Graphics g, Rectangle r, String s, 
        Font font) {
    FontRenderContext frc = 
            new FontRenderContext(null, true, true);

    Rectangle2D r2D = font.getStringBounds(s, frc);
    int rWidth = (int) Math.round(r2D.getWidth());
    int rHeight = (int) Math.round(r2D.getHeight());
    int rX = (int) Math.round(r2D.getX());
    int rY = (int) Math.round(r2D.getY());

    int a = (r.width / 2) - (rWidth / 2) - rX;
    int b = (r.height / 2) - (rHeight / 2) - rY;

    g.setFont(font);
    g.drawString(s, r.x + a, r.y + b);
}
Comment

PREVIOUS NEXT
Code Example
Java :: Palindrome Program in Java. 
Java :: java create array with values 
Java :: measure view height android 
Java :: get week number from localdate java 
Java :: search 
Java :: what is a package in java 
Java :: stringbuilder with delimiter java 
Java :: command to create a Hashmap in Java 
Java :: polymorphism in java 
Java :: android studio int ot string 
Java :: Java void Keyword 
Java :: index 1 out of bound for length 1 java 
Java :: list of java 
Java :: find first and last position of element in sorted array 
Java :: java infinity 
Java :: java boolean data type 
Java :: Java Create a Scanner Object in Java 
Java :: add random numbers to array 
Java :: what is jar file in java 
Java :: java to kotlin converter android studio 
Java :: find minimum element in a sorted and rotated array 
Java :: java first index of an arraylist 
Java :: variable might not have been initialized error 
Java :: calculating the percentile in java 
Java :: vim yank to clipboard 
Java :: jenkins password decrypt online 
Java :: move gif in wave motion java 
Java :: android java how to show progressdialog in fragment 
Java :: Marging ViewPager 
Java :: power of a number in java 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =