public final class AWTUtil extends Object
Modifier and Type | Field and Description |
---|---|
static String[] |
GENERIC_FONT_FAMILIES
An array containing the 3 following generic font families:
SansSerif, Serif, Monospaced.
|
static boolean |
isHeadless
Returns
true if the computer has a display,
keyboard and mouse; false otherwise. |
Modifier and Type | Method and Description |
---|---|
static int |
checkDisplayScaling(int scaling)
Checks that specified display scaling is consistent with the
the system's display scaling and with what is supported by XXE.
|
static Color |
convertToGray(Color c,
float increment,
boolean createContrast)
Returns specified color converted to gray,
possibly after changing its brightness.
|
static Color |
editHSB(Color color,
int h,
float incrS,
float incrB,
boolean createContrast)
Modify the hue, saturation and brightness of specified color.
|
static Color |
fromHexString(String spec)
Returns the color specified using its hexadecimal string
representation #RRGGBB.
|
static int |
getDisplayScaling()
Returns the display scaling of the main screen of the computer.
|
static String[] |
getFontFamilies()
Equivalent to
getFontFamilies(Locale.getDefault()) , except that the resulting list
is cached. |
static String[] |
getFontFamilies(Locale locale)
Returns the list of all font family names, localized for
specified locale, available on this platform.
|
static Image |
getScaledImage(Image image,
int width,
int height,
boolean smooth)
Returns a scaled copy of specified image.
|
static double |
getScreenResolution()
Returns the resolution in DPI of the main screen of the computer.
|
static int |
getSystemDisplayScaling()
Returns the display scaling of the main screen of the computer as
set by the system.
|
static int[] |
getUnscaledImageSize(Image image)
If specified image has been created using
getScaledImage(java.awt.Image, int, int, boolean) ,
return the original dimensions of the image in the form of an array
of 2 int s containing the image width and image height
in pixels. |
static boolean |
hasDrawThinRectBug()
Not part of the public documented API: Java 1.8 on Retina Mac:
when drawing a thin rectangle, that is, using
a 2 physical pixels line with, 1 physical pixel top and/or left lines
are drawn outside the boundaries of the rectangle.
|
static boolean |
hasDrawUnscaledImageBug()
Not part of the public documented API: Java 1.9 on Windows HiDPI:
unscaled images are drawn by Java at 150% to 300% of their normal size
using a low-quality interpolation algorithm.
|
static double |
px(double pixels)
Converts specified virtual pixels to screen pixels.
|
static float |
px(float pixels)
Converts specified virtual pixels to screen pixels.
|
static int |
px(int pixels)
Converts specified virtual pixels to screen pixels.
|
static Font |
pxDeriveFont(Font font,
int style,
int delta)
Creates a font derived from specified font taking into account
the display scaling if any.
|
static int |
pxFontSize(Font font,
int delta)
Computes the size of the font returned by
pxDeriveFont(java.awt.Font, int, int) . |
static int |
setDisplayScaling(int scaling)
Allows to set the value returned by
getDisplayScaling() . |
static void |
setFocusable(Component component,
boolean focusable)
Recursively invokes
setFocusable on specified Component
and all its descendants (if specified Component is a Container). |
static void |
setLineAntialiasing(Graphics g,
boolean antialias)
Convenience methods: allows to turn on and off line
antialiasing in specified
Graphics . |
static void |
setTextAntialiasing(Graphics g)
Configures text antialiasing in specified
Graphics
in the same way as native desktop applications. |
static Color |
toBorderColor(Color backgroundColor)
Returns specified color after changing its brightness to make it suitable
for painting a border.
|
static BufferedImage |
toBufferedImage(Image original)
Converts specified image to an ARGB BufferedImage.
|
static BufferedImage |
toBufferedImage(Image original,
int convertedType)
Converts specified image to a BufferedImage having specified type
(
BufferedImage.TYPE_INT_RGB , BufferedImage.TYPE_INT_ARGB ,
etc). |
static Color |
toDarkShadowColor(Color backgroundColor)
Returns specified color after changing its brightness to make it suitable
for painting a dark shadow.
|
static String |
toHexString(Color color)
Returns the hexadecimal string representation #RRGGBB of
specified color.
|
static Color |
toHighlightColor(Color backgroundColor)
Returns specified color after changing its brightness to make it suitable
for painting a highlight.
|
static Color |
toSeparatorColor(Color backgroundColor)
Returns specified color after changing its brightness to make it suitable
for painting a separator.
|
static Color |
toShadowColor(Color backgroundColor)
Returns specified color after changing its brightness to make it suitable
for painting a shadow.
|
static void |
union(Rectangle u,
Rectangle r)
Updates first rectangle to be the union of specified rectangles.
|
static double |
vpx(double pixels)
Converts specified screen pixels to virtual pixels.
|
static int |
vpx(int pixels)
Converts specified screen pixels to virtual pixels.
|
public static final boolean isHeadless
true
if the computer has a display,
keyboard and mouse; false
otherwise.public static final String[] GENERIC_FONT_FAMILIES
public static int setDisplayScaling(int scaling)
getDisplayScaling()
.
Specified value is checked using checkDisplayScaling(int)
.
scaling
- user-specified display scalinggetDisplayScaling()
,
possibly different from specified valuepublic static int checkDisplayScaling(int scaling)
scaling
- user-specified display scalingpublic static int getSystemDisplayScaling()
getDisplayScaling()
public static int getDisplayScaling()
It's a percentage. 100% means no display scaling.
A negative value means that Java uses virtual pixels. Example: -200 on a Retina Mac.
A positive value greater than 100 means that Java still uses physical (screen) pixels and that the client code needs to upscale images, layouts, etc. Example: 150 on a HiDPI Windows.
public static double getScreenResolution()
This resolution is 96 on the Mac, Retina or not and 96, 144, 192, etc, on Windows depending on the DPI scale factor (100%, 150%, 200%, etc).
public static int px(int pixels)
getDisplayScaling()
public static float px(float pixels)
getDisplayScaling()
public static double px(double pixels)
getDisplayScaling()
public static int vpx(int pixels)
This is the inverse operation of px(int)
getDisplayScaling()
public static double vpx(double pixels)
This is the inverse operation of px(double)
getDisplayScaling()
public static Font pxDeriveFont(Font font, int style, int delta)
font
- the font to be derivedstyle
- the style of the derived font,
for example Font.PLAIN
delta
- size difference in points: use a positive int to create
a larger font and a negative int to create a smaller fontpxFontSize(java.awt.Font, int)
public static int pxFontSize(Font font, int delta)
pxDeriveFont(java.awt.Font, int, int)
.font
- the font to be deriveddelta
- size difference in points: use a positive int to create
a larger font and a negative int to create a smaller fontpublic static boolean hasDrawThinRectBug()
public static boolean hasDrawUnscaledImageBug()
public static void setTextAntialiasing(Graphics g)
Graphics
in the same way as native desktop applications.public static void setLineAntialiasing(Graphics g, boolean antialias)
Graphics
.public static void union(Rectangle u, Rectangle r)
public static Image getScaledImage(Image image, int width, int height, boolean smooth)
image
- source imagewidth
- desired widthheight
- desired heightsmooth
- if true
favor quality over speedgetUnscaledImageSize(java.awt.Image)
public static int[] getUnscaledImageSize(Image image)
getScaledImage(java.awt.Image, int, int, boolean)
,
return the original dimensions of the image in the form of an array
of 2 int
s containing the image width and image height
in pixels.public static BufferedImage toBufferedImage(Image original)
public static BufferedImage toBufferedImage(Image original, int convertedType)
BufferedImage.TYPE_INT_RGB
, BufferedImage.TYPE_INT_ARGB
,
etc).public static void setFocusable(Component component, boolean focusable)
setFocusable
on specified Component
and all its descendants (if specified Component is a Container).public static String[] getFontFamilies()
getFontFamilies(Locale.getDefault())
, except that the resulting list
is cached.public static String[] getFontFamilies(Locale locale)
locale
- the desired locale. null
means: default.public static String toHexString(Color color)
fromHexString(java.lang.String)
public static Color fromHexString(String spec)
null
if specification cannot be parsed.toHexString(java.awt.Color)
public static Color editHSB(Color color, int h, float incrS, float incrB, boolean createContrast)
Examples:
editHSB(panel_bg, 240, 0.3F, -0.4F, true)
or editHSB(list_bg, 240, 0.3F, -0.5F, true)
.
editHSB(panel_bg, 240, 0.5F, -0.2F, true)
.
editHSB(list_bg, 60, 0.1F, 0.05, true)
.
color
- color to be modifiedh
- new hue, a number between 0 and 360 (degrees).
Examples: blue=240deg, yellow=60deg.
Specify -1 to preserve hue.
incrS
- saturation increment, a number between -1 and 1.
A negative value means less saturated.
A positive value means more saturated.
Specify 0 to preserve saturation.
incrB
- brightness increment, a number between -1 and 1.
A negative value means darker.
A positive value means brighter.
Specify 0 to preserve brightness.
createContrast
- if this option is true
,
public static Color toHighlightColor(Color backgroundColor)
public static Color toShadowColor(Color backgroundColor)
public static Color toDarkShadowColor(Color backgroundColor)
public static Color toBorderColor(Color backgroundColor)
public static Color toSeparatorColor(Color backgroundColor)
public static Color convertToGray(Color c, float increment, boolean createContrast)
c
- color to be converted to grayincrement
- brightness increment, a number between -1 and 1.
A negative value means darker. A positive value means brighter.createContrast
- if this option is true
,