private void paintPattern(Graphics2D g2d, Shapes tilingShapes, Rectangle2D bBoxMod) {

        if (tilingShapes == null) {
            return;
        }

        // store previous state so we can draw bounds
        AffineTransform preAf = g2d.getTransform();

        // apply scale an shear but not translation
        AffineTransform af = g2d.getTransform();
        // todo there is a still a bug here, the call to setToShear
        // wipes out the scale, further investigation is needed.
        if (matrix.getScaleX() != 0.0 && matrix.getScaleY() != 0.0) {
            af.scale(matrix.getScaleX(), Math.abs(matrix.getScaleY()));
        }
        af.shear(matrix.getShearX(), matrix.getShearY());
        g2d.transform(af);
        // pain the key pattern
        tilingShapes.paint(g2d);

        // if we have a shear then we need to pad out the pattern
        // so we can fill a pattern paint buffer.
        if (matrix.getShearX() > 0 ||
                matrix.getShearY() > 0) {
            g2d.translate(bBoxMod.getWidth(), 0);
            tilingShapes.paint(g2d);
            g2d.translate(0, -bBoxMod.getHeight());
            tilingShapes.paint(g2d);
            g2d.translate(-bBoxMod.getWidth(), 0);
            tilingShapes.paint(g2d);
            g2d.translate(-bBoxMod.getWidth(), 0);
            tilingShapes.paint(g2d);
            g2d.translate(0, bBoxMod.getHeight());
            tilingShapes.paint(g2d);
            g2d.translate(0, bBoxMod.getHeight());
            tilingShapes.paint(g2d);
            g2d.translate(bBoxMod.getWidth(), 0);
            tilingShapes.paint(g2d);
            g2d.translate(bBoxMod.getWidth(), 0);
            tilingShapes.paint(g2d);
            // highlight key square.
//            g2d.translate(-bBoxMod.getWidth(), -bBoxMod.getHeight());
//            g2d.setColor(Color.red);
//            g2d.draw(bBoxMod);
        }
        g2d.setTransform(preAf);
    }