/
alex_ep0411
/
java_lab3
Обзор
Документация
Войти
/
alex_ep0411
/
java_lab3
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/SlideshowController.java
285 строк
9 KB
александр
лабораторная работа 3
08 июн 2026, 12:17
08 июн 2026, 12:17
3ce77bb
Код
Авторство
О чём код?
import javafx.animation.FadeTransition; import javafx.animation.RotateTransition; import javafx.animation.ScaleTransition; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.util.Duration; import java.io.File; import java.util.ArrayList; import java.util.List; public class SlideshowController { @FXML private ImageView imageView; @FXML private Button prevButton; @FXML private Button nextButton; @FXML private Button firstButton; @FXML private Button lastButton; @FXML private Button autoButton; @FXML private ComboBox<String> filterBox; @FXML private ComboBox<String> effectBox; // Новый ComboBox для выбора эффекта @FXML private Label positionLabel; @FXML private Label infoLabel; private ImageIterator iterator; private List<File> allImages; private Thread autoThread; private boolean autoRunning = false; @FXML public void initialize() { iterator = new ImageIterator(); // Настройка фильтров filterBox.getItems().addAll("Все", "Только JPG", "Только PNG"); filterBox.setValue("Все"); // Настройка эффектов effectBox.getItems().addAll("Исчезание", "Масштабирование", "Поворот"); effectBox.setValue("Исчезание"); loadAllImages(); updateDisplay(); } @FXML private void onPrevButtonClick() { navigateWithAnimation(-1); } @FXML private void onNextButtonClick() { navigateWithAnimation(1); } @FXML private void onFirstButtonClick() { if (iterator.isEmpty()) return; iterator.first(); updateDisplay(); } @FXML private void onLastButtonClick() { if (iterator.isEmpty()) return; iterator.last(); updateDisplay(); } @FXML private void onAutoButtonClick() { toggleAutoSlideshow(); } @FXML private void onFilterChanged() { applyFilter(); } private void loadAllImages() { allImages = new ArrayList<>(); File folder = new File("images"); if (!folder.exists()) { folder = new File("./images"); } if (!folder.exists()) { folder = new File(System.getProperty("user.dir") + "/images"); } if (!folder.exists()) { infoLabel.setText("Папка 'images' не найдена!"); folder.mkdir(); return; } File[] files = folder.listFiles(); if (files != null) { for (File file : files) { String name = file.getName().toLowerCase(); if (name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".png") || name.endsWith(".gif")) { allImages.add(file); } } } applyFilter(); } private void applyFilter() { List<File> filtered = new ArrayList<>(); String filter = filterBox.getValue(); if (filter == null) filter = "Все"; for (File file : allImages) { String name = file.getName().toLowerCase(); if (filter.equals("Все")) { filtered.add(file); } else if (filter.equals("Только JPG") && (name.endsWith(".jpg") || name.endsWith(".jpeg"))) { filtered.add(file); } else if (filter.equals("Только PNG") && name.endsWith(".png")) { filtered.add(file); } } iterator.setImages(filtered); updateDisplay(); } private void navigateWithAnimation(int direction) { if (iterator.isEmpty()) return; String selectedEffect = effectBox.getValue(); if (selectedEffect == null) selectedEffect = "Исчезание"; switch (selectedEffect) { case "Исчезание": navigateWithFadeAnimation(direction); break; case "Масштабирование": navigateWithScaleAnimation(direction); break; case "Поворот": navigateWithRotateAnimation(direction); break; default: navigateWithFadeAnimation(direction); } } // Эффект 1: Исчезание/появление private void navigateWithFadeAnimation(int direction) { FadeTransition fadeOut = new FadeTransition(Duration.millis(200), imageView); fadeOut.setFromValue(1.0); fadeOut.setToValue(0.0); fadeOut.setOnFinished(e -> { changeImage(direction); FadeTransition fadeIn = new FadeTransition(Duration.millis(200), imageView); fadeIn.setFromValue(0.0); fadeIn.setToValue(1.0); fadeIn.play(); }); fadeOut.play(); } // Эффект 2: Масштабирование private void navigateWithScaleAnimation(int direction) { ScaleTransition scaleOut = new ScaleTransition(Duration.millis(200), imageView); scaleOut.setFromX(1.0); scaleOut.setFromY(1.0); scaleOut.setToX(0.5); scaleOut.setToY(0.5); scaleOut.setOnFinished(e -> { changeImage(direction); ScaleTransition scaleIn = new ScaleTransition(Duration.millis(200), imageView); scaleIn.setFromX(0.5); scaleIn.setFromY(0.5); scaleIn.setToX(1.0); scaleIn.setToY(1.0); scaleIn.play(); }); scaleOut.play(); } // Эффект 3: Поворот (НОВЫЙ!) private void navigateWithRotateAnimation(int direction) { RotateTransition rotateOut = new RotateTransition(Duration.millis(300), imageView); rotateOut.setFromAngle(0); rotateOut.setToAngle(direction == 1 ? 360 : -360); rotateOut.setCycleCount(1); rotateOut.setOnFinished(e -> { // Возвращаем в исходное положение перед сменой картинки imageView.setRotate(0); changeImage(direction); // Небольшой эффект появления с поворотом RotateTransition rotateIn = new RotateTransition(Duration.millis(200), imageView); rotateIn.setFromAngle(direction == 1 ? -180 : 180); rotateIn.setToAngle(0); rotateIn.play(); }); rotateOut.play(); } // Общий метод для смены изображения private void changeImage(int direction) { if (direction == 1) { iterator.next(); } else { iterator.previous(); } updateDisplay(); } private void toggleAutoSlideshow() { if (autoRunning) { autoRunning = false; if (autoThread != null) { autoThread.interrupt(); } autoButton.setText("▶ Авто"); } else { autoRunning = true; autoButton.setText("⏸ Стоп"); startAutoSlideshow(); } } private void startAutoSlideshow() { autoThread = new Thread(() -> { while (autoRunning) { try { Thread.sleep(3000); javafx.application.Platform.runLater(() -> { if (!iterator.isEmpty()) { navigateWithAnimation(1); } }); } catch (InterruptedException e) { break; } } }); autoThread.setDaemon(true); autoThread.start(); } private void updateDisplay() { if (iterator.isEmpty()) { imageView.setImage(null); positionLabel.setText("0 из 0"); infoLabel.setText("Нет изображений в папке 'images'"); return; } try { Image image = iterator.getCurrentImage(); if (image != null) { imageView.setImage(image); } positionLabel.setText(iterator.getCurrentIndex() + " из " + iterator.getTotalCount()); File currentFile = iterator.getCurrentFile(); if (currentFile != null) { infoLabel.setText(String.format("📷 %s | 💾 %d KB | 📅 %td.%<tm.%<tY", currentFile.getName(), currentFile.length() / 1024, currentFile.lastModified())); } } catch (Exception e) { infoLabel.setText("Ошибка: " + e.getMessage()); } } }