Package ej.drawing

Class TransformPainter


  • public class TransformPainter
    extends Object
    The TransformPainter class offers a set of static methods to render elements (image, renderable character) with a transformation.

    The transformation (rotate, scale etc.) is applied during the drawing: the source element is not updated (for instance the source image is not rotated: this is its rendering on the graphics context which is rotated).

    • Constructor Detail

      • TransformPainter

        public TransformPainter()
    • Method Detail

      • drawFlippedImage

        public static void drawFlippedImage​(GraphicsContext gc,
                                            Image image,
                                            int x,
                                            int y,
                                            TransformPainter.Flip flip,
                                            int alpha)
        Draws the image in the graphics context at given anchor top-left position, using the given flip and opacity level.

        In addition with drawFlippedImage(GraphicsContext, Image, int, int, Flip), this method allows to specify the global opacity value to apply during the image rendering.

        Parameters:
        gc - the graphics context where to render the drawing.
        image - the image to draw
        x - the x coordinate of the image reference anchor top-left point
        y - the y coordinate of the image reference anchor top-left point
        flip - the flip to apply.
        alpha - the global opacity rendering value.
        Throws:
        IllegalArgumentException - if the given alpha is not a value between GraphicsContext.TRANSPARENT and GraphicsContext.OPAQUE.
      • drawDeformedImage

        public static void drawDeformedImage​(GraphicsContext gc,
                                             Image image,
                                             int x,
                                             int y,
                                             int[] xys)
        Draws a deformed image at the given anchor top-left point.

        The deformed image is identified by its four corner points. These points are defined by the array of integer coordinates and they must respect the following order: first is the top-left corner, second is the top-right, third is the bottom-right and fourth is the bottom-left.

        Examples with img an image and imgWidth and imgHeight its size.
        • To draw normal img, the array should be : {0, 0, imgWidth-1, 0, imgWidth-1, imgHeight-1, 0, imgHeight-1}.
        • To draw img with a rotation clockwise by 90 degrees, the array should be : {imgHeight-1, 0, imgHeight-1, imgWidth-1, 0, imgWidth-1, 0, 0}.
        • To draw img mirrored about the vertical axis, the array should be : {0, 0, -(imgWidth-1), 0, -(imgWidth-1), -(imgHeight-1), 0, -(imgHeight-1)}.
        • To draw img with a double scale, the array should be : {0, 0, (imgWidth-1)*2, 0, (imgWidth-1)*2, (imgHeight-1)*2, 0, (imgHeight-1)*2}.
        Parameters:
        gc - the graphics context where to render the drawing.
        image - the image to draw
        x - the x coordinate of the anchor top-left point
        y - the y coordinate of the anchor top-left point
        xys - the array of coordinates : x1,y1,x2,y2,x3,y3,x4,y4.
        Throws:
        IllegalArgumentException - if the @{code xys} length is different than 2*4.
      • drawRotatedImageBilinear

        public static void drawRotatedImageBilinear​(GraphicsContext gc,
                                                    Image image,
                                                    int x,
                                                    int y,
                                                    int rx,
                                                    int ry,
                                                    float angle)
        Draws the image applying the given rotation. This method uses the bilinear algorithm to render the content. This algorithm performs better rendering than nearest neighbor algorithm but it is slower to apply.

        The rotation is specified by the center and the angle. The reference point is the graphical object top-left corner. The rotation point is relative to the current translation of the graphics context where the graphical object will be drawn.

        To rotate an image on itself, use the following lines:

        int rx = x + image.getWidth() / 2;
        int ry = y + image.getHeight() / 2;
        TransformPainter.drawRotatedImageBilinear(gc, image, x, y, rx, ry, 78);

        To rotate an image around a circle, use the following lines:

        for (int i = 0; i < 360; i += 45) {
        TransformPainter.drawRotatedImageBilinear(gc, image, rx - diameter / 2, ry - diameter / 2, rx, ry, i);
        }

        Equivalent to calling drawRotatedImageBilinear(GraphicsContext, Image, int, int, int, int, float, int) with GraphicsContext.OPAQUE as alpha.

        Parameters:
        gc - the graphics context where to render the drawing.
        image - the image to draw.
        x - the x coordinate of the image reference anchor top-left point
        y - the y coordinate of the image reference anchor top-left point
        rx - the rotation center X coordinate.
        ry - the rotation center Y coordinate.
        angle - the rotation angle.
        See Also:
        drawRotatedImageNearestNeighbor(GraphicsContext, Image, int, int, int, int, float)
      • drawScaledImageBilinear

        public static void drawScaledImageBilinear​(GraphicsContext gc,
                                                   Image image,
                                                   int x,
                                                   int y,
                                                   float factorX,
                                                   float factorY)
        Draws the image in the graphics context at given anchor top-left position and using the given scaling factor. This method uses the bilinear algorithm to render the image. This algorithm performs better rendering than nearest neighbor algorithm but it is slower to apply.

        Equivalent to calling drawScaledImageBilinear(GraphicsContext, Image, int, int, float, float, int) with GraphicsContext.OPAQUE as alpha.

        Parameters:
        gc - the graphics context where to render the drawing.
        image - the image to draw
        x - the x coordinate of the image reference anchor top-left point
        y - the y coordinate of the image reference anchor top-left point
        factorX - the scaling X factor.
        factorY - the scaling Y factor.
        Throws:
        IllegalArgumentException - if a factor is lower than to zero.
      • drawScaledImageBilinear

        public static void drawScaledImageBilinear​(GraphicsContext gc,
                                                   Image image,
                                                   int x,
                                                   int y,
                                                   float factorX,
                                                   float factorY,
                                                   int alpha)
        Draws the image in the graphics context at given anchor top-left position and using the given scaling factor and opacity level. This method uses the bilinear algorithm to render the image. This algorithm performs better rendering than nearest neighbor algorithm but it is slower to apply.

        In addition with drawScaledImageBilinear(GraphicsContext, Image, int, int, float, float), this method allows to specify the global opacity value to apply during the image rendering.

        Parameters:
        gc - the graphics context where to render the drawing.
        image - the image to draw
        x - the x coordinate of the image reference anchor top-left point
        y - the y coordinate of the image reference anchor top-left point
        factorX - the scaling X factor.
        factorY - the scaling Y factor.
        alpha - the global opacity rendering value.
        Throws:
        IllegalArgumentException - if the given alpha is not a value between GraphicsContext.TRANSPARENT and GraphicsContext.OPAQUE or if a factor is lower than to zero.
      • drawScaledImageNearestNeighbor

        public static void drawScaledImageNearestNeighbor​(GraphicsContext gc,
                                                          Image image,
                                                          int x,
                                                          int y,
                                                          float factorX,
                                                          float factorY)
        Draws the image in the graphics context at given anchor top-left position and using the given scaling factor. This method uses the nearest neighbor algorithm to render the image. This algorithm is faster than bilinear algorithm but its rendering is more simple.

        Equivalent to calling drawScaledImageNearestNeighbor(GraphicsContext, Image, int, int, float, float, int) with GraphicsContext.OPAQUE as alpha.

        Parameters:
        gc - the graphics context where to render the drawing.
        image - the image to draw
        x - the x coordinate of the image reference anchor top-left point
        y - the y coordinate of the image reference anchor top-left point
        factorX - the scaling X factor.
        factorY - the scaling Y factor.
        Throws:
        IllegalArgumentException - if a factor is lower than to zero.
      • drawScaledImageNearestNeighbor

        public static void drawScaledImageNearestNeighbor​(GraphicsContext gc,
                                                          Image image,
                                                          int x,
                                                          int y,
                                                          float factorX,
                                                          float factorY,
                                                          int alpha)
        Draws the image in the graphics context at given anchor top-left position and using the given scaling factor and opacity level. This method uses the nearest neighbor algorithm to render the image. This algorithm is faster than bilinear algorithm but its rendering is more simple.

        In addition with drawScaledImageNearestNeighbor(GraphicsContext, Image, int, int, float, float), this method allows to specify the global opacity value to apply during the image rendering.

        Parameters:
        gc - the graphics context where to render the drawing.
        image - the image to draw
        x - the x coordinate of the image reference anchor top-left point
        y - the y coordinate of the image reference anchor top-left point
        factorX - the scaling X factor.
        factorY - the scaling Y factor.
        alpha - the global opacity rendering value.
        Throws:
        IllegalArgumentException - if the given alpha is not a value between GraphicsContext.TRANSPARENT and GraphicsContext.OPAQUE or if a factor is lower than to zero.
      • drawScaledCharBilinear

        public static void drawScaledCharBilinear​(GraphicsContext gc,
                                                  char character,
                                                  Font font,
                                                  int x,
                                                  int y,
                                                  float factorX,
                                                  float factorY)
        Draws the character in the graphics context at given anchor top-left position and using the given scaling factor. This method uses the bilinear algorithm to render the character.

        This method only supports characters in the range U+0000 to U+FFFF. It does not handle characters with code points greater than U+FFFF, which are represented as a pair of char values (referred to as "surrogate pair").

        Parameters:
        gc - the graphics context where to render the drawing.
        character - the character to draw
        font - the font to use.
        x - the x coordinate of the character reference anchor top-left point
        y - the y coordinate of the character reference anchor top-left point
        factorX - the scaling X factor.
        factorY - the scaling Y factor.
        Throws:
        IllegalArgumentException - if a factor is lower than to zero.
      • drawScaledStringBilinear

        public static void drawScaledStringBilinear​(GraphicsContext gc,
                                                    String string,
                                                    Font font,
                                                    int x,
                                                    int y,
                                                    float factorX,
                                                    float factorY)
        Draws the string in the graphics context at given anchor top-left position and using the given scaling factor. This method uses the bilinear algorithm to render the string.
        Parameters:
        gc - the graphics context where to render the drawing.
        string - the string to draw
        font - the font to use.
        x - the x coordinate of the character reference anchor top-left point
        y - the y coordinate of the character reference anchor top-left point
        factorX - the scaling X factor.
        factorY - the scaling Y factor.
        Throws:
        IllegalArgumentException - if a factor is lower than to zero.
      • drawScaledSubstringBilinear

        public static void drawScaledSubstringBilinear​(GraphicsContext gc,
                                                       String string,
                                                       int offset,
                                                       int length,
                                                       Font font,
                                                       int x,
                                                       int y,
                                                       float factorX,
                                                       float factorY)
        Draws the substring in the graphics context at given anchor top-left position and using the given scaling factor. This method uses the bilinear algorithm to render the string.
        Parameters:
        gc - the graphics context where to render the drawing.
        string - the string to draw
        offset - the index of the first character of the string.
        length - the number of characters of the string.
        font - the font to use.
        x - the x coordinate of the character reference anchor top-left point
        y - the y coordinate of the character reference anchor top-left point
        factorX - the scaling X factor.
        factorY - the scaling Y factor.
        Throws:
        IllegalArgumentException - if a factor is lower than to zero.
        StringIndexOutOfBoundsException - if the given offset and length do not specify a valid range within the given string.
      • drawScaledRenderableStringBilinear

        public static void drawScaledRenderableStringBilinear​(GraphicsContext gc,
                                                              RenderableString string,
                                                              int x,
                                                              int y,
                                                              float factorX,
                                                              float factorY)
        Draws the renderable string in the graphics context at given anchor top-left position and using the given scaling factor. This method uses the bilinear algorithm to render the renderable string.
        Parameters:
        gc - the graphics context where to render the drawing.
        string - the renderable string to draw
        x - the x coordinate of the character reference anchor top-left point
        y - the y coordinate of the character reference anchor top-left point
        factorX - the scaling X factor.
        factorY - the scaling Y factor.
        Throws:
        IllegalArgumentException - if a factor is lower than to zero.