// // Sep-27 Fri: basic GUI frame for GalleryApp, // can load images, use prev/next buttons to navigate images // // Sep-30 Mon: animate through the images using Timeline (KeyFrame, Duration, Animation) // prev() and next() methods for modular design // HBox as control panel with play/stop buttons import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; import javafx.util.Duration; public class GalleryApp extends Application { public static final int APP_WIDTH = 500; public static final int APP_HEIGHT = 600; public static final int N_IMAGES = 30; public static final double INTERVAL= 0.5; // seconds private BorderPane mainPane; private HBox hbox; private Button bnPrev, bnNext; private Button bnPlay, bnStop; private Label lbImage; private Image[] imgArray; private ImageView[] ivArray; private Timeline timeline; private int currentIndex; @Override public void start(Stage stage) { setupImages(); setupPane(); setupControls(); setupControlPanel(); setupTimeline(); Scene scene = new Scene(mainPane, APP_WIDTH, APP_HEIGHT); // stage setup: title, scene, show stage.setTitle("CS112 F24 GalleryApp"); stage.setScene(scene); stage.show(); } private void setupControlPanel() { hbox = new HBox(); // TODO: improve visual bnPlay = new Button("Play"); bnStop = new Button("Stop"); bnPlay.setOnAction(e -> { timeline.play(); }); bnStop.setOnAction(e -> { timeline.stop(); }); hbox.getChildren().addAll(bnPlay, bnStop); mainPane.setTop(hbox); } private void setupTimeline() { KeyFrame frame = new KeyFrame(Duration.seconds(INTERVAL), e -> { next(); }); timeline = new Timeline(frame); timeline.setCycleCount(Animation.INDEFINITE); } private void setupImages() { imgArray = new Image[N_IMAGES]; ivArray = new ImageView[N_IMAGES]; String prefix = "file:f"; for (int i = 0; i < N_IMAGES; ++i) { String fname = prefix + (i + 1) + ".png"; imgArray[i] = new Image(fname); ivArray[i] = new ImageView(imgArray[i]); } // Q. do we need imgArray (Image[]) } private void setupPane() { mainPane = new BorderPane(); } private void setupControls() { lbImage = new Label(); // Label("Text"); Label(imgView) lbImage.setGraphic(ivArray[currentIndex]); bnPrev = new Button("<"); bnNext = new Button(">"); bnPrev.setPrefHeight(APP_HEIGHT); bnNext.setPrefHeight(APP_HEIGHT); // action listener bnPrev.setOnAction(e -> { prev(); }); bnNext.setOnAction(e -> { next(); }); mainPane.setCenter(lbImage); mainPane.setLeft(bnPrev); mainPane.setRight(bnNext); } private void prev() { --currentIndex; // wrap-around if (currentIndex < 0) { currentIndex = N_IMAGES - 1; } lbImage.setGraphic(ivArray[currentIndex]); } private void next() { ++currentIndex; // wrap-around if (currentIndex == N_IMAGES) { currentIndex = 0; } lbImage.setGraphic(ivArray[currentIndex]); } public static void main(String[] args) { launch(args); } }