Class AwtRenderingBackend

java.lang.Object
org.htmlunit.platform.canvas.rendering.AwtRenderingBackend
All Implemented Interfaces:
RenderingBackend

public class AwtRenderingBackend extends Object implements RenderingBackend
The default implementation of RenderingBackend.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static final class 
     

    Nested classes/interfaces inherited from interface org.htmlunit.platform.canvas.rendering.RenderingBackend

    RenderingBackend.WindingRule
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private Color
     
    private float
     
    private final Graphics2D
     
    private final int
     
    private static int
     
    private final BufferedImage
     
    private static final Map<String,Color>
     
    private int
     
    private static final org.apache.commons.logging.Log
     
     
    private Color
     
    private final List<Path2D>
     
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    AwtRenderingBackend(int imageWidth, int imageHeight)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    arc(double x, double y, double radius, double startAngle, double endAngle, boolean anticlockwise)
    Adds a circular arc to the current sub-path.
    void
    Starts a new path by emptying the list of sub-paths.
    void
    bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
    Adds a cubic Bézier curve to the current sub-path.
    void
    clearRect(double x, double y, double w, double h)
    Paints the specified rectangular area.
    void
    Turns the current or given path into the current clipping region.
    void
    Attempts to add a straight line from the current point to the start of the current sub-path.
    void
    drawImage(ImageData imageData, int sx, int sy, Integer sWidth, Integer sHeight, int dx, int dy, Integer dWidth, Integer dHeight)
    Draws images onto the context.
    void
    ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle, boolean anticlockwise)
    Creates an elliptical arc centered at (x, y) with the radii radiusX and radiusY.
    Constructs a base64 encoded string out of the image data.
    private static Color
     
    void
    Fills the current or given path with the current fillStyle.
    void
    fillRect(int x, int y, int w, int h)
    Paints the specified rectangular area.
    void
    fillText(String text, double x, double y)
    Fills a given text at the given (x, y) position.
    byte[]
    getBytes(int width, int height, int sx, int sy)
    Creates a byte array containing the (4) color values of all pixels.
    private Path2D
     
    double
    int
    Returns the lineWidth property.
    void
    lineTo(double x, double y)
    Adds a straight line to the current sub-path by connecting the sub-path's last point to the specified (x, y) coordinates.
    void
    moveTo(double x, double y)
    Begins a new sub-path at the point specified by the given (x, y) coordinates.
    private static int
     
    void
    putImageData(byte[] imageDataBytes, int imageDataWidth, int imageDataHeight, int dx, int dy, int dirtyX, int dirtyY, int dirtyWidth, int dirtyHeight)
    Paints data from the given ImageData object onto the canvas.
    void
    quadraticCurveTo(double cpx, double cpy, double x, double y)
    Adds a quadratic Bézier curve to the current sub-path.
    void
    rect(double x, double y, double w, double h)
    Adds a rectangle to the current path.
    void
    Restores the most recently saved canvas state by popping the top entry in the drawing state stack.
    void
    rotate(double angle)
    Adds a rotation to the transformation matrix.
    void
    Saves the entire state of the canvas by pushing the current state onto a stack.
    void
    setFillStyle(String fillStyle)
    Sets the fillStyle property.
    void
    setGlobalAlpha(double globalAlpha)
    Specifies the alpha (transparency) value that is applied to shapes and images before they are drawn onto the canvas.
    void
    setLineWidth(int lineWidth)
    Sets the lineWidth property.
    void
    setStrokeStyle(String strokeStyle)
    Sets the strokeStyle property.
    void
    setTransform(double m11, double m12, double m21, double m22, double dx, double dy)
    Resets (overrides) the current transformation to the identity matrix, and then invokes a transformation described by the arguments of this method.
    void
    Strokes (outlines) the current or given path with the current stroke style.
    void
    strokeRect(int x, int y, int w, int h)
    Paints the specified rectangular area.
    private static Color
     
    void
    transform(double m11, double m12, double m21, double m22, double dx, double dy)
    Multiplies the current transformation with the matrix described by the arguments of this method.
    void
    translate(int x, int y)
    Adds a translation transformation to the current matrix.

    Methods inherited from class java.lang.Object

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

    • LOG

      private static final org.apache.commons.logging.Log LOG
    • ID_GENERATOR_

      private static int ID_GENERATOR_
    • knownColors

      private static final Map<String,Color> knownColors
    • id_

      private final int id_
    • image_

      private final BufferedImage image_
    • graphics2D_

      private final Graphics2D graphics2D_
    • transformation_

      private AffineTransform transformation_
    • globalAlpha_

      private float globalAlpha_
    • lineWidth_

      private int lineWidth_
    • fillColor_

      private Color fillColor_
    • strokeColor_

      private Color strokeColor_
    • subPaths_

      private final List<Path2D> subPaths_
    • savedStates_

      private final Deque<AwtRenderingBackend.SaveState> savedStates_
  • Constructor Details

    • AwtRenderingBackend

      public AwtRenderingBackend(int imageWidth, int imageHeight)
      Constructor.
      Parameters:
      imageWidth - the width
      imageHeight - the height
  • Method Details

    • nextId

      private static int nextId()
    • getGlobalAlpha

      public double getGlobalAlpha()
      Specified by:
      getGlobalAlpha in interface RenderingBackend
      Returns:
      the alpha (transparency) value that is applied to shapes and images before they are drawn onto the canvas.
    • setGlobalAlpha

      public void setGlobalAlpha(double globalAlpha)
      Specifies the alpha (transparency) value that is applied to shapes and images before they are drawn onto the canvas.
      Specified by:
      setGlobalAlpha in interface RenderingBackend
      Parameters:
      globalAlpha - the new alpha
    • beginPath

      public void beginPath()
      Starts a new path by emptying the list of sub-paths.
      Specified by:
      beginPath in interface RenderingBackend
    • ellipse

      public void ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle, boolean anticlockwise)
      Creates an elliptical arc centered at (x, y) with the radii radiusX and radiusY. The path starts at startAngle and ends at endAngle, and travels in the direction given by anticlockwise (defaulting to clockwise).
      Specified by:
      ellipse in interface RenderingBackend
      Parameters:
      x - the x
      y - the y
      radiusX - the radiusX
      radiusY - the radiusY
      rotation - the rotation
      startAngle - the start angle
      endAngle - the end angle
      anticlockwise - is anti-clockwise
    • bezierCurveTo

      public void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
      Adds a cubic Bézier curve to the current sub-path. It requires three points: the first two are control points and the third one is the end point. The starting point is the latest point in the current path, which can be changed using moveTo() before creating the Bézier curve.
      Specified by:
      bezierCurveTo in interface RenderingBackend
      Parameters:
      cp1x - the cp1x
      cp1y - the cp1y
      cp2x - the cp2x
      cp2y - the cp2y
      x - the x
      y - the y
    • arc

      public void arc(double x, double y, double radius, double startAngle, double endAngle, boolean anticlockwise)
      Adds a circular arc to the current sub-path.
      Specified by:
      arc in interface RenderingBackend
      Parameters:
      x - the x
      y - the y
      radius - the radius
      startAngle - the start angle
      endAngle - the end angle
      anticlockwise - is anti-clockwise
    • clearRect

      public void clearRect(double x, double y, double w, double h)
      Paints the specified rectangular area.
      Specified by:
      clearRect in interface RenderingBackend
      Parameters:
      x - the x
      y - the y
      w - the width
      h - the height
    • drawImage

      public void drawImage(ImageData imageData, int sx, int sy, Integer sWidth, Integer sHeight, int dx, int dy, Integer dWidth, Integer dHeight) throws IOException
      Draws images onto the context.
      Specified by:
      drawImage in interface RenderingBackend
      Parameters:
      imageData - the reader to read the image from 8the first one)
      sx - the X coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context
      sy - the Y coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context
      sWidth - the width of the sub-rectangle of the source image to draw into the destination context
      sHeight - the height of the sub-rectangle of the source image to draw into the destination context
      dx - the X coordinate in the destination canvas at which to place the top-left corner of the source image
      dy - the Y coordinate in the destination canvas at which to place the top-left corner of the source image
      dWidth - the width to draw the image in the destination canvas. This allows scaling of the drawn image
      dHeight - the height to draw the image in the destination canvas. This allows scaling of the drawn image
      Throws:
      IOException - in case o problems
    • encodeToString

      public String encodeToString(String type) throws IOException
      Constructs a base64 encoded string out of the image data.
      Specified by:
      encodeToString in interface RenderingBackend
      Parameters:
      type - the name of the image format
      Returns:
      the base64 encoded string
      Throws:
      IOException - in case o problems
    • fill

      public void fill()
      Fills the current or given path with the current fillStyle.
      Specified by:
      fill in interface RenderingBackend
    • fillRect

      public void fillRect(int x, int y, int w, int h)
      Paints the specified rectangular area.
      Specified by:
      fillRect in interface RenderingBackend
      Parameters:
      x - the x
      y - the y
      w - the width
      h - the height
    • fillText

      public void fillText(String text, double x, double y)
      Fills a given text at the given (x, y) position.
      Specified by:
      fillText in interface RenderingBackend
      Parameters:
      text - the text
      x - the x
      y - the y
    • getBytes

      public byte[] getBytes(int width, int height, int sx, int sy)
      Creates a byte array containing the (4) color values of all pixels.
      Specified by:
      getBytes in interface RenderingBackend
      Parameters:
      width - the width
      height - the height
      sx - start point x
      sy - start point y
      Returns:
      the bytes
    • lineTo

      public void lineTo(double x, double y)
      Adds a straight line to the current sub-path by connecting the sub-path's last point to the specified (x, y) coordinates.
      Specified by:
      lineTo in interface RenderingBackend
      Parameters:
      x - the x
      y - the y
    • moveTo

      public void moveTo(double x, double y)
      Begins a new sub-path at the point specified by the given (x, y) coordinates.
      Specified by:
      moveTo in interface RenderingBackend
      Parameters:
      x - the x
      y - the y
    • putImageData

      public void putImageData(byte[] imageDataBytes, int imageDataWidth, int imageDataHeight, int dx, int dy, int dirtyX, int dirtyY, int dirtyWidth, int dirtyHeight)
      Paints data from the given ImageData object onto the canvas.
      Specified by:
      putImageData in interface RenderingBackend
      Parameters:
      imageDataBytes - an array of pixel values
      imageDataWidth - the height of the imageData
      imageDataHeight - the width of the imageData
      dx - horizontal position (x coordinate) at which to place the image data in the destination canvas
      dy - vertical position (y coordinate) at which to place the image data in the destination canvas
      dirtyX - horizontal position (x coordinate) of the top-left corner from which the image data will be extracted. Defaults to 0.
      dirtyY - vertical position (y coordinate) of the top-left corner from which the image data will be extracted. Defaults to 0.
      dirtyWidth - width of the rectangle to be painted. Defaults to the width of the image data.
      dirtyHeight - height of the rectangle to be painted. Defaults to the height of the image data.
    • quadraticCurveTo

      public void quadraticCurveTo(double cpx, double cpy, double x, double y)
      Adds a quadratic Bézier curve to the current sub-path. It requires two points: the first one is a control point and the second one is the end point. The starting point is the latest point in the current path, which can be changed using moveTo() before creating the quadratic Bézier curve.
      Specified by:
      quadraticCurveTo in interface RenderingBackend
      Parameters:
      cpx - the cpx
      cpy - the cpy
      x - the x
      y - the y
    • rect

      public void rect(double x, double y, double w, double h)
      Adds a rectangle to the current path.
      Specified by:
      rect in interface RenderingBackend
      Parameters:
      x - the x
      y - the y
      w - the width
      h - the height
    • setFillStyle

      public void setFillStyle(String fillStyle)
      Sets the fillStyle property.
      Specified by:
      setFillStyle in interface RenderingBackend
      Parameters:
      fillStyle - the fillStyle property
    • setStrokeStyle

      public void setStrokeStyle(String strokeStyle)
      Sets the strokeStyle property.
      Specified by:
      setStrokeStyle in interface RenderingBackend
      Parameters:
      strokeStyle - the strokeStyle property
    • extractColor

      private static Color extractColor(String style)
    • getLineWidth

      public int getLineWidth()
      Returns the lineWidth property.
      Specified by:
      getLineWidth in interface RenderingBackend
      Returns:
      the lineWidth property
    • restore

      public void restore()
      Restores the most recently saved canvas state by popping the top entry in the drawing state stack. If there is no saved state, this method does nothing.
      Specified by:
      restore in interface RenderingBackend
    • rotate

      public void rotate(double angle)
      Adds a rotation to the transformation matrix.
      Specified by:
      rotate in interface RenderingBackend
      Parameters:
      angle - the angle
    • save

      public void save()
      Saves the entire state of the canvas by pushing the current state onto a stack.
      Specified by:
      save in interface RenderingBackend
    • setLineWidth

      public void setLineWidth(int lineWidth)
      Sets the lineWidth property.
      Specified by:
      setLineWidth in interface RenderingBackend
      Parameters:
      lineWidth - the lineWidth property
    • setTransform

      public void setTransform(double m11, double m12, double m21, double m22, double dx, double dy)
      Resets (overrides) the current transformation to the identity matrix, and then invokes a transformation described by the arguments of this method. This lets you scale, rotate, translate (move), and skew the context.
      Specified by:
      setTransform in interface RenderingBackend
      Parameters:
      m11 - Horizontal scaling. A value of 1 results in no scaling
      m12 - Vertical skewing
      m21 - Horizontal skewing
      m22 - Vertical scaling. A value of 1 results in no scaling
      dx - Horizontal translation (moving)
      dy - Vertical translation (moving).
    • stroke

      public void stroke()
      Strokes (outlines) the current or given path with the current stroke style.
      Specified by:
      stroke in interface RenderingBackend
    • strokeRect

      public void strokeRect(int x, int y, int w, int h)
      Paints the specified rectangular area.
      Specified by:
      strokeRect in interface RenderingBackend
      Parameters:
      x - the x
      y - the y
      w - the width
      h - the height
    • transform

      public void transform(double m11, double m12, double m21, double m22, double dx, double dy)
      Multiplies the current transformation with the matrix described by the arguments of this method. This lets you scale, rotate, translate (move), and skew the context.
      Specified by:
      transform in interface RenderingBackend
      Parameters:
      m11 - Horizontal scaling. A value of 1 results in no scaling
      m12 - Vertical skewing
      m21 - Horizontal skewing
      m22 - Vertical scaling. A value of 1 results in no scaling
      dx - Horizontal translation (moving)
      dy - Vertical translation (moving).
    • translate

      public void translate(int x, int y)
      Adds a translation transformation to the current matrix.
      Specified by:
      translate in interface RenderingBackend
      Parameters:
      x - the x
      y - the y
    • clip

      public void clip(RenderingBackend.WindingRule windingRule, Path2D path)
      Turns the current or given path into the current clipping region. It replaces any previous clipping region.
      Specified by:
      clip in interface RenderingBackend
      Parameters:
      windingRule - the RenderingBackend.WindingRule RenderingBackend.WindingRule to be used
      path - the path or null if the current path should be used
    • closePath

      public void closePath()
      Attempts to add a straight line from the current point to the start of the current sub-path. If the shape has already been closed or has only one point, this function does nothing.
      Specified by:
      closePath in interface RenderingBackend
    • getCurrentSubPath

      private Path2D getCurrentSubPath()
    • toAwtColor

      private static Color toAwtColor(Color color)