Jose Martinez
Jose Martinez

Reputation: 23

Making a slideshow JAVA

My prompt is the following:

Write a program that takes the names of several image files as command-line arguments and displays them in a slide show (one every two seconds), using a fade effect to black and a fade from black between pictures.

I have the portion that fades the image but I'm having a problem that keeps all the images under one window. For example, when I run my program it will open a new window - fade picture A to the black picture. Open a new window with the black image and then fade to picture c. I'm trying to have it start from picture A, fade to black, and then fade into the new picture without opening a new window. I know it has to do with my pic.show() code but I'm not sure how to fix this.

Here's my code:

package fade;

import edu.princeton.cs.introcs.Picture;
import java.awt.Color;

public class Fade {

    public static Color blend(Color c, Color d, double alpha) {
        double r = (1 - alpha) * c.getRed() + alpha * d.getRed();
        double g = (1 - alpha) * c.getGreen() + alpha * d.getGreen();
        double b = (1 - alpha) * c.getBlue() + alpha * d.getBlue();
        return new Color((int) r, (int) g, (int) b);
    }

    public static void pause(int t) {
    try { Thread.sleep(t); }
    catch (InterruptedException e) { System.out.println("Error sleeping"); }
}


    public static void main(String[] args) {
        for (int k = 1; k < args.length; k++) {
            Picture source = new Picture(args[k]);
            Picture target = new Picture(args[0]);
            int M = 100;
            int width = source.width();
            int height = source.height();
            Picture pic = new Picture(width, height);
            for (int t = 0; t <= M; t++) {
                for (int i = 0; i < width; i++) {
                    for (int j = 0; j < height; j++) {
                        Color c0 = source.get(i, j);
                        Color cM = target.get(i, j);
                        Color c = blend(c0, cM, (double) t / M);
                        pic.set(i, j, c);
                    }
                }
                pic.show();
            }

        }

    }
}

Upvotes: 0

Views: 1113

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

Since you're only using a few images.

  • Create a Java Swing JFrame with a JPanel. Extend the JPanel so you can override the paintComponent method and paint a java.awt.image.BufferedImage.

  • Using javax.imageio.ImageIO, read each picture into a BufferedImage.

  • Create one BufferedImage that's all black.

  • Cycle through the BufferedImages.

Upvotes: 1

Related Questions