Class VectorIcon

java.lang.Object
org.openide.util.VectorIcon
All Implemented Interfaces:
Serializable, Icon

public abstract class VectorIcon extends Object implements Icon, Serializable
A scalable icon that can be drawn at any resolution, for use with HiDPI displays. Implementations will typically use hand-crafted painting code that may take special care to align graphics to device pixels, and which may perform small tweaks to make the icon look good at all resolutions. The API of this class intends to make this straightforward.

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.

Since:
9.12
See Also:
  • Constructor Details

    • VectorIcon

      protected VectorIcon(int width, int height)
  • Method Details

    • getIconWidth

      public final int getIconWidth()
      Specified by:
      getIconWidth in interface Icon
    • getIconHeight

      public final int getIconHeight()
      Specified by:
      getIconHeight in interface Icon
    • setAntiAliasing

      protected static final void setAntiAliasing(Graphics2D g, boolean enabled)
      Selectively enable or disable antialiasing during painting. Certain shapes may look slightly better without antialiasing, e.g. entirely regular diagonal lines in very small icons when there is no HiDPI scaling. Text antialiasing is unaffected by this setting.
      Parameters:
      g - the graphics to set antialiasing setting for
      enabled - whether antialiasing should be enabled or disabled
    • round

      protected static final int round(double d)
    • paintIcon

      public final void paintIcon(Component c, Graphics g0, int x, int y)
      Specified by:
      paintIcon in interface Icon
    • paintIcon

      protected abstract void paintIcon(Component c, Graphics2D g, int width, int height, double scaling)
      Paint the icon at the given width and height. The dimensions given are the device pixels onto which the icon must be drawn after it has been scaled up from its originally constant logical dimensions and aligned onto the device pixel grid. Painting onto the supplied 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.

      Parameters:
      c - may be used to get properties useful for painting, as in Icon.paintIcon(Component,Graphics,int,int)
      g - need not be cleaned up or restored to its previous state after use; will have anti-aliasing already enabled by default
      width - the target width of the icon, after scaling and alignment adjustments, in device pixels
      height - the target height of the icon, after scaling and alignment adjustments, in device pixels
      scaling - the scaling factor that was used to scale the icon dimensions up to their stated value