Saturday, January 21, 2017

Jython 3. Cubic Curve

This Example, Cubic Curve, will produce the same output as Java Example 3. With Jython, it is possible to use JavaFX using Python code.


However the Python code, for Jython 2.7, will be compatible with Python 2.7, and not the newest Python. This is usually not a problem. The benefits are access to Java libraries, like JavaFX, which is better than most, if not all, Python GUI libraries.


# ex03.py
# Cubic Curve

from javafx.application import Application
from javafx.scene import Scene
from javafx.scene import Group
from javafx.scene.paint import Color
from javafx.scene.shape import CubicCurve

def getPos():
    pos = []
    pos.extend([20,20]) # start x,y
    pos.extend([20,220]) # control x1,y1
    pos.extend([300,220]) # control x2,y2
    pos.extend([300,20]) # end x,y
    return pos

class Ex03(Application):
    def start(self, stage):
        stage.setTitle("Example 3. Cubic Curve")
        root = Group()
        
        pos = getPos()
        cubicCurve = CubicCurve(*pos)
        cubicCurve.setStroke(Color.BLACK)
        cubicCurve.setStrokeWidth(3);
        cubicCurve.setFill(Color.WHITE)
                
        root.getChildren().add(cubicCurve)

        scene = Scene(root, 320, 240, Color.WHITE)
        stage.setScene(scene)
        stage.show()

if __name__ == '__main__':
    Application.launch(Ex03().class, [])

The function, getPos(), returns a list of 8 elements. However CubicCurve() requires either 0 or 8 arguments. We use *pos to convert the list to 8 positional arguments.


No comments:

Post a Comment