Class TermUtil


  • public class TermUtil
    extends java.lang.Object
    Utilities class.
    Author:
    Christopher Mindus
    • Field Summary

      Fields 
      Modifier and TypeFieldDescription
      static java.awt.InsetsINSETS_0
      An easy way to do new Insets(0,0,0,0).
    • Constructor Summary

      Constructors 
      ConstructorDescription
      TermUtil() 
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and TypeMethodDescription
      static java.awt.FontderiveFont​(java.awt.Graphics g, java.awt.Font font, int cxFont, int cyFont)
      Derives a new font from another one by scaling it.
      static java.lang.String[]getAvailableFonts()
      Get available fonts.
      static java.lang.String[]getAvailableMonospacedFonts()
      Get available monospaced fonts.
      static java.awt.FontMetricsgetFontMetrics​(java.awt.Component c, java.awt.Font f)
      Gets the FontMetrics from the component in question or default toolkit if component is null.
      static java.awt.FontMetricsgetFontMetrics​(java.awt.Font f)
      Gets the FontMetrics from the default toolkit.
      static java.awt.Graphics2DgetGraphics()
      Gets a screen compatible Graphics2D instance that is preset for font (text) anti-aliasing and fractional metrics (instead of Integers).
      static java.awt.Graphics2DgetGraphics​(javax.swing.JComponent component)
      Gets the graphics of a JComponent.
      static booleanhasFont​(java.lang.String font)
      Checks if a font is available.
      static voidsetTextAntiAlias​(java.awt.Graphics g, boolean on)
      Sets or removes text anti-aliasing (only when using Java2D).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • INSETS_0

        public static final java.awt.Insets INSETS_0
        An easy way to do new Insets(0,0,0,0).
    • Constructor Detail

      • TermUtil

        public TermUtil()
    • Method Detail

      • getAvailableFonts

        public static java.lang.String[] getAvailableFonts()
        Get available fonts.

        Workaround for JDK 1.7 Bug ID 7083197: Font returns BOLD version if i ask for PLAIN after getAllFonts() call.

        Returns:
        list of available font names or null if error.
      • hasFont

        public static boolean hasFont​(java.lang.String font)
        Checks if a font is available.
        Parameters:
        font - The font name.
        Returns:
        true if font exists, false otherwise.
      • getGraphics

        public static java.awt.Graphics2D getGraphics()
        Gets a screen compatible Graphics2D instance that is preset for font (text) anti-aliasing and fractional metrics (instead of Integers).
        Returns:
        The Graphics2D instance: DO NOT FORGET TO CALL dispose() on it when you are done with it.
      • getGraphics

        public static java.awt.Graphics2D getGraphics​(javax.swing.JComponent component)
        Gets the graphics of a JComponent. If the method JComponent.getGraphics() returns null, the Graphics2D from getGraphics() is returned.

        The Graphics2D instance is preset for font (text) anti-aliasing and fractional metrics (instead of Integers).

        Parameters:
        component - The JComponent.
        Returns:
        The Graphics2D instance: DO NOT FORGET TO CALL dispose() on it when you are done with it.
      • getFontMetrics

        public static java.awt.FontMetrics getFontMetrics​(java.awt.Font f)
        Gets the FontMetrics from the default toolkit. This method is deprecated in the Toolkit implementation, so it may be removed. Use this method for safety.
        Returns:
        The font metrics for the screen.
      • getFontMetrics

        public static java.awt.FontMetrics getFontMetrics​(java.awt.Component c,
                                                          java.awt.Font f)
        Gets the FontMetrics from the component in question or default toolkit if component is null. This method is deprecated in the Toolkit implementation, so it may be removed. Use this method for safety.
        Parameters:
        c - The component, or null for none.
        f - The font.
        Returns:
        The font metrics for the component or the screen.
      • getAvailableMonospacedFonts

        public static java.lang.String[] getAvailableMonospacedFonts()
        Get available monospaced fonts.
        Returns:
        list of available font names or null if error.
      • deriveFont

        public static java.awt.Font deriveFont​(java.awt.Graphics g,
                                               java.awt.Font font,
                                               int cxFont,
                                               int cyFont)
        Derives a new font from another one by scaling it.
        Parameters:
        g - The Graphics2D object.
        font - The font.
        cxFont - The width of the font in pixels.
        cyFont - The height of the font in pixels.
        Returns:
        The new font with the specified size.
      • setTextAntiAlias

        public static void setTextAntiAlias​(java.awt.Graphics g,
                                            boolean on)
        Sets or removes text anti-aliasing (only when using Java2D).
        Parameters:
        g - The graphics.
        on - The flag.