Wednesday, January 25, 2017

Jython 9. 4 Triangles

This Example, 4 Triangles, will produce the same output as Java Example 9. Four lists are created, tri1 to tri4, defining the three vertices. We pass the list to the Polygon() function. In JavaFX it is expecting a double array since it does not how many vertices are in Polygon, and not a positional argument as in the previous examples, thus we pass a list in Jython as we only want 1 argument.


# ex09.py
# 4 Triangles

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 Polygon

class Ex09(Application):
    def start(self, stage):
        stage.setTitle("Example 9. 4 Triangles")
        root = Group()
        
        x,y,w = 200,200,100
        xy = [x,y]
        tri1 = xy+[x+w,y,x,y-w]
        tri2 = xy+[x,y-w,x-100,y]
        tri3 = xy+[x-w,y,x,y+w]
        tri4 = xy+[x+w,y,x,y+w]
        
        poly1 = Polygon(tri1)
        poly1.setFill(Color.RED)
        
        poly2 = Polygon(tri2)
        poly2.setFill(Color.GREEN)
        
        poly3 = Polygon(tri3)
        poly3.setFill(Color.BLUE)
                
        poly4 = Polygon(tri4)
        poly4.setFill(Color.FUCHSIA)
        
        root.getChildren().addAll(poly1,poly2,poly3,poly4)
        
        scene = Scene(root, 400, 400, Color.GHOSTWHITE)
        stage.setScene(scene)
        stage.show()

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

No comments:

Post a Comment