Class PaintAlpha


  • public class PaintAlpha
    extends java.lang.Object
    This class contains static methods for the manipulation of objects of type Paint

    The intention is to honour the alpha-channel in the process. PaintAlpha was originally conceived to improve the rendering of 3D Shapes with transparent colours and to allow invisible bars by making them completely transparent.

    Previously Color.darker() was used for this, which always returns an opaque colour.

    Additionally there are methods to control the behaviour and in particular a cloneImage(..) method which is needed to darken objects of type TexturePaint.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static double FACTOR
      Multiplier for the darker Methods.
      (taken from Color.FACTOR)
      private static boolean legacyAlpha  
    • Constructor Summary

      Constructors 
      Constructor Description
      PaintAlpha()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.image.BufferedImage cloneImage​(java.awt.image.BufferedImage image)
      Clone a BufferedImage.
      private static java.awt.Color darker​(java.awt.Color paint)
      Similar to Color.darker().
      private static java.awt.GradientPaint darker​(java.awt.GradientPaint paint)
      Create a new GradientPaint with its colors darkened.
      static java.awt.Paint darker​(java.awt.Paint paint)
      Create a new (if possible, darker) Paint of the same Type.
      private static java.awt.Paint darkerLinearGradientPaint​(java.awt.LinearGradientPaint paint)
      Create a new Gradient with its colours darkened.
      private static java.awt.Paint darkerRadialGradientPaint​(java.awt.RadialGradientPaint paint)
      Create a new Gradient with its colours darkened.
      private static java.awt.TexturePaint darkerTexturePaint​(java.awt.TexturePaint paint)
      Create a new TexturePaint with its colors darkened.
      static boolean setLegacyAlpha​(boolean legacyAlpha)
      Per default PaintAlpha will try to honour alpha-channel information.
      • Methods inherited from class java.lang.Object

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

      • FACTOR

        private static final double FACTOR
        Multiplier for the darker Methods.
        (taken from Color.FACTOR)
        See Also:
        Constant Field Values
      • legacyAlpha

        private static boolean legacyAlpha
    • Constructor Detail

      • PaintAlpha

        public PaintAlpha()
    • Method Detail

      • setLegacyAlpha

        public static boolean setLegacyAlpha​(boolean legacyAlpha)
        Per default PaintAlpha will try to honour alpha-channel information. In the past this was not the case. If you wish legacy functionality for your application you can request this here.
        Parameters:
        legacyAlpha - boolean
        Returns:
        the previous setting
      • darker

        public static java.awt.Paint darker​(java.awt.Paint paint)
        Create a new (if possible, darker) Paint of the same Type. If the Type is not supported, the original Paint is returned.

        Parameters:
        paint - a Paint implementation (e.g. Color, GradientPaint, TexturePaint,..)

        Returns:
        a (usually new, see above) Paint
      • darker

        private static java.awt.Color darker​(java.awt.Color paint)
        Similar to Color.darker().

        The essential difference is that this method maintains the alpha-channel unchanged

        Parameters:
        paint - a Color
        Returns:
        a darker version of the Color
      • darker

        private static java.awt.GradientPaint darker​(java.awt.GradientPaint paint)
        Create a new GradientPaint with its colors darkened.
        Parameters:
        paint - the gradient paint (null not permitted).
        Returns:
        a darker version of the GradientPaint
      • darkerLinearGradientPaint

        private static java.awt.Paint darkerLinearGradientPaint​(java.awt.LinearGradientPaint paint)
        Create a new Gradient with its colours darkened.
        Parameters:
        paint - a LinearGradientPaint
        Returns:
        a darker version of the LinearGradientPaint
      • darkerRadialGradientPaint

        private static java.awt.Paint darkerRadialGradientPaint​(java.awt.RadialGradientPaint paint)
        Create a new Gradient with its colours darkened.
        Parameters:
        paint - a RadialGradientPaint
        Returns:
        a darker version of the RadialGradientPaint
      • darkerTexturePaint

        private static java.awt.TexturePaint darkerTexturePaint​(java.awt.TexturePaint paint)
        Create a new TexturePaint with its colors darkened.

        This entails cloning the underlying BufferedImage, then darkening each color-pixel individually!

        Parameters:
        paint - a TexturePaint
        Returns:
        a darker version of the TexturePaint
      • cloneImage

        public static java.awt.image.BufferedImage cloneImage​(java.awt.image.BufferedImage image)
        Clone a BufferedImage.

        Note: when constructing the clone, the original Color Model Object is reused.
        That keeps things simple and should not be a problem, as all known Color Models
        (IndexColorModel, DirectColorModel, ComponentColorModel) are immutable.

        Parameters:
        image - original BufferedImage to clone
        Returns:
        a new BufferedImage reusing the original's Color Model and containing a clone of its pixels