Java Swing UIManager GUI_unit(double p_GUI_unit, java.awt.Component c)

Here you can find the source of GUI_unit(double p_GUI_unit, java.awt.Component c)

Description

Converts a distance measured in "GUI unit" to pixels.

License

Open Source License

Declaration

public static int GUI_unit(double p_GUI_unit, java.awt.Component c) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2006-2013 AITIA International, Inc.
 * /*w ww.  j  av  a  2 s .  co  m*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

public class Main {
    private static java.awt.Component g_referenceComponent = null;
    private static Integer g_GUI_unit = null;

    /** Converts a distance measured in "GUI unit" to pixels.
     * The "GUI unit" scales automatically with the current font and the
     * monitor's resolution (it is the pixel height of the default JLabel font). 
     */
    public static int GUI_unit(double p_GUI_unit) {
        if (g_GUI_unit == null) {
            g_GUI_unit = getRowHeight(null);
        }
        return (int) Math.round(p_GUI_unit * g_GUI_unit);
    }

    /** Converts a distance measured in "GUI unit" to pixels.
     * The "GUI unit" scales automatically with the current font and the
     * monitor's resolution (it is the pixel height of c's font). 
     */
    public static int GUI_unit(double p_GUI_unit, java.awt.Component c) {
        return (int) Math.round(p_GUI_unit * getRowHeight(c));
    }

    /** Returns the row height (font height) of 'c' of the reference component if 'c'
     *  is null. */
    public static int getRowHeight(java.awt.Component c) {
        java.awt.Font f = (c != null) ? c.getFont() : null;
        if (f == null)
            f = getRefComp().getFont();
        if (f == null)
            f = javax.swing.UIManager.getFont("Label.font");
        return (c != null ? c : getRefComp()).getFontMetrics(f).getHeight();
    }

    /** 
     * Some functions (such as font/screen size calculation) require a component
      * e.g. to access the screen. I call it as 'reference component'.
      * It is recommended to set it to a JLabel/JButton in the main window.
      */
    public static java.awt.Component getRefComp() {
        if (g_referenceComponent == null) {
            g_referenceComponent = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
            if (g_referenceComponent == null)
                g_referenceComponent = new javax.swing.JLabel();
        }
        return g_referenceComponent;
    }
}

Related

  1. getUnfocusedSelectionBackground()
  2. getUnfocusedSelectionForeground()
  3. getUnfocusedSelectionForeground()
  4. getUnfocusedSelectionForeground()
  5. getWindowsVisualStyle()
  6. init()
  7. initComponentHeight(final Component... components)
  8. initGui()
  9. initialize()