Sunday, January 29, 2017

Jython 11. Text

This Example, Text, will produce the same output as Java Example 11. We never use something like a new keyword, here as well as in or the previous examples, since Python or Jython variables aren't declared. We can always check if the type is what we intend by using the type keyword.


# ex11.py
# Text

from javafx.application import Application
from javafx.scene import Scene
from javafx.scene import Group
from javafx.scene.paint import Color
from javafx.scene.text import Font
from javafx.scene.text import Text

class Ex11(Application):
    def start(self, stage):
        stage.setTitle("Example 11. Text")
        root = Group()
        
        text = Text(10,60,"Hello\nText\nWorld!")
        text.setFont(Font("Georgia",50))
        print type(text)
        
        root.getChildren().add(text)
        
        scene = Scene(root, 400, 200, Color.HONEYDEW)
        stage.setScene(scene)
        stage.show()

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

We should get that the type is:
<type 'javafx.scene.text.Text'>

Thursday, January 26, 2017

Jython 10. Polyline

This Example, Polyline, will produce the same output as Java Example 10. A list of vertices is held in vertData. We pass the list to the Polyline() function.


# ex10.py
# Polyline

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 Polyline

class Ex10(Application):
    def start(self, stage):
        stage.setTitle("Example 10. Polyline")
        root = Group()
        
        x,y,w = 200,200,100
        
        vertData = []
        vertData.extend([x-2*w, y-w])
        vertData.extend([x-w, y+w])
        vertData.extend([x, y-w])
        vertData.extend([x+w, y+w])
        vertData.extend([x+2*w, y-w])
        vertData.extend([x, 0])
        vertData.extend([x-2*w, y-w])
        
        polyline = Polyline(vertData)
        polyline.setFill(Color.rgb(1, 225, 80))
                
        root.getChildren().addAll(polyline);
        
        scene = Scene(root, 400, 400, Color.PALEGREEN)
        stage.setScene(scene)
        stage.show()

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

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, [])

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, [])

Sunday, January 22, 2017

Jython 7. QuadCurve

This Example, QuadCurve, will produce the same output as Java Example 7.


# ex07.py
# QuadCurve

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 QuadCurve

class Pos:
    pass

pos = Pos()
pos.start = [0,200] # start x,y
pos.control = [200,0] # control x,y
pos.end = [400,200] # end x,y
pos.val = pos.start + pos.control + pos.end

class Ex07(Application):
    def start(self, stage):
        stage.setTitle("Example 7. QuadCurve")
        root = Group()
        
        quad = QuadCurve(*pos.val)
        quad.setFill(Color.RED)
        
        root.getChildren().add(quad)
        
        scene = Scene(root, 400, 400, Color.GHOSTWHITE)
        stage.setScene(scene)
        stage.show()

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

Jython 6. Ellipse

This Example, Circle, will produce the same output as Java Example 6.


# ex06.py
# Ellipse

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 Ellipse

class Pos:
    pass

pos = Pos()
pos.cen = [200,200] # center x,y
pos.rad = [100,150] # radius x,y
pos.val = pos.cen + pos.rad

class Ex06(Application):
    def start(self, stage):
        stage.setTitle("Example 6. Ellipse")
        root = Group()
        
        elli = Ellipse(*pos.val)
        elli.setFill(Color.RED)
        root.getChildren().add(elli)
        
        scene = Scene(root, 400, 400, Color.AQUA)
        stage.setScene(scene)
        stage.show()

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

An empty class is created. We create an instance pos and then set its values. It is also possible to, for example, use variables like cen, rad, and val, in the global namespace. However this organizes things since they are related, and the only reason we are using them is to create a new namespace.


Jython 5. Circle

This Example, Circle, will produce the same output as Java Example 5.


# ex05.py
# Circle

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 Circle

class Pos:
    pass

pos = Pos()
pos.cen = [200,200] # center x,y
pos.rad = [100] # radius
pos.fill = [Color.RED] # fill
pos.val = pos.cen + pos.rad + pos.fill

class Ex05(Application):
    def start(self, stage):
        stage.setTitle("Example 5. Circle")
        root = Group()
        circ = Circle(*pos.val)
        root.getChildren().add(circ)
        scene = Scene(root, 400, 400, Color.AQUA)
        stage.setScene(scene)
        stage.show()

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

An empty class is created. We create an instance pos and then set its values.