Created
June 13, 2012 14:24
-
-
Save skrb/2924399 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.javainthebox.jfx.simplepresenter; | |
import java.io.IOException; | |
import javafx.animation.TranslateTransition; | |
import javafx.application.Application; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.scene.Group; | |
import javafx.scene.Node; | |
import javafx.scene.Scene; | |
import javafx.scene.image.Image; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.input.MouseEvent; | |
import javafx.stage.Stage; | |
import javafx.stage.StageStyle; | |
import javafx.util.Duration; | |
public class SimpleImagePresenter extends Application { | |
private static final double WIDTH = 800.0; | |
private static final double HEIGHT = 600.0; | |
// 表示するページ群 | |
private String[] pages = { | |
"page1.jpg", | |
"page2.jpg", | |
"page3.jpg" | |
}; | |
// 現在表示しているページ番号 | |
private int pageIndex; | |
// シーングラフのルート要素 | |
private Group root; | |
@Override | |
public void start(Stage stage) throws Exception { | |
// ステージを透明にする | |
stage.initStyle(StageStyle.TRANSPARENT); | |
root = new Group(); | |
root.setOnMouseClicked(new EventHandler<MouseEvent>() { | |
@Override | |
public void handle(MouseEvent event) { | |
try { | |
// マウスクリックされたら、次のページへ | |
goForward(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
}); | |
Scene scene = new Scene(root, WIDTH, HEIGHT); | |
scene.setFill(null); | |
stage.setScene(scene); | |
stage.show(); | |
// 最初のページを表示する | |
goForward(); | |
} | |
// ページを進める | |
private void goForward() throws IOException { | |
// 次のページをロードして、表示する | |
String imageURL = getClass().getResource(pages[pageIndex]).toString(); | |
Image image = new Image(imageURL); | |
ImageView next = new ImageView(image); | |
root.getChildren().add(next); | |
// 前のページが存在していれば、presentに代入 | |
Node present = null; | |
if (root.getChildren().size() > 1) { | |
present = root.getChildren().get(0); | |
} | |
// ページ遷移のアニメーションを行う | |
translatePage(next, present); | |
// ページインデックスを進める | |
// 最後までいったら最初に戻す | |
pageIndex++; | |
if (pageIndex >= pages.length) { | |
pageIndex = 0; | |
} | |
} | |
// ページ遷移アニメーション | |
private void translatePage(Node next, final Node present) { | |
// 新しいページを右からスライドさせるアニメーション | |
TranslateTransition slidein | |
= new TranslateTransition(new Duration(1000)); | |
slidein.setNode(next); | |
slidein.setFromX(WIDTH); | |
slidein.setToX(0); | |
slidein.play(); | |
if (present != null) { | |
// 現在表示しているページがあれば、 | |
// 左にスライドさせる | |
TranslateTransition slideout | |
= new TranslateTransition(new Duration(1000)); | |
slideout.setNode(present); | |
slideout.setToX(-WIDTH); | |
slideout.setOnFinished(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
// アニメーションが終了したら、 | |
// シーングラフから削除する | |
root.getChildren().remove(present); | |
} | |
}); | |
slideout.play(); | |
} | |
} | |
public void main(String... args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment