Start Search Contents Index Links About
SlideShow.java
/*
 * Author: Havard Rast Blok
 * E-mail:  
 * Web   : www.rememberjava.com
 */

import java.awt.*;
import java.io.*;
import javax.swing.*;

import com.rememberjava.io.SunFileFilter;

/*
 * Application to view an image.
 */
public class SlideShow extends JWindow
{
  private SunFileFilter fileFilter;
  private Toolkit tk;
  private File files[];
  private Image img;
  private boolean newImage;

  public SlideShow()
  {
    fileFilter = new SunFileFilter(new String[]{"gif""png""jpg"});
    files = loadFileNames();
    
    tk = Toolkit.getDefaultToolkit();
    show();
    setFullScreen();
    
    showImages();
  }
  
  private File[] loadFileNames()
  {  
    String curDir = null;
    File path;
    File ans[];
    
    //get the current directory
    try
    {
      curDir = System.getProperty"user.dir" );
    }
    catch(Exception e)
    {
      System.out.println("SlideShow.loadFileNames - error getting current dir: "+e);
      System.exit(1);
    }
    
    path = new FilecurDir );
    ans = path.listFilesfileFilter );
    
    return ans;
  }

  private void setFullScreen()
  {
    GraphicsEnvironment ge;
    GraphicsDevice gd;
    
    ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    gd = ge.getDefaultScreenDevice();
    
    System.out.println("isFullScreenSupported: "+gd.isFullScreenSupported());
    gd.setFullScreenWindowthis );
  }
  
  private void showImages()
  {
    for(int i=0; i<files.length; i++)
    {
      img = tk.createImage(files[i].getAbsolutePath());
      newImage = true;
      repaint();
      
      try
      {
        Thread.sleep(8000);
      }
      catch(Exception e) {} //do nothing
    }
  }
  
  public void paintGraphics g )
  {
    //use float variables to avoid integer devisions
    float imgWidth, imgHeight;
    float scrWidth, scrHeight;
    float newWidth, newHeight;
    float imgRatio, scrRatio;
    
    ifimg != null )
    {
      //get image and frame size
      imgWidth = img.getWidth(this);
      imgHeight = img.getHeight(this);
      scrWidth = getWidth();
      scrHeight = getHeight();

      //check that images was loaded
      ifimgWidth > && imgHeight > )
      {
        //calculate aspect ratio
        imgRatio = imgHeight/imgWidth;
        scrRatio = scrHeight/scrWidth;

        //the new size
        ifimgRatio < scrRatio )
        {
          newWidth = scrWidth;
          newHeight = imgHeight * (newWidth/imgWidth);
        }
        else
        {
          newHeight = scrHeight;
          newWidth = imgWidth * (newHeight/imgHeight);
        }

        //clear the screen before a _new_ image is shown
        ifnewImage )
        {
          g.setColorColor.BLACK );
          g.fillRect(00(int)scrWidth, (int)scrHeight );
          newImage = false;
        }

        g.drawImage(img, 00(int)newWidth, (int)newHeight, this);
      }
    }
  }

  public static void mainString [] args)
  {
    new SlideShow()
  }

}


site: Håvard Rast Blok
mail:
updated: 27 July 2007