JythonでVanishingCircles
久しぶりにJythonです。
Jython 2.7beta1が出ていましたので前回のJythonエントリーと同じようにインストールとNetBeans 6.7の設定を行いました。
JythonからJavaFX 2を利用する
NetBeansからJythonFX
ダウンロードはこちら
Download Jython 2.7beta1 - Traditional Installer
Vanishing Circlesは文字通り消える円という事でマウスクリックしたターゲットの円が3秒間で消えます。
Stephen ChinさんがScalaFXやVisageのサンプルコードとして書いていますね。これのJython版です。
Hacking JavaFX with Groovy Clojure Scala and Visage
マウスクリックすると円が3秒間で消える関数(PyListを使っているところがJythonらしいです。)
def handle2(event): pyList3 = [KeyValue(event.target.radiusProperty(), 0)] timeline = Timeline() timeline.getKeyFrames().addAll(KeyFrame(Duration.seconds(3), pyList3)) timeline.play()
コード全体はこんな感じです。
VanishingCirclesJython.py
#!/usr/bin/env jython # -*- coding: utf-8 -*- import sys from java.lang.Math import random from javafx.animation import KeyFrame from javafx.animation import KeyValue from javafx.animation import Timeline from javafx.scene.paint import Color from javafx.application import Application from javafx.scene import Group from javafx.scene import Scene from javafx.scene.effect import BoxBlur from javafx.scene.shape import Circle from javafx.scene.shape import StrokeType from javafx.util import Duration from javafx.scene.input import MouseEvent from javafx.beans.binding import Bindings def handle2(event): pyList3 = [KeyValue(event.target.radiusProperty(), 0)] timeline = Timeline() timeline.getKeyFrames().addAll(KeyFrame(Duration.seconds(3), pyList3)) timeline.play() class VanishingCirclesJython(Application): def start(self, stage): root = Group() scene = Scene(root, 800, 600, Color.BLACK) stage.setScene(scene) circles = Group() for i in range(50): circle = Circle(150, Color.web("white", 0.05)) circle.setCenterX(random() * 800) circle.setCenterY(random() * 600) circle.setFill(Color(random(), random(), random(), .2)) circle.setEffect(BoxBlur(10, 10, 3)) circle.setStrokeType(StrokeType.OUTSIDE) circle.setStroke(Color.web("white", 0.16)) circle.setStrokeWidth(4) circle.strokeWidthProperty().bind(Bindings.when(circle.hoverProperty()).then(7).otherwise(0)) circles.getChildren().add(circle) circle.addEventHandler(MouseEvent.MOUSE_CLICKED, handle2) root.getChildren().add(circles) stage.setTitle("Vanishing Circles by Jython") stage.show() moveCircles = Timeline() moveCircles.setCycleCount(Timeline.INDEFINITE) moveCircles.setAutoReverse(1) for circle in circles.getChildren(): print circle pyList = [KeyValue(circle.centerXProperty(), random() * 800), KeyValue(circle.centerYProperty(), random() * 600)] pyList2 = [KeyValue(circle.centerXProperty(), random() * 800),KeyValue(circle.centerYProperty(), random() * 600)] moveCircles.getKeyFrames().addAll(KeyFrame(Duration.ZERO, pyList ),KeyFrame(Duration(40000), pyList2 )) moveCircles.play() if __name__ == '__main__': Application.launch(VanishingCirclesJython().class, sys.argv[1:])