2013-02-16 221 views
0

我的提示是:製作幻燈片JAVA

編寫一個程序,它的多個圖像文件作爲命令行參數的名稱,並將其顯示在幻燈片(一個每兩秒鐘),使用褪色效果到黑色和圖片之間的黑色淡化。

我有變淡的圖像,但我有,保持在一個窗口中的所有圖像有問題的部分。例如,當我運行我的程序時,它會打開一個新窗口 - 將圖片A淡入黑色圖片。用黑色圖像打開一個新窗口,然後淡入圖片c。我試圖讓它從圖片A開始,淡入黑色,然後在不打開新窗口的情況下淡入新圖片。我知道它與我的pic.show()代碼有關,但我不知道如何解決這個問題。

這裏是我的代碼:

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(); 
      } 

     } 

    } 
} 
+0

查看http://introcs.cs.princeton.edu/java/stdlib/Picture.java.html這只是Picture類的行爲。它調用'show'時爲每個實例打開一個新窗口(JFrame)。你必須改變你的方法... – home 2013-02-16 21:37:30

+0

任何想法如何解決這個問題的不同? – 2013-02-16 22:19:43

+0

對不起,我不知道這個API – home 2013-02-16 22:43:37

回答

1

既然你只使用一些圖片。

  • 使用JPanel創建Java Swing JFrame。擴展JPanel,以便覆蓋paintComponent方法並繪製java.awt.image.BufferedImage。

  • 使用javax.imageio.ImageIO中,讀取每個畫面成一個BufferedImage。

  • 創建一個BufferedImage的這一切都爲黑色。

  • 通過BufferedImages循環。