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.


Saturday, January 21, 2017

Jython 4. Arc

This Example, Arc, will produce the same output as Java Example 4.


# ex04.py
# Arc

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 Arc
from javafx.scene.shape import ArcType

def getPos():
    pos = []
    pos.extend([200,200]) # center x,y
    pos.extend([100,100]) # radius x,y
    pos.extend([0,270]) # start and end angles
    return pos

class Ex04(Application):
    def start(self, stage):
        stage.setTitle("Example 4. Arc")
        root = Group()
        
        pos = getPos()
        arc = Arc(*pos)
        arc.setType(ArcType.ROUND)
        arc.setFill(Color.RED)
        root.getChildren().add(arc)
        
        scene = Scene(root, 400, 400, Color.AZURE)
        stage.setScene(scene)
        stage.show()

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

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.


Jython 2. Six Lines

This Example, Six Lines, will produce the same output as Java Example 2, except now it is written in Jython.


# ex02.py
# Six Lines

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 Line
from javafx.scene.shape import StrokeLineCap

class Ex02(Application):
    def start(self, stage):
        stage.setTitle("Example 2. Six Lines")
        root = Group()
        
        # Red line - top and bottom
        redTop = Line(10, 10, 630, 10)
        redTop.setStroke(Color.RED)
        redTop.setStrokeWidth(10)
        redBot = Line(10, 470, 630, 470)
        redBot.setStroke(Color.RED)
        redBot.setStrokeWidth(10)
        root.getChildren().addAll(redTop,redBot)
        
        # Green Line -left and right
        greenLeft = Line(10, 30, 10, 450)
        greenLeft.setStroke(Color.GREEN)
        greenLeft.setStrokeWidth(10)
        greenLeft.setStrokeLineCap(StrokeLineCap.ROUND)
        greenRight = Line(630, 30, 630, 450)
        greenRight.setStroke(Color.GREEN)
        greenRight.setStrokeWidth(10)
        greenRight.setStrokeLineCap(StrokeLineCap.ROUND)
        root.getChildren().addAll(greenLeft,greenRight)
        
        # Yellow lines - diagonal lines
        yellow1 = Line(30, 30, 610, 450)
        yellow1.setStroke(Color.YELLOW)
        yellow1.setStrokeWidth(10)
        yellow1.getStrokeDashArray().addAll(20.0, 10.0, 20.0)
        yellow2 = Line(30, 450, 610, 30)
        yellow2.setStroke(Color.YELLOW)
        yellow2.setStrokeWidth(10)
        yellow2.getStrokeDashArray().addAll(20.0, 10.0, 20.0)
        root.getChildren().addAll(yellow1,yellow2)
        
        scene = Scene(root, 640, 480, Color.CYAN)
        stage.setScene(scene)
        stage.show()

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

Friday, January 20, 2017

Jython 1. Four Buttons in a Grid

The Java Example 1, Four Buttons in a Grid, is written again, however, now in Jython.


# ex01.py
# Four Buttons in a Grid

from javafx.application import Application
from javafx.scene import Scene
from javafx.scene.layout import GridPane
from javafx.geometry import Pos

def select(i):
    print "The button selected is",i
        
class Ex01(Application):
    def start(self, stage):
        stage.setTitle("Example 1. Four Buttons in a Grid")

        grid = GridPane()
        grid.setVgap(100)
        grid.setHgap(100)
        grid.setAlignment(Pos.CENTER)

        from javafx.scene.control import Button
        button1 = Button("Button 1")
        GridPane.setConstraints(button1, 0, 0)
        button1.setOnAction(lambda e: select(1))
        
        button2 = Button("Button 2")
        GridPane.setConstraints(button2, 1, 0)
        button2.setOnAction(lambda e: select(2))
        
        button3 = Button("Button 3")
        GridPane.setConstraints(button3, 0, 1)
        button3.setOnAction(lambda e: select(3))
        
        button4 = Button("Button 4")
        GridPane.setConstraints(button4, 1, 1)
        button4.setOnAction(lambda e: select(4))
        
        grid.getChildren().addAll(button1, button2, button3, button4)
        scene = Scene(grid, 640, 480)
        stage.setScene(scene)
        stage.show()

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

The output is the same with the result being printed out for which button is clicked.