-1

I am making a program where there is 3 images in it and it goes through a slideshow each image showing two seconds. I have the seconds different than what they should be currently. But I am having a problem with my line of code declaring the KeyFrame. I put all my code bellow. Any suggestions on what I should change with keyframe or anything else additionally in my code. This is using JavaFX.

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package digitalpictureframe;

import java.io.File;
//import java.time.Duration;
import java.util.Arrays;
import javafx.util.Duration;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
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.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author Zachary Murphy
 */
public class DigitalPictureFrame extends Application {


    @Override
    public void start(Stage primaryStage) {

        Image image1 = new Image("1.png");
        Image image2 = new Image("2.png");
        Image image3 = new Image("3.png");
        ImageView imageView = new ImageView();
        Timeline timeline = new Timeline(

                new KeyFrame(Duration.ZERO, new KeyValue(imageView.imageProperty(), image1)),
            new KeyFrame(Duration.seconds(1), new KeyValue(imageView.imageProperty(), image2)),  
            new KeyFrame(Duration.seconds(2), new KeyValue(imageView.imageProperty(), image3)),
            new KeyFrame(Duration.seconds(4), new KeyValue(imageView.imageProperty(), null))
            );
        timeline.play();
        StackPane root = new StackPane();
        root.getChildren().add(imageView);
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}v
ztmcoder
  • 119
  • 1
  • 7
  • The other questions post does not help me at all... – ztmcoder Mar 11 '16 at 21:46
  • It says that not suitable constructor for keyframe. How do I fix that? – ztmcoder Mar 11 '16 at 21:47
  • @jewelsea the other question is NOT answered my keyframes have an error. I seen that question before and tried the solution hence why I am asking it! – ztmcoder Mar 11 '16 at 22:25
  • OK, I think it is essentially the same as [Timeline to Display Random images one by one](http://stackoverflow.com/questions/35869192/timeline-to-display-random-images-one-by-one), but I'll reopen this question for now in case I am wrong. BTW it looks like you have edited to your question to include code similar to incorrect code from the related question, rather than basing your updated question code on one of the correct answers to that question. – jewelsea Mar 11 '16 at 23:16
  • I am implementing the solution for the other one. Event the checked answer does not work when implemented. – ztmcoder Mar 11 '16 at 23:27
  • 1
    What "does not work" mean? Both answers to the related question work for me (OS X 10.9.5, Java Runtime 8u74). If you are experiencing errors or odd behavior, in your question you should include the compile error or runtime stack trace together with a description of the unexpected behavior and a detailed description of your environment (JDK version used, etc). – jewelsea Mar 11 '16 at 23:35
  • error: no suitable constructor found for KeyFrame(java.time.Duration,KeyValue) new KeyFrame(Duration.ZERO, new KeyValue(imageView.imageProperty(), image1)), constructor KeyFrame.KeyFrame(javafx.util.Duration,String,EventHandler,KeyValue...) is not applicable (argument mismatch; java.time.Duration cannot be converted to javafx.util.Duration) constructor KeyFrame.KeyFrame(javafx.util.Duration,EventHandler,KeyValue...) is not applicable (argument mismatch; java.time.Duration cannot be converted to javafx.util.Duration) – ztmcoder Mar 11 '16 at 23:42
  • constructor KeyFrame.KeyFrame(javafx.util.Duration,String,KeyValue...) is not applicable (argument mismatch; java.time.Duration cannot be converted to javafx.util.Duration) constructor KeyFrame.KeyFrame(javafx.util.Duration,KeyValue...) is not applicable (argument mismatch; java.time.Duration cannot be converted to javafx.util.Duration) – ztmcoder Mar 11 '16 at 23:42
  • error: cannot find symbol new KeyFrame(Duration.seconds(1), new KeyValue(imageView.imageProperty(), image2)), symbol: method seconds(int) location: class Duration – ztmcoder Mar 11 '16 at 23:43
  • error: cannot find symbol new KeyFrame(Duration.seconds(2), new KeyValue(imageView.imageProperty(), image3)), symbol: method seconds(int) location: class Duration – ztmcoder Mar 11 '16 at 23:43
  • rror: cannot find symbol new KeyFrame(Duration.seconds(4), new KeyValue(imageView.imageProperty(), null)) symbol: method seconds(int) location: class Duration Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 4 errors – ztmcoder Mar 11 '16 at 23:43
  • Using JDK 8 and 1.8_73 – ztmcoder Mar 11 '16 at 23:43
  • That is exactly the output error when program is ran. – ztmcoder Mar 11 '16 at 23:44
  • I even created a new project with the exact code from the "solution" and did the same thing. – ztmcoder Mar 11 '16 at 23:44
  • You just have the wrong import: you have `import java.time.Duration;` instead of the javafx one. The error message (which you should include in your question) tells you this explicitly. – James_D Mar 11 '16 at 23:47
  • What is the javafx one then? – ztmcoder Mar 11 '16 at 23:48
  • import javafx.util.Duration;??? – ztmcoder Mar 11 '16 at 23:49
  • If i use the javafx.util.Duration i get more errors. – ztmcoder Mar 11 '16 at 23:52
  • Executing C:\Users\Zachary Murphy\Documents\GitHub\Digital-Picture-Frame\DigitalPictureFrame\dist\run556729355\DigitalPictureFrame.jar using platform C:\Program Files\Java\jdk1.8.0_73\jre/bin/java Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) – ztmcoder Mar 11 '16 at 23:53
  • at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) – ztmcoder Mar 11 '16 at 23:53
  • at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:745) – ztmcoder Mar 11 '16 at 23:53
  • aused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found at javafx.scene.image.Image.validateUrl(Image.java:1118) at javafx.scene.image.Image.(Image.java:620) at digitalpictureframe.DigitalPictureFrame.start(DigitalPictureFrame.java:42) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) – ztmcoder Mar 11 '16 at 23:54
  • t com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) – ztmcoder Mar 11 '16 at 23:54
  • .. 1 more Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found at javafx.scene.image.Image.validateUrl(Image.java:1110) ... 11 more Exception running application digitalpictureframe.DigitalPictureFrame Java Result: 1 – ztmcoder Mar 11 '16 at 23:54
  • 2
    Better to edit the question and place stack traces formatted in there rather than in comments. – jewelsea Mar 11 '16 at 23:57
  • Please [edit] the question to show the updated code and the full stack trace. Instead of posting every error you get on a forum, it will probably help you to learn how to read them and understand what they are saying. [This](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) question may be very useful to you. – James_D Mar 11 '16 at 23:58
  • You have to make the images available, if you are going to use the ones named 1.png, 2.png, etc. from the question then you need to actually have images named that and they need to be placed in the same directory as your class files are. Or you can use the http based urls from the other answer, then the code will pull the images dynamically off the net rather than from the java class path. – jewelsea Mar 11 '16 at 23:59
  • I have the images in the class folder and still giving errors? – ztmcoder Mar 12 '16 at 00:14
  • Updated my code too. – ztmcoder Mar 12 '16 at 00:15

1 Answers1

0

The following code is for the image slideshow. It loops through 25 images and pause with click and start again with another click. I also used the Fade Animation between images. Each image will stay for 2 seconds.

import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class lab12 extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    // Global ImageView array variable.
    ImageView[] imgView = new ImageView[25];
    int imgIndex = 0;

    public void start(Stage stage) {

        Pane pane = new Pane();

        for (int i = 0; i < 25; i++) {
            imgView[i] = new ImageView(new Image("imagescards/" + i + ".jpg"));
            imgView[i].setFitWidth(600);
            imgView[i].setFitHeight(600);

        }

        pane.getChildren().add(imgView[imgIndex]);

        EventHandler<ActionEvent> eventHandler = e -> {
            if (imgIndex < 24) {
                // Adding Children
                pane.getChildren().remove(imgView[imgIndex]);
                imgIndex++;
                pane.getChildren().add(imgView[imgIndex]);
                FadeTransition ft = new FadeTransition(Duration.millis(1000), imgView[imgIndex]);
                ft.setFromValue(0);
                ft.setToValue(1);
                ft.play();
            }
            else if (imgIndex == 24) {
                imgIndex = 0;
                pane.getChildren().remove(imgView[24]);
                pane.getChildren().add(imgView[imgIndex]);
                FadeTransition ft = new FadeTransition(Duration.millis(1000), imgView[imgIndex]);
                ft.setFromValue(0);
                ft.setToValue(1);
                ft.play();
            }
        };

        // Timeline Animation
        Timeline animation = new Timeline(new KeyFrame(Duration.millis(3000), eventHandler));

        animation.setCycleCount(Timeline.INDEFINITE);
        animation.play();

        pane.setOnMouseClicked(e -> {
            if (animation.getStatus() == Animation.Status.PAUSED) {
                animation.play();
            } else {
                animation.pause();
            }
        });

        Scene scene = new Scene(pane, 600, 600);

        stage.setScene(scene);
        stage.setTitle("Slide Show");
        stage.show();
    }
}
Maihan Nijat
  • 9,054
  • 11
  • 62
  • 110