public abstract class VectorIcon extends Object implements Icon, Serializable
HiDPI support now exists on MacOS, Windows, and Linux. On MacOS, scaling is 200% for Retina displays, while on Windows 10, the "Change display settings" panel provides the options 100%, 125%, 150%, 175%, 200%, and 225%, as well as the option to enter an arbitrary scaling factor. Non-integral scaling factors can lead to various alignment problems that makes otherwise well-aligned icons look unsharp; this class takes special care to avoid such problems.
Hand-crafted painting code is a good design choice for icons that are simple, ubiqutious in the UI (e.g. part of the Look-and-Feel), or highly parameterized. Swing's native Windows L&F uses this approach for many of its basic icons; see WindowsIconFactory.
When developing new icons, or adjusting existing ones, use the VectorIconTester
utility found in
o.n.swing.tabcontrol/test/unit/src/org/netbeans/swing/tabcontrol/plaf/VectorIconTester.java
to preview and compare icons at different resolutions.
Modifier | Constructor and Description |
---|---|
protected |
VectorIcon(int width,
int height) |
Modifier and Type | Method and Description |
---|---|
int |
getIconHeight() |
int |
getIconWidth() |
protected abstract void |
paintIcon(Component c,
Graphics2D g,
int width,
int height,
double scaling)
Paint the icon at the given width and height.
|
void |
paintIcon(Component c,
Graphics g0,
int x,
int y) |
protected static int |
round(double d) |
protected static void |
setAntiAliasing(Graphics2D g,
boolean enabled)
Selectively enable or disable antialiasing during painting.
|
public final int getIconWidth()
getIconWidth
in interface Icon
public final int getIconHeight()
getIconHeight
in interface Icon
protected static final void setAntiAliasing(Graphics2D g, boolean enabled)
g
- the graphics to set antialiasing setting forenabled
- whether antialiasing should be enabled or disabledprotected static final int round(double d)
protected abstract void paintIcon(Component c, Graphics2D g, int width, int height, double scaling)
Graphics2D
instance using whole number coordinates (for horizontal and vertical
lines) will encourage sharp and well-aligned icons.
The icon should be painted with its upper left-hand corner at position (0, 0). Icons need not be opaque. Due to rounding errors and alignment correction, the aspect ratio of the device dimensions supplied here may not be exactly the same as that of the logical pixel dimensions specified in the constructor.
c
- may be used to get properties useful for painting, as in
Icon.paintIcon(Component,Graphics,int,int)
width
- the target width of the icon, after scaling and alignment adjustments, in device
pixelsheight
- the target height of the icon, after scaling and alignment adjustments, in
device pixelsscaling
- the scaling factor that was used to scale the icon dimensions up to their
stated valueg
- need not be cleaned up or restored to its previous state after use; will
have anti-aliasing already enabled by default