Created
December 20, 2013 14:23
-
-
Save skrb/8055438 to your computer and use it in GitHub Desktop.
Zoetrope: Animation using touch interface
This file contains 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.zoetrope; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
public class RingBuffer<T> { | |
final private List<T> buffer; | |
private int index = -1; | |
public RingBuffer(Collection<T> collection) { | |
buffer = new ArrayList<>(collection); | |
} | |
// 次の要素を取得する | |
// 最後まで到達したら、最初に戻る | |
public T next() { | |
index++; | |
if (index >= buffer.size()) { | |
index = 0; | |
} | |
T element = buffer.get(index); | |
return element; | |
} | |
// 前の要素を取得する | |
// 最初まで到達したら、最後に戻る | |
public T previous() { | |
index--; | |
if (index < 0) { | |
index = buffer.size() - 1; | |
} | |
T element = buffer.get(index); | |
return element; | |
} | |
} |
This file contains 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.zoetrope; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import javafx.application.Application; | |
import javafx.geometry.Pos; | |
import javafx.scene.Scene; | |
import javafx.scene.image.Image; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.layout.HBox; | |
import javafx.stage.Stage; | |
public class Zoetrope extends Application { | |
private List<ImageView> views; | |
private RingBuffer<Image> images; | |
private double diff = 0.0; | |
private static final double CHANGE_THRESHOLD = 100.0; | |
@Override | |
public void start(Stage stage) { | |
HBox root = new HBox(10.0); | |
root.setAlignment(Pos.CENTER); | |
// ImageView オブジェクトを 10 作成して、リストに格納する | |
views = IntStream.range(0, 10) | |
.mapToObj(i -> new ImageView()) | |
.collect(Collectors.toList()); | |
root.getChildren().addAll(views); | |
// 9個のファイルを読み込んでリストに格納し、それをリングバッファにする | |
images = new RingBuffer( | |
IntStream.range(1, 10) | |
.mapToObj(i -> new Image(getClass().getResource("resources/T" + i + ".gif").toString())) | |
.collect(Collectors.toList())); | |
// ImageオブジェクトをImageViewオブジェクトにセットする | |
views.forEach(view -> view.setImage(images.next())); | |
initScrollEventHandler(root); | |
Scene scene = new Scene(root, 800, 200); | |
stage.setTitle("Zoetrope"); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
private void initScrollEventHandler(HBox root) { | |
// スクロール開始時に移動量をリセットしておく | |
root.setOnScrollStarted(e -> diff = 0.0); | |
// 移動量がしきい値 CHANGE_THRESHOLD を超えたらイメージを切り替える | |
root.setOnScroll(e -> { | |
// 前回からの移動量の差分を加える | |
diff += e.getDeltaX(); | |
if (diff >= CHANGE_THRESHOLD) { // 右方向のスクロール | |
diff = 0; | |
// イメージの切り替え | |
views.forEach(view -> view.setImage(images.next())); | |
} else if (diff <= -CHANGE_THRESHOLD) { // 左方向のスクロール | |
diff = 0; | |
// イメージを戻すように切り替える | |
IntStream.iterate(views.size() - 1, i -> i-1) | |
.limit(views.size()) | |
.mapToObj(i -> views.get(i)) | |
.forEach(view -> view.setImage(images.previous())); | |
} | |
}); | |
} | |
public static void main(String... args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment