Java Swing Font Metrics getFontMetrics(JComponent c, Graphics g, Font f)

Here you can find the source of getFontMetrics(JComponent c, Graphics g, Font f)

Description

get Font Metrics

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static FontMetrics getFontMetrics(JComponent c, Graphics g, Font f) 

Method Source Code


//package com.java2s;
/*//from  w  ww. ja va 2  s .  c  om
* Copyright (c) 2002 and later by MH Software-Entwicklung. All Rights Reserved.
*  
* JTattoo is multiple licensed. If your are an open source developer you can use
* it under the terms and conditions of the GNU General Public License version 2.0
* or later as published by the Free Software Foundation.
*  
* see: gpl-2.0.txt
* 
* If you pay for a license you will become a registered user who could use the
* software under the terms and conditions of the GNU Lesser General Public License
* version 2.0 or later with classpath exception as published by the Free Software
* Foundation.
* 
* see: lgpl-2.0.txt
* see: classpath-exception.txt
* 
* Registered users could also use JTattoo under the terms and conditions of the 
* Apache License, Version 2.0 as published by the Apache Software Foundation.
*  
* see: APACHE-LICENSE-2.0.txt
*/

import java.awt.*;
import java.lang.reflect.Method;
import javax.swing.*;

public class Main {
    private static Double javaVersion = null;

    @SuppressWarnings("unchecked")
    public static FontMetrics getFontMetrics(JComponent c, Graphics g, Font f) {
        FontMetrics fm = null;
        if (getJavaVersion() >= 1.6) {
            try {
                Class swingUtilities2Class = Class.forName("sun.swing.SwingUtilities2");
                Class classParams[] = { JComponent.class, Graphics.class, Font.class };
                Method m = swingUtilities2Class.getMethod("getFontMetrics", classParams);
                Object methodParams[] = { c, g, f };
                fm = (FontMetrics) m.invoke(null, methodParams);
            } catch (Exception ex) {
                // Nothing to do
            }
        }
        if (fm == null) {
            if (g == null) {
                if (c != null) {
                    g = c.getGraphics();
                }
            }
            if (g != null) {
                if (f != null) {
                    fm = g.getFontMetrics(f);
                } else {
                    fm = g.getFontMetrics();
                }
            } else if (c != null) {
                if (f != null) {
                    fm = c.getFontMetrics(f);
                } else {
                    fm = c.getFontMetrics(c.getFont());
                }
            }
        }
        return fm;
    }

    public static double getJavaVersion() {
        if (javaVersion == null) {
            try {
                String ver = System.getProperty("java.version");
                String version = "";
                boolean firstPoint = true;
                for (int i = 0; i < ver.length(); i++) {
                    if (ver.charAt(i) == '.') {
                        if (firstPoint) {
                            version += ver.charAt(i);
                        }
                        firstPoint = false;
                    } else if (Character.isDigit(ver.charAt(i))) {
                        version += ver.charAt(i);
                    }
                }
                javaVersion = Double.parseDouble(version);
            } catch (Exception ex) {
                javaVersion = 1.3;
            }
        }
        return javaVersion.doubleValue();
    }
}

Related

  1. getFontMetrics(JComponent c, Graphics g)
  2. getFontMetrics(JComponent c, Graphics g)
  3. getFontMetrics(JComponent c, Graphics g)
  4. getFontMetrics(JComponent c, Graphics g)
  5. getFontMetrics(JComponent c, Graphics g)
  6. getFontMetrics(JComponent c, Graphics g, Font font)
  7. getFRC(JComponent c, FontMetrics fm)
  8. getImage(String text, boolean clockwise, Font font, FontMetrics fm, Color bg, Color fontColor)
  9. getMethodGetFontMetrics()