JavaFXの例-TimeLineとAnimationの使用



Javafx Example Use Timeline



この2日間の仕事はとても忙しかったです。 Androidタブレットでのプロジェクトの最終リリースのため、私は前夜の11時まで残業し、朝の4時まで残業しました。そのため、Sourceforgeの前のページも作成されました。ただし、プロジェクトの最終リリースにより、今日は簡単になり、JavaFXの例をいくつか作成しました。






上記は簡単な例です。

サンプルデモアドレス: http://wjfxgame.sourceforge.net/examples/exp1/AniTest.html




[開始]をクリックしてアニメーションを開始します。


主にJavaFXでのタイムラインとアニメーションのアプリケーション。

タイムラインは、以前のブログ投稿の多くの場所で使用されています。また、タイムラインであり、キーフレームを追加することでアニメーション化されます。


そしてアニメーション、主にノードをアニメーション化するため。 JavaFXのアニメーションは、javafx.animationパッケージに含まれています。アニメーションには、タイムラインとトランジションの2つのサブクラスがあります。つまり、APIの階層関係によれば、タイムラインは一種のアニメーションです。


もちろん、タイトルにタイムラインとアニメーションを絡める必要はありません。


Transitionには、FadeTransition、FillTransition、ParallelTransition、PathTransition、PauseTransition、RotateTransition、ScaleTransition、SequentialTransition、StrokeTransition、TranslateTransitionの10個のサブクラスがあります。


実際、多くのサブクラスがありますが、アニメーションの基本的な効果は、名前の接頭辞によって判断できます。基本的に、ノードプロパティは、境界線の色、塗りつぶしの色、透明度、ズーム、回転、パン、移動、パスによる一時停止などの変更を行います。


ParallelTransitionのみが並列アニメーション(一連のアニメーションが同時に実行される)であり、SequentialTransitionは順次アニメーション(一連のアニメーションが順次実行される)です。


ソースコードを見てみましょう:

import javafx.animation.* import javafx.event.ActionEvent import javafx.event.EventHandler import javafx.scene.control.ToggleButton import javafx.scene.control.ToggleGroup import javafx.scene.effect.Bloom import javafx.scene.layout.HBox import javafx.scene.layout.Pane import javafx.scene.paint.Color import javafx.scene.shape.* import javafx.util.Duration /** * * @author wing */ public class TestPane extends Pane{ private ParallelTransition mAnimList private Timeline timeline private HBox hBox private ToggleButton start, pause, stop private ToggleGroup btnGroup private double duration = 200 public TestPane(){ btnGroup = new ToggleGroup() start = new ToggleButton('Start') start.setToggleGroup(btnGroup) start.setOnAction(new EventHandler() { @Override public void handle(ActionEvent arg0) { timeline.play() checkUIState() } }) pause = new ToggleButton('Pause') pause.setToggleGroup(btnGroup) pause.setOnAction(new EventHandler() { @Override public void handle(ActionEvent arg0) { timeline.pause() checkUIState() } }) stop = new ToggleButton('Stop') stop.setToggleGroup(btnGroup) stop.setOnAction(new EventHandler() { @Override public void handle(ActionEvent arg0) { timeline.stop() checkUIState() } }) hBox = new HBox(10) hBox.getChildren().addAll(start, pause, stop) hBox.setTranslateX((Anitest.WIDTH - 200) / 2) hBox.setTranslateY(20) getChildren().add(hBox) timeline = new Timeline() timeline.setCycleCount(Timeline.INDEFINITE) KeyFrame keyFrame = new KeyFrame(Duration.millis(duration), new EventHandler() { @Override public void handle(ActionEvent event) { createObject() } }) timeline.getKeyFrames().add(keyFrame) } /** * Check the status of the three buttons start pause stop */ public void checkUIState(){ start.setDisable(false) pause.setDisable(false) stop.setDisable(false) switch(timeline.getStatus()){ case RUNNING: start.setDisable(true) break case PAUSED: pause.setDisable(true) break case STOPPED: stop.setDisable(true) break } } /** * Create an Object and perform animation, create a ParallelTransition here, and add a TranslateTransition with random left and right translations, a FadeTransition with transparency gradually becoming 0 and a ScaleTransition gradually magnifying 0.2 times. Bind the Object you just created to the ParallelTransition that combines the three animations, and then execute ParallelTransition. */ public void createObject() { double width = Math.max(50, Math.random() * 200) double height = Math.max(50, Math.random() * 200) double x = Math.min(Math.random() * Anitest.WIDTH, Anitest.WIDTH - width) double y = Math.max(Math.random() * (Anitest.HEIGHT - 100), 100) double dx = Math.random() * 50 double dy = Math.random() * 50 final Shape shape = new Circle(x, y, width / 2) shape.setEffect(new Bloom(50)) shape.setFill(Color.rgb((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255))) getChildren().add(shape) mAnimList = new ParallelTransition( shape, TranslateTransitionBuilder.create().byX(Math.random() > 0.5 ? dx : -dx).byY(Math.random() > 0.5 ? dy : -dy).duration(Duration.millis(1000)).build(), FadeTransitionBuilder.create().toValue(0).duration(Duration.millis(1000)).build(), ScaleTransitionBuilder.create().byX(0.2).byY(0.2).duration(Duration.millis(800)).build()) mAnimList.play() mAnimList.setOnFinished(new EventHandler() { @Override public void handle(ActionEvent arg0) { getChildren().remove(shape) } }) } }


そして、アニメーションの最後に、作成したばかりのオブジェクトが削除されます。


ここでのトランジションは、それぞれのビルダーで作成されます。


ここでペインが継承される理由については、主にこれらの例がそのアプリで使用されるSourceforgeホームページサイトに追加されます。


メインクラスは次のとおりです。

import javafx.application.Application import javafx.scene.Scene import javafx.scene.layout.StackPane import javafx.scene.paint.Color import javafx.stage.Stage /** * @author wing * 2012/8/30 */ public class Anitest extends Application { public static final int WIDTH = 800 public static final int HEIGHT = 600 public static void main(String[] args) { launch(args) } @Override public void start(Stage primaryStage) { TestPane mPane = new TestPane() StackPane root = new StackPane() root.getChildren().add(mPane) Scene scene = new Scene(root, WIDTH, HEIGHT) scene.setFill(Color.BLACK) primaryStage.setScene(scene) primaryStage.setTitle('JavaFX example - use of TimeLine and Animation') primaryStage.show() } }

メインクラスは説明されていません、それは非常に基本的な内容です。


ソースを示してください: http://blog.csdn.net/ml3947/

-------------------------------------------------- -------

もともとブログにアプレットを埋め込もうとしていたのですが、長い間試してもうまくいきませんでしたが、以前注目していた牛のブログにアプレットが埋め込まれているのを見ました。なんか変な感じ。後で見る準備ができました。


そこで、デモのアドレスを直接伝えました。システムにJavaFXがインストールされていない場合は、新しいバージョンのJavaをインストールするように求められます。 JavaFXはすでにJRE7に含まれているためです。