 |
|
|
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 File( curDir );
ans = path.listFiles( fileFilter );
return ans;
}
private void setFullScreen()
{
GraphicsEnvironment ge;
GraphicsDevice gd;
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gd = ge.getDefaultScreenDevice();
System.out.println("isFullScreenSupported: "+gd.isFullScreenSupported());
gd.setFullScreenWindow( this );
}
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 paint( Graphics g )
{
//use float variables to avoid integer devisions
float imgWidth, imgHeight;
float scrWidth, scrHeight;
float newWidth, newHeight;
float imgRatio, scrRatio;
if( img != null )
{
//get image and frame size
imgWidth = img.getWidth(this);
imgHeight = img.getHeight(this);
scrWidth = getWidth();
scrHeight = getHeight();
//check that images was loaded
if( imgWidth > 0 && imgHeight > 0 )
{
//calculate aspect ratio
imgRatio = imgHeight/imgWidth;
scrRatio = scrHeight/scrWidth;
//the new size
if( imgRatio < scrRatio )
{
newWidth = scrWidth;
newHeight = imgHeight * (newWidth/imgWidth);
}
else
{
newHeight = scrHeight;
newWidth = imgWidth * (newHeight/imgHeight);
}
//clear the screen before a _new_ image is shown
if( newImage )
{
g.setColor( Color.BLACK );
g.fillRect(0, 0, (int)scrWidth, (int)scrHeight );
newImage = false;
}
g.drawImage(img, 0, 0, (int)newWidth, (int)newHeight, this);
}
}
}
public static void main( String [] args)
{
new SlideShow();
}
}
|
|
|
 |