Class Graphics2DCanvas

java.lang.Object
net.codecrete.qrbill.canvas.AbstractCanvas
net.codecrete.qrbill.canvas.Graphics2DCanvas
All Implemented Interfaces:
Closeable, AutoCloseable, Canvas
Direct Known Subclasses:
PNGCanvas

public class Graphics2DCanvas extends AbstractCanvas
Canvas for drawing to a Graphics2D instance.
  • Nested Class Summary

    Nested classes/interfaces inherited from interface net.codecrete.qrbill.canvas.Canvas

    Canvas.LineStyle
  • Field Summary

    Fields inherited from class net.codecrete.qrbill.canvas.AbstractCanvas

    fontMetrics, MM_TO_PT
  • Constructor Summary

    Constructors
    Constructor
    Description
    Graphics2DCanvas(Graphics2D graphics, float offsetX, float offsetY, float scale, String fontFamily)
    Creates a new instance for the given graphics context.
    Creates a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addRectangle(double x, double y, double width, double height)
    Adds a rectangle to the path
    void
     
    void
    Closes the current subpath
    void
    cubicCurveTo(double x1, double y1, double x2, double y2, double x, double y)
    Adds a cubic Beziér curve to the open path going from the previous point to the specified position.
    void
    fillPath(int color, boolean smoothing)
    Fills the current path and ends it
    protected void
    initGraphics(Graphics2D graphics, boolean ownsGraphics, float scale)
    Initializes the canvas with the given graphics context.
    void
    lineTo(double x, double y)
    Adds a line segment to the open path from the previous point to the specified position.
    void
    moveTo(double x, double y)
    Moves the current point of the open path to the specified position.
    void
    putText(String text, double x, double y, int fontSize, boolean isBold)
    Adds text to the graphics.
    protected void
    setOffset(float x, float y)
    Sets the offset to the bottom left corner of the drawing area.
    void
    setTransformation(double translateX, double translateY, double rotate, double scaleX, double scaleY)
    Sets a translation, rotation and scaling for the subsequent operations
    void
    Starts a path that can be filled or stroked
    void
    strokePath(double strokeWidth, int color, Canvas.LineStyle lineStyle, boolean smoothing)
    Strokes the current path and ends it

    Methods inherited from class net.codecrete.qrbill.canvas.AbstractCanvas

    getAscender, getDescender, getLineHeight, getTextWidth, putTextLines, setupFontMetrics, splitLines

    Methods inherited from class java.lang.Object

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

    • Graphics2DCanvas

      public Graphics2DCanvas(String fontFamily)
      Creates a new instance.

      Before drawing to the canvas, initGraphics(Graphics2D, boolean, float) must be called to initialize it.

      Parameters:
      fontFamily - a list of font family names, separated by comma (same syntax as for CSS). The first installed font family will be used.
    • Graphics2DCanvas

      public Graphics2DCanvas(Graphics2D graphics, float offsetX, float offsetY, float scale, String fontFamily)
      Creates a new instance for the given graphics context.

      The offset is specified in the graphics context's coordinate system. Positive y coordinates point downwards.

      The graphics context is neither owned nor disposed.

      Parameters:
      graphics - graphics context
      offsetX - the x-offset to the bottom left corner of the drawing area, in the graphics context's coordinate system.
      offsetY - the y-offset to the bottom left corner of the drawing area, in the graphics context's coordinate system.
      scale - the conversion factor from mm to the drawing surface coordinate system.
      fontFamily - a list of font family names, separated by comma (same syntax as for CSS). The first installed font family will be used.
  • Method Details

    • initGraphics

      protected void initGraphics(Graphics2D graphics, boolean ownsGraphics, float scale)
      Initializes the canvas with the given graphics context.
      Parameters:
      graphics - graphics context
      ownsGraphics - if true, this instance will own the graphics surface and dispose it on closing
      scale - the conversion factor from mm to the drawing surface coordinate system.
    • setOffset

      protected void setOffset(float x, float y)
      Sets the offset to the bottom left corner of the drawing area.

      This method must be called before calling initGraphics(Graphics2D, boolean, float).

      Parameters:
      x - the x-offset to the bottom left corner of the drawing area, in the graphics context's coordinate system.
      y - the y-offset to the bottom left corner of the drawing area, in the graphics context's coordinate system.
    • close

      public void close()
    • setTransformation

      public void setTransformation(double translateX, double translateY, double rotate, double scaleX, double scaleY)
      Description copied from interface: Canvas
      Sets a translation, rotation and scaling for the subsequent operations

      Before a new translation is applied, the coordinate system is reset to it's original state.

      The transformations are applied in the order translation, rotation, scaling.

      Parameters:
      translateX - translation in x direction (in mm)
      translateY - translation in y direction (in mm)
      rotate - rotation angle, in radians
      scaleX - scale factor in x direction (1.0 = no scaling)
      scaleY - scale factor in y direction (1.0 = no scaling)
    • putText

      public void putText(String text, double x, double y, int fontSize, boolean isBold)
      Description copied from interface: Canvas
      Adds text to the graphics.

      The text position refers to the left most point on the text's baseline.

      Parameters:
      text - the text
      x - x position of the text's start (in mm)
      y - y position of the text's top (in mm)
      fontSize - the font size (in pt)
      isBold - indicates if the text is in bold or regular weight
    • startPath

      public void startPath()
      Description copied from interface: Canvas
      Starts a path that can be filled or stroked
    • moveTo

      public void moveTo(double x, double y)
      Description copied from interface: Canvas
      Moves the current point of the open path to the specified position.
      Parameters:
      x - x-coordinate of position
      y - y-coordinate of position
    • lineTo

      public void lineTo(double x, double y)
      Description copied from interface: Canvas
      Adds a line segment to the open path from the previous point to the specified position.
      Parameters:
      x - x-coordinate of position
      y - y-coordinate of position
    • cubicCurveTo

      public void cubicCurveTo(double x1, double y1, double x2, double y2, double x, double y)
      Description copied from interface: Canvas
      Adds a cubic Beziér curve to the open path going from the previous point to the specified position. Two control points control the curve
      Parameters:
      x1 - x-coordinate of first control point
      y1 - y-coordinate of first control point
      x2 - x-coordinate of second control point
      y2 - y-coordinate of second control point
      x - x-coordinate of position
      y - y-coordinate of position
    • addRectangle

      public void addRectangle(double x, double y, double width, double height)
      Description copied from interface: Canvas
      Adds a rectangle to the path
      Parameters:
      x - the rectangle's left position (in mm)
      y - the rectangle's top position (in mm)
      width - the rectangle's width (in mm)
      height - rectangle's height (in mm)
    • closeSubpath

      public void closeSubpath()
      Description copied from interface: Canvas
      Closes the current subpath
    • fillPath

      public void fillPath(int color, boolean smoothing)
      Description copied from interface: Canvas
      Fills the current path and ends it
      Parameters:
      color - the fill color (expressed similar to HTML, e.g. 0xffffff for white)
      smoothing - true for using smoothing techniques such as antialiasing, false otherwise
    • strokePath

      public void strokePath(double strokeWidth, int color, Canvas.LineStyle lineStyle, boolean smoothing)
      Description copied from interface: Canvas
      Strokes the current path and ends it
      Parameters:
      strokeWidth - the stroke width (in pt)
      color - the stroke color (expressed similar to HTML, e.g. 0xffffff for white)
      lineStyle - the line style
      smoothing - true for using smoothing techniques such as antialiasing, false otherwise