### Draw a Rectangle using JavaObject

An example on how to create a rectangle and draw on the MainForm.
In addition some how to use the Rectangle Class methods.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Rectangle.html
Example Sub to create a new Rectangle
' Create a new Rectangle
' Parameter: x, y position as double
' Parameter: width, height as double
' Paremeter: fill color as paint (like fx.Colors.Green=
' Example: CreateRectangle(0, 0, 300, 300, fx.Colors.Green)
`Sub CreateRectangle(x As Double, y As Double, w As Double, h As Double, fill As Paint)
Dim jo As JavaObject
' Init a new instance of a rectangle class. Note that all pareámeters are Double and not int!
jo.InitializeNewInstance("javafx.scene.shape.Rectangle", Array As Object(x,y,w,h))
' Fill the new rectangle with a color
jo.RunMethod("setFill", Array(fill))
' And add the rectangle to the rootnode --- do not forget this else the new rectangle is not visible
' Add also an ID
Dim n As Node = jo
n.Id = "My Rectangle"
MainForm.RootPane.AddNode(n,x,y,-1,-1)
End Sub`
Example using additional methods
' Set the fill color using the fx color scheme
jo.RunMethod("setFill", Array(fx.Colors.Green))
Example subroutine to get Rectangle properties
`Sub RectangleGetProperty(r As Node, p As String) As Double
Dim result As Double = -1
Dim jo As JavaObject = r
Select p.ToLowerCase
Case "x"
result = jo.RunMethod("getX", Null)
Case "y"
result = jo.RunMethod("getY", Null)
Case "h"
result = jo.RunMethod("getHeight", Null)
Case "w"
result = jo.RunMethod("getWidth", Null)
End Select
Return result
End Sub`
`Sub RectangleShowProperties
Log("X: " & RectangleGetProperty(idRectangle, "x"))
Log("Y: " & RectangleGetProperty(idRectangle, "y"))
Log("W: " & RectangleGetProperty(idRectangle, "w"))
Log("H: " & RectangleGetProperty(idRectangle, "h"))
End Sub`
Examples on how to access the node id
' List the id of all root node
`Sub GetAllRootNodes
For Each n As Node In MainForm.rootpane.GetAllViewsRecursive
Log(n.Id)
Next
End Sub`
' Get a node by id
`Sub GetRootNode(id As String) As Node
Dim result As Node = Null
For Each n As Node In MainForm.rootpane.GetAllViewsRecursive
If n.id.ToLowerCase = id.ToLowerCase Then
result = n
Log("Returning: " & result.Id)
Return result
End If
Next
Return result
End Sub`
Usage:
CreateRectangle(0, 0, 300, 300, fx.Colors.Green)
Log("This is " & GetRootNode("my rectangle").Id)
Example to Remove a Node
Dim ID as string = "1"
Dim rn As Node = GetRootNodeByID(ID)
If rn.IsInitialized Then
Log("Removing Node: " & rn.Id)
rn.RemoveNodeFromParent
End If
Here a complete example Project
Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 400
End Region
'https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Rectangle.html
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private rectMain As Node
Private btnChange As Button
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
MainForm.Show
'
RectangleShowProperties(rectMain)
End Sub`
`Sub btnChange_Action
CreateRectangle(0, 0, 300, 300, fx.Colors.Green)
End Sub`
`Sub RectangleShowProperties(r As Node)
Log("X: " & RectangleGetProperty(r, "x"))
Log("Y: " & RectangleGetProperty(r, "y"))
Log("W: " & RectangleGetProperty(r, "w"))
Log("H: " & RectangleGetProperty(r, "h"))
End Sub`
`Sub RectangleSetSize(r As Node, w As Int, h As Int)
Dim jo As JavaObject = r
jo.RunMethod("setWidth", Array(w))
jo.RunMethod("setHeight", Array(h))
End Sub`
`Sub RectangleGetProperty(r As Node, p As String) As Double
Dim result As Double = -1
Dim jo As JavaObject = r
Select p.ToLowerCase
Case "x"
result = jo.RunMethod("getX", Null)
Case "y"
result = jo.RunMethod("getY", Null)
Case "h"
result = jo.RunMethod("getHeight", Null)
Case "w"
result = jo.RunMethod("getWidth", Null)
End Select
Return result
End Sub`
' Create a new Rectangle
' Parameter: x, y position as double
' Parameter: width, height as double
' Paremeter: fill color as paint (like fx.Colors.Green=
' Example: CreateRectangle(0, 0, 300, 300, fx.Colors.Green)
`Sub CreateRectangle(x As Double, y As Double, w As Double, h As Double, fill As Paint)
Dim jo As JavaObject
' Init a new instance of a rectangle class. Note that all pareámeters are Double and not int!
jo.InitializeNewInstance("javafx.scene.shape.Rectangle", Array As Object(x,y,w,h))
' Fill the new rectangle with a color
jo.RunMethod("setFill", Array(fill))
' And add the rectangle to the rootnode --- do not forget this else the new rectangle is not visible
MainForm.RootPane.AddNode(jo,0,0,-1,-1)
End Sub`
Layout Nodes
Rectangle rectMain
Button btnChange
Canvas cvsMain

Changed: , Category: Shapes, File: T6C247M1786.htm, ID: 1786