Class OptimizedShapeRendering

java.lang.Object
swingtree.api.laf.OptimizedShapeRendering

public final class OptimizedShapeRendering extends Object
A utility class for optimized shape filling, the way the SwingTree style engine does it for its own surfaces: antialiasing stays on wherever the outline actually curves, and is switched off everywhere else.
It optimizes shape filling specifically by turning antialiasing off strategically for parts which do not need it because the result looks identical.

Filling anti-aliased shapes can be extremely slow. This is because a Graphics2D rasterizer asked to antialias computes a computest multiple pixels internal for every visual pixel of a shape, including the great majority which lie well inside it and come out fully covered. Over a rounded rectangle the size of a scroll bar's thumb or a progress bar's fill, that is a few hundred pixels of genuine curve and tens of thousands of pixels of arithmetic arriving at "opaque". A fill without antialiasing on the other hand writes those directly: So much, much faster!

Support for such optimized routines is worth having in a custom look and feel because a ComponentUI still needs to draw most of the visible primitives like scroll bars itself (which SwingTree will not do for you).

See Also:
  • Method Details

    • fill

      public static void fill(Graphics2D g2d, Shape shape)
      Fills a shape with the graphics context's current paint, in as few antialiased pixels as the shape allows, writing exactly the pixels Graphics2D.fill(Shape) would write.

      Two kinds of shape get a faster treatment:

      • A Rectangle is integer valued by its very type and so has no soft edge anywhere. Only the transform could still put one between two pixels, which is why that is checked before filling it in one go, without antialiasing.
      • A RoundRectangle2D curves only inside its four corner boxes, so it is filled as antialiasing-free bands plus antialiased corners.
      Anything else, like a fractional Rectangle2D or a rotated transform, keeps antialiasing and is filled in one go.
      Parameters:
      g2d - the graphics context to fill on, whose paint, clip and transform are all used and none of which are changed by this call
      shape - the shape to fill
    • fillWithoutAntialiasing

      public static void fillWithoutAntialiasing(Graphics2D g2d, Shape... shapes)
      Fills the given shapes with antialiasing switched off and then switches it back on.
      Parameters:
      g2d - the graphics context to fill on
      shapes - the shapes to fill, none of which has a soft edge worth computing