import java.awt.*; import java.awt.event.*; /** TGA file animation viewer * Master's Thesis, Univ of Colo, 2001 * Reads a set of TGA files frame1, frame2... * and displays them rapidly for animation * @author: Phil Gage */ public class ShowMovie extends Panel { /** Total number of frames */ int frames = 0; /** Current frame number */ int frame = 0; /** Initialization flag */ boolean init = false; /** Maximum number of image frames */ final int MAXFRAMES = 100; /** Image pixmap array for graphics */ Image img[] = new Image [MAXFRAMES]; /** Graphic window to display animation */ public static Frame f; /** Delay in msec to slow animation */ static int delay = 0; /** * Method loadImages reads the TGA files into the image array. */ void loadImages () { String filename; boolean success; FrameBuffer fb = new FrameBuffer (); for (int i=0; i<=MAXFRAMES; i++) { filename = "frame" + i + ".tga"; success = fb.loadtga (filename); if (!success) break; if (i == MAXFRAMES) { System.out.println ("Frame limit exceeded: " + i); break; } img[i] = fb.getImage (f); frames++; System.out.println (filename); } if (frames == 0) { System.out.println ("No image files found"); System.exit (0); } System.out.println (frames + " frames loaded"); // Resize window to image size f.setSize (fb.width,fb.height); } /** * Method paint redraws window each time window is exposed. * This version saves graphics in images for fast redraw. * @param g graphics object */ public void paint (Graphics g) { // System.out.println ("in paint"); // Initialize image once if (!init) { init = true; loadImages (); } // Refresh image g.drawImage (img[frame], 0, 0, getWidth(), getHeight(), this); repaint(); } /** * Method update redraws window each time after repaint. * This overrides default method that erases window and. * causes an annoying flicker when white background shows. * @param g graphics object */ public void update (Graphics g) { if (++frame >= frames) frame=0; g.drawImage (img[frame], 0, 0, getWidth(), getHeight(), this); if (delay > 0) { try { Thread.sleep(delay); //delay } catch(InterruptedException e) {} } if (frames > 1) repaint(); } /** * Main program for command line application. * Adds window close event handler. * Creates panel in frame and displays image. * @param argv command line argument strings */ public static void main (String[] argv) { System.out.println ("TGA Animation viewer by Phil Gage"); System.out.println ("Master's Thesis, Univ of Colo, 2002"); System.out.println ("Usage: java ShowMovie [delay]"); System.out.println ("Optional delay in msec"); System.out.println ("Reads files frame0.tga, frame1.tga..."); // Get optional delay between frames if (argv.length > 0) { try { delay = new Integer(argv[0].trim()).intValue(); } catch(Exception e) { System.out.println ("Invalid delay"); System.exit (0); } } // Create frame and app f = new Frame ("Java Ray Tracer Animation"); ShowMovie app = new ShowMovie (); // Add window close event handler f.addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent e) { System.exit (0); } }); // Put panel in frame f.add ("Center", app); f.setSize (320,200); f.show (); } }