Tuesday, January 24, 2017

Jython 8. 2 Rectangles

This Example, 2 Rectangles, will produce the same output as Java Example 8. Two objects of Pos class are created, each defining the parameters for a rectangle. We have to use *args in the function call so it will properly unpack.


# ex08.py
# 2 Rectangles

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

class Pos:
    pass

pos1 = Pos()
pos1.corner = [100,100] # NW corner x,y
pos1.dimension = [100,100] # width, height
pos1.val = pos1.corner + pos1.dimension

pos2 = Pos()
pos2.corner = [200,200] # NW corner x,y
pos2.dimension = [100,100] # width, height
pos2.val = pos2.corner + pos2.dimension

class Ex08(Application):
    def start(self, stage):
        stage.setTitle("Example 8. 2 Rectangles")
        root = Group()
        
        rect1 = Rectangle(*pos1.val)
        rect1.setFill(Color.RED)
        
        rect2 = Rectangle(*pos2.val)
        rect2.setFill(Color.BLUE)
        rect2.setArcHeight(50)
        rect2.setArcWidth(50)
        
        root.getChildren().addAll(rect1,rect2)
        
        scene = Scene(root, 400, 400, Color.GHOSTWHITE)
        stage.setScene(scene)
        stage.show()

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

No comments:

Post a Comment