Class GradientUtil

java.lang.Object
ca.corbett.extras.gradient.GradientUtil

public class GradientUtil extends Object
Contains some static utility methods for dealing with drawing gradients into images.
Since:
2022-05-10
Author:
scorbo2
  • Constructor Details

    • GradientUtil

      protected GradientUtil()
      Protected constructor to allow subclassing for application-specific utility methods while preventing direct instantiation of this utility class.
  • Method Details

    • createGradientImage

      public static BufferedImage createGradientImage(Gradient conf, int width, int height)
      Creates a new RGB image of the given width and height, and then fills it with the given Gradient.
      Parameters:
      conf - The Gradient to use to fill the image.
      width - The width of the image to generate.
      height - The height of the image to generate.
      Returns:
      A BufferedImage containing the requested gradient.
    • fill

      public static void fill(Gradient conf, Graphics2D graphics, int x1, int y1, int width, int height)
      Renders a gradient fill into the given Graphics2D object using the given Gradient.
      Parameters:
      conf - The Gradient describing the type of gradient to render.
      graphics - A Graphics2D object to receive the gradient. Will not call dispose() here.
      x1 - The x co-ordinate of the upper left area to fill.
      y1 - The y co-ordinate of the upper left area to fill.
      width - The width of the gradient fill.
      height - The height of the gradient fill.
    • drawRect

      public static void drawRect(Gradient conf, Graphics2D graphics, int x1, int y1, int width, int height)
      Draws an unfilled rectangle into the given Graphics2D object using the given Gradient.
      Parameters:
      conf - The Gradient describing the gradient to use.
      graphics - The Graphics2D object to use. Won't dispose() it here.
      x1 - The x value of the left edge of the rectangle.
      y1 - The y value of the top edge of the rectangle.
      width - The pixel width of the rectangle.
      height - The pixel height of the rectangle.
    • drawString

      public static void drawString(Gradient conf, Graphics2D graphics, int textX, int textY, String string)
      Draws the given String into the given Graphics2D object using the given Gradient. The boundaries for the gradient will be determined automatically based on the text extents. If this is unacceptable, use the other drawString() method to set them manually.
      Parameters:
      conf - The Gradient describing the gradient to use.
      graphics - The Graphics2D object to use. Won't call dispose() here.
      textX - The x value of the left of the text.
      textY - The y value of the text baseline.
      string - The string to render.
    • drawString

      public static void drawString(Gradient conf, Graphics2D graphics, int gradientX1, int gradientY1, int gradientX2, int gradientY2, int textX, int textY, String string)
      Draws the given String into the given Graphics2D object using the given Gradient. The gradient bounds will not be inferred from the text! This is because it's quite often nice to be able to set the gradient bounds to a larger area than just the text (else the gradient is less effective). For example, if your image is 500x500 and your text is only 300x40, you might want to gradient bounds to be 0,0 to 499,499 so you can draw the text right in the middle and have it better match the desired gradient of the image. If you don't care about the gradient bounds, you can invoke drawString(Gradient,Graphics2D,int,int) instead and they will be auto-determined based on the size of the text string to draw.

      Note that this allows you to make multiple calls to this method with the same full-sized gradient bounds to write out multiple lines of text one after the other with the same gradient applying to all of them, as opposed to each line of text having its own gradient box calculated according to the text extents of that one line. So, you could do a gradual vertical gradient that spans multiple lines by putting all the lines within one large rectangle (the gradient bounding area).

      Parameters:
      conf - The Gradient describing the gradient to use.
      graphics - The Graphics2D object to use. Won't call dispose() here.
      gradientX1 - The left edge of the text gradient area.
      gradientY1 - The top edge of the text gradient area.
      gradientX2 - The right edge of the text gradient area.
      gradientY2 - The bottom edge of the text gradient area.
      textX - The left edge of the text location.
      textY - The bottom edge of the text location.
      string - The String to render.
    • fillHorizontalGradient

      protected static void fillHorizontalGradient(Graphics2D graphics, int x1, int y1, int x2, int y2, Color color1, Color color2)
    • fillVerticalGradient

      protected static void fillVerticalGradient(Graphics2D graphics, int x1, int y1, int x2, int y2, Color color1, Color color2)
    • fillDiagonal1Gradient

      protected static void fillDiagonal1Gradient(Graphics2D graphics, int x1, int y1, int x2, int y2, Color color1, Color color2)
    • fillDiagonal2Gradient

      protected static void fillDiagonal2Gradient(Graphics2D graphics, int x1, int y1, int x2, int y2, Color color1, Color color2)
    • fillCircularGradient

      protected static void fillCircularGradient(Graphics2D graphics, int x1, int y1, int width, int height, Color color1, Color color2)
    • createCircularGradientPaint

      protected static RadialGradientPaint createCircularGradientPaint(float centerX, float centerY, int width, int height, Color color1, Color color2)