B4J Library jCanvasExt Extends Canvas methods

Discussion in 'B4J Libraries & Classes' started by klaus, Nov 22, 2017.

  1. klaus

    klaus Expert Licensed User

    This library, with the Class source code, extends the Canvas functionalities.
    Some time ago I was playing with B4J Canvas with methods not exposed directly to B4J.
    There was a question in the forum for a routine to draw rectangles with round corners.
    So, I think it may be useful for others.

    upload_2017-11-22_12-59-3.png
    Screenshot of the demo program.

    jCanvasExt

    Author: Klaus CHRISTL (klaus)
    Version: 1
    • CanvasExt
      • Functions:
        • appendSVGPath (SVGPath As String) As String
          Appends a SVGPath string to the current path
          M = moveTo
          L = lineTo
          H = horizontal lineTo
          V = vertical lineTo
          C = curveto
          S = smooth curveto
          Q = quadratic Bézier curve
          T = smooth quadratic Bézier curveto
          A = elliptical arc
          Z = closePath
          All of the commands above can also be expressed with lower letters.
          Capital letters means absolutely positioned, lower cases means relatively positioned.
          For more details look here:
          https://www.w3schools.com/graphics/svg_path.asp
        • arc (CenterX As Double, CenterY As Double, RadiusX As Double, RadiusY As Double, StartAngle As Double, Lenght As Double) As String
          Adds an arc to the arc end point in the current path
          CenterX, CenterY coordinates of the center
          RadiusX and RadiusY X and Y radius
          StartAngle
          Length length of the arc
          Adds path elements to the current path to make an arc that uses Euclidean degrees.
          This Euclidean orientation sweeps from East to North, then West, then South, then back to East.
          The coordinates are transformed by the current transform as they are added to the path.
        • arcTo (x1 As Double, y1 As Double, x2 As Double, y2 As Double, Radius As Double) As String
          Adds an arc to the next point (x2, y2) in the current path
          x1, y1 coordinates of point 1
          x2, y2 coordinates of point 2
          Radius
          For details look here:
          https://docs.oracle.com/javase/8/ja...rc-double-double-double-double-double-double-
        • beginPath As String
          Begins a new current path
        • bezierCurveTo (xc1 As Double, yc1 As Double, xc2 As Double, yc2 As Double, x1 As Double, y1 As Double) As String
          Adds a cubic Bezier curve to the next point (x1, y1) in the current path
          xc1, yc1 coordinates of first Bezier control point
          xc2, yc2 coordinates of second Bezier control point
          x1, y1 coordinates of the end point
        • Class_Globals As String
        • closePath As String
          Closes the path
        • DrawArc (x As Double, y As Double, Width As Double, Height As Double, StartAngle As Double, AngleExtend As Double, ArcType As String, Color As Paint, Filled As Boolean, LineWidth As Double) As String
          Draws an eliptic Arc
          x = left coordinate of the surrounding rectangle
          Y = top coordinate of the surrounding rectangle
          Width = width coordinate of the surrounding rectangle
          Height = height coordinate of the surrounding rectangle
          StartingAngle = starting angle in degrees, 0 = horizontal right
          AngleExtend = angle extend in degrees positve counter clockwise
          ArcType possible values CHORD, OPEN or ROUND
          CHORD joins the begin and end points
          ROUND joins the begin and end points to the center point
          OPEN the arc is open, when Filled = True is equivalent to CHORD
          Color = fx.Colors.Color
          Filled True Filled, False only the line
          Stroke = line width, has no effect when Filled = True
        • DrawArc2 (CenterX As Double, CenterY As Double, Radius As Double, StartAngle As Double, AngleExtend As Double, ArcType As String, Color As Paint, Filled As Boolean, LineWidth As Double) As String
          Draws a circular Arc
          CenterX = center X coordinate
          CenterY = center X coordinate
          Radius = radius of the arc
          StartingAngle = starting angle in degrees, 0 = horizontal right
          AngleExtend = angle extend in degrees positve counter clockwise
          ArcType possible values CHORD, OPEN or ROUND
          CHORD joins the begin and end points
          ROUND joins the begin and end points to the center point
          OPEN the arc is open, when Filled = True is ie equivalent to CHORD
          Color = fx.Colors.Color
          Filled True Filled, False only the line
          Stroke = line width, has no effect when Filled = True
        • DrawDashedLine (x1 As Double, y1 As Double, x2 As Double, y2 As Double, Color As Paint, LineWidth As Double, LineDashes As Double(), LineDashOffset As Double) As String
          Draws a dashed line
          x = left coordinate of the surrounding rectangle
          Y = top coordinate of the surrounding rectangle
          Width = width coordinate of the surrounding rectangle
          Height = height coordinate of the surrounding rectangle
          Radius = corner radius
          Color = fx.Colors.Color
          Filled True Filled, False only the line
          Stroke = line width, has no effect when Filled = True
          Example:
          Private Dashes(4) As Double
          Dashes = Array As Double(15, 5, 35, 10)
        • DrawOval (x As Double, y As Double, Width As Double, Height As Double, Color As Paint, Filled As Boolean, LineWidth As Double) As String
          Draws an Oval
          x = left coordinate of the surrounding rectangle
          Y = top coordinate of the surrounding rectangle
          Width = width coordinate of the surrounding rectangle
          Height = height coordinate of the surrounding rectangle
          Color = fx.Colors.Color
          Filled True Filled, False only the line
          Stroke = line width, has no effect when Filled = True
        • DrawPolygon (x As Double(), y As Double(), NumberPoints As Int, Color As Paint, Filled As Boolean, LineWidth As Double) As String
          Draws a polygon
          x() a one dimension array of the x coordinates
          y() a one dimension array of the y coordinates
          NumberPoints = number of points it can be smaller than the x and y array lenths
          The polygon is automatically closed, no need to set the last point equal to the firrst one
          Color = fx.Colors.Color
          Filled True Filled, False only the line
          Stroke = line width, has no effect when Filled = True
        • DrawPolyline (x As Double(), y As Double(), NumberPoints As Int, Color As Paint, LineWidth As Double) As String
          Draws a polyline
          x() a one dimension array of the x coordinates
          y() a one dimension array of the y coordinates
          NumberPoints = number of points it can be smaller than the x and y arrays
          Color = fx.Colors.Color
          Stroke = line width, has no effect when Filled = True
        • DrawPolyline2 (x As Double(), y As Double(), NumberPoints As Int, Color As Paint, LineWidth As Double, strokeLineCap As String, strokeLineJoin As String) As String
          Draws a polyline with LineCap and LineJoin
          x() a one dimension array of the x coordinates
          y() a one dimension array of the y coordinates
          NumberPoints = number of points it can be smaller than the x and y arrays
          Color = fx.Colors.Color
          LineWidth = line width, has no effect when Filled = True
          strokeLineCap = line extremities shape, possible values: BUTT, ROUND, or SQUARE
          strokeLineJoin = line join shapes, possible values: MITER, BEVEL or ROUND
        • DrawRect (x As Double, y As Double, Width As Double, Height As Double, Color As Paint, Filled As Boolean, LineWidth As Double) As String
          Draws a Rectangle
          x = left coordinate of the surrounding rectangle
          Y = top coordinate of the surrounding rectangle
          Width = width coordinate of the surrounding rectangle
          Height = height coordinate of the surrounding rectangle
          Color = fx.Colors.Color
          Filled True Filled, False only the line
          Stroke = line width, has no effect when Filled = True
        • DrawRect2 (x1 As Double, y1 As Double, x2 As Double, y2 As Double, Color As Paint, Filled As Boolean, LineWidth As Double) As String
          Draws a Rectangle
          x1 = left coordinate of the rectangle
          Y1 = top coordinate of the rectangle
          x2 = right coordinate of the rectangle
          y2 = bottom coordinate of the rectangle
          Color = fx.Colors.Color
          Filled True Filled, False only the line
          Stroke = line width, has no effect when Filled = True
        • DrawRectRounded (x As Double, y As Double, Width As Double, Height As Double, ArcWidth As Double, ArcHeight As Double, Color As Paint, Filled As Boolean, LineWidth As Double) As String
          Draws a rouded Rectangle
          x = left coordinate of the surrounding rectangle
          Y = top coordinate of the surrounding rectangle
          Width = width coordinate of the surrounding rectangle
          Height = height coordinate of the surrounding rectangle
          ArcWidth = corner arc width for a radius set both Arc values the same
          ArcHeight = corner arc height
          Color = fx.Colors.Color
          Filled True Filled, False only the line
          Stroke = line width, has no effect when Filled = True
        • DrawRectRounded2 (x1 As Double, y1 As Double, x2 As Double, y2 As Double, ArcWidth As Double, ArcHeight As Double, Color As Paint, Filled As Boolean, LineWidth As Double) As String
          Draws a rouded Rectangle
          x1 = left coordinate of the rectangle
          Y1 = top coordinate of the rectangle
          x2 = right coordinate of the rectangle
          y2 = bottom coordinate of the rectangle
          ArcWidth = corner arc width for a radius set both Arc values the same
          ArcHeight = corner arc height
          Color = fx.Colors.Color
          Filled True Filled, False only the line
          Stroke = line width, has no effect when Filled = True
        • DrawText3 (Text As String, x As Double, y As Double, Font As Font, Color As Paint, HorizontalAlignment As String, VerticalAlignment As String, Filled As Boolean) As String
          Draws a text
          Text = text to draw
          x = x coordinate of the reference point
          y = y coordinate of the reference point
          Font = text font name
          Color = fx.Colors.Color
          HorizontalAlignment possible values: LEFT, CENTER, RIGHT
          VerticalAlignment possible values: TOP, CENTER, BASELINE, or BOTTOM
          Filled True for standard text False for empty text.
        • fill As String
          Fills the current path (fills the area)
        • fillPolygon (x As Double(), y As Double(), NumberPoints As Int, Color As Paint, Filled As Boolean, LineWidth As Double) As String
          Fills a polygon with the current fill properties
          x() a one dimension array of the x coordinates
          y() a one dimension array of the y coordinates
          NumberPoints = number of points it can be smaller than the x and y array lenths
        • GetPaint (Color As String) As Paint
          Returns the paint object according to the color string
        • Initialize (Canvas As Canvas) As String
          Initializes the object.
        • IsInitialized As Boolean
          Tests whether the object has been initialized.
        • isPointInPath (x As Double, y As Double) As Boolean
          Returns true if the given point is insides the current path
        • lineTo (x As Double, y As Double) As String
          Adds a line to the next point (x, y) in the current path
        • moveTo (x As Double, y As Double) As String
          Moves to the next point (x, y) in the current path
        • quadraticCurveTo (xc As Double, yc As Double, x1 As Double, y1 As Double) As String
          Adds a quadratic curve to the next point (x1, y1) in the current path
          Adds segments to the current path to make a quadratic Bezier curve.
          xc, yc coordinates of the control point
          x1, y1 coordinates of the end point
        • SetFill (Color As Paint) As String
          Sets the filled color
        • SetLineCap (LineCap As String) As String
          Sets the line Cap
          possible values are SQUARE, BUTT, ROUND
        • SetLineDashes (LineDashes As Double()) As String
          Sets the line dashes
          LineDashes() array of doubles of finite non-negative dash lengths
          Example:<code>
          Private Dashes(4) As Double
          Dashes = Array As Double(15, 5, 35, 10)
          cvsTestExt.DrawDashedLine(0, 10, cvsTest.Width, 10, fx.Colors.Red, 2, Dashes, 0)
          </code>
        • SetLineDashOffset (LineDashOffset As Double) As String
          Sets the line dash offset
        • SetLineJoin (LineJoin As String) As String
          Sets the line Join
          possible values are MITER, BEVEL, ROUND
        • SetLineWidth (LineWidth As Double) As String
          Sets the line width
        • SetMiterLimit (MiterLimit As Double) As String
          Sets the miter Limit
          possible values are MITER, BEVEL, ROUND
        • SetStroke (Color As Paint) As String
          Sets the stroke (line) color
        • SetTranslateX (x As Double) As String
          Moves the Canvas horizontally by x pixels
        • SetTranslateY (y As Double) As String
          Moves the Canvas vertically by y pixels
        • stroke As String
          Strokes (draws) the current path
        • strokeLine (x1 As Double, y1 As Double, x2 As Double, y2 As Double) As String
          Strokes (draws) a line with the current stroke properties
        • strokePolygon (x As Double(), y As Double(), NumberPoints As Int) As String
          Strokes (draws) a polygon with the current stroke properties
          x() a one dimension array of the x coordinates
          y() a one dimension array of the y coordinates
          NumberPoints = number of points it can be smaller than the x and y array lenths
        • strokePolyline (x As Double(), y As Double(), NumberPoints As Int) As String
          Strokes (draws) a polyline with the current stroke properties
          x() a one dimension array of the x coordinates
          y() a one dimension array of the y coordinates
          NumberPoints = number of points, it can be smaller than the x and y array lenths
     

    Attached Files:

    Last edited: Nov 22, 2017
    BPak, Tayfur, ilan and 9 others like this.
  2. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    Hello Klaus!
    To find the Centroid of this figure?

    regards...
     

    Attached Files:

  3. klaus

    klaus Expert Licensed User

    Sorry for answering only today, but I was very busy the last two days.
    I suggest you to ask the question in the questions forum, in a new thread, as it is not related to the jCanvasExt library, it is a general geometry question.
    I will look at it and answer in your new thread.
     
  4. Informatix

    Informatix Expert Licensed User

    Try SimpleGameEngine. It contains all the functions of jCanvasExt and a function able to compute the centroid of a polygon (class sgePolygon, function Centroid) among many other functions.
     
    ilan and hibrid0 like this.
  5. Johan Hormaza

    Johan Hormaza Active Member Licensed User

  6. Tayfur

    Tayfur Well-Known Member Licensed User

    is it possible B4XCanvas?
    i tried it. it not convert.
     
  7. klaus

    klaus Expert Licensed User

    The jCanvasExt is a B4J class, it is based on this documentation.
    https://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/GraphicsContext.html
    To convert it to B4XCanvas you would need to look at the B4A and B4i documentations and write the methods with JaveObject and NativeObject.
    I am not sure that all functions axist in all three platforms.
    It might be a challenge for Erel :).
     
    Johan Hormaza and Tayfur like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice