Class PDFPattern

  • All Implemented Interfaces:
    java.awt.Paint, java.awt.Transparency

    public final class PDFPattern
    extends java.lang.Object
    implements java.awt.Paint

    Create a new "pattern" fill to paint with. A PDFPattern can be passed into the PDFStyle.setFillColor(java.awt.Paint) method to cause any shapes or text painted with that style to be filled with the specified pattern.

    Patterns can either be created by passing in a PDFCanvas, or by loading a named duotone or color pattern from a file (duotone patterns require a background and foreground color to be specified, whereas color patterns already include this information).

    Several patterns are supplied with the default distribution. They include

    BrickA "brick-wall" pattern
    CheckA chequered-square pattern
    GridA grid pattern
    SpotA spot pattern, like the half-toning pattern used in newspapers
    StarA 5-pointed star pattern
    StripexxxA stripe pattern. xxx is the angle of the strripes - eg. "000" for vertical stripes, "045" or "060" for diagonal and so on

    Users may create their own patterns and store them in a file for use by the library. The file is a standard PropertyResourceBundle file, with the following attributes (all mandatory):

    • type must be the word "color" for color patterns or "duotone" for duotone patterns
    • width is the width of the pattern before it repeats, as a float
    • height is the height of the pattern before it repeats, as a float
    • action is the raw PDF graphics state instructions to write to the stream. So, for example, "1 1 1 rg 0 0 300 200 re f 1 0 0 rg 0 100 300 200 re f" would draw the Indonesian flag (it's a simple example). Duotone patterns should not include any color commands, whereas color patterns must set the fill and/or line colors before they're used.

    This file should then be stored as part of a package, eg "com/yourcompany/yourpattern.properties". The resource name can then be passed into the PDFPattern constructor, eg.

       PDFPattern pattern = new PDFPattern("com.yourcompany.yourpattern", 0, 0, 300, 200);
     
    Since:
    2.0
    • Field Summary

      • Fields inherited from interface java.awt.Transparency

        BITMASK, OPAQUE, TRANSLUCENT
    • Constructor Summary

      Constructors 
      Constructor Description
      PDFPattern​(java.lang.String pattern, float x1, float y1, float x2, float y2, java.awt.Color fg, java.awt.Color bg)
      Create a PDFPattern from a known, named duotone pattern.
      PDFPattern​(PDFCanvas canvas, float x1, float y1, float x2, float y2)
      Create a pattern from the specified PDFCanvas.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      PDFPattern brightnessClone​(float delta, float min, float max)  
      java.awt.PaintContext createContext​(java.awt.image.ColorModel cm, java.awt.Rectangle deviceBounds, java.awt.geom.Rectangle2D userBounds, java.awt.geom.AffineTransform xform, java.awt.RenderingHints hints)
      Required as part of the Paint contract.
      java.awt.Paint getShaded​(float delta, float min, float max)
      This method is required when painting patterns using the graph library.
      int getTransparency()
      Required as part of the Paint contract.
      void setTransform​(java.awt.geom.AffineTransform transform)
      Set the transform to use for this pattern.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • PDFPattern

        public PDFPattern​(PDFCanvas canvas,
                          float x1,
                          float y1,
                          float x2,
                          float y2)
        Create a pattern from the specified PDFCanvas. The two parameters specify where on the page to start tiling the canvas from.
        Parameters:
        canvas - the PDFCanvas to use as the source of the pattern
        x1 - the left edge of the patterns reference tile on the page
        y1 - the bottom edge of the patterns reference tile on the page
        x2 - the left edge of the patterns reference tile on the page
        y2 - the bottom edge of the patterns reference tile on the page
      • PDFPattern

        public PDFPattern​(java.lang.String pattern,
                          float x1,
                          float y1,
                          float x2,
                          float y2,
                          java.awt.Color fg,
                          java.awt.Color bg)
                   throws java.util.MissingResourceException

        Create a PDFPattern from a known, named duotone pattern. The pattern will be resized so that the rectangle (x1, y1, x2, y2) on the page will contain one complete copy of the pattern - drawing it outside this rectangle will tile it appropriately. For simpler patterns that don't have to be positioned exactly, often x1 and y1 are 0, and x2 and y2 specify your choice for the width and height of the pattern.

        Patterns created with this constructor are two-color or "duotone" patterns. A background and foreground color must be supplied, which may be translucent

        Parameters:
        pattern - the name of the pattern to load - either one of the predefined patterns (see above), or the fully-qualified name of a PropertyResourceBundle to load.
        x1 - the left edge of the patterns reference tile on the page
        y1 - the bottom edge of the patterns reference tile on the page
        x2 - the right edge of the patterns reference tile on the page
        y2 - the top edge of the patterns reference tile on the page
        fg - the foreground color to use
        bg - the background color to use
        Throws:
        java.util.MissingResourceException - if the specified pattern cannot be found.
    • Method Detail

      • getTransparency

        public int getTransparency()
        Required as part of the Paint contract. Returns Transparency.OPAQUE.
        Specified by:
        getTransparency in interface java.awt.Transparency
      • createContext

        public java.awt.PaintContext createContext​(java.awt.image.ColorModel cm,
                                                   java.awt.Rectangle deviceBounds,
                                                   java.awt.geom.Rectangle2D userBounds,
                                                   java.awt.geom.AffineTransform xform,
                                                   java.awt.RenderingHints hints)
        Required as part of the Paint contract. This method is only needed when drawing a Paint to an AWT canvas, so it's not implemented and throws an error if called.
        Specified by:
        createContext in interface java.awt.Paint
      • setTransform

        public void setTransform​(java.awt.geom.AffineTransform transform)
        Set the transform to use for this pattern.
        Parameters:
        transform - the transform to apply when painting this pattern
        Since:
        2.23.3
      • getShaded

        public java.awt.Paint getShaded​(float delta,
                                        float min,
                                        float max)
        This method is required when painting patterns using the graph library. It should not be called by the user
      • brightnessClone

        public PDFPattern brightnessClone​(float delta,
                                          float min,
                                          float max)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object