001/* 002 003*/ 004 005package renderer.gui; 006import renderer.framebuffer.FrameBuffer; 007 008import java.awt.event.*; 009import javax.swing.JFrame; 010import java.awt.BorderLayout; 011import javax.swing.Timer; 012 013/** 014 This class allows our renderer to implement an animation. 015 This class is an {@link InteractiveFrame} with an added 016 {@link Timer} that controls the frames-per-second rate 017 of the animation. 018*/ 019@SuppressWarnings("serial") 020public class AnimationFrame extends InteractiveFrame 021{ 022 private Timer timer; 023 private int fps; 024 025 /** 026 Associate a {@link Timer} object to an {@link InteractiveFrame} 027 window. Use the {@link Timer}'s {@link ActionEvent}s to drive 028 an animation. 029 030 @param title title for the {@link JFrame} window 031 @param fbWidth width for the initial {@link FrameBuffer} used by this {@link JFrame} 032 @param fbHeight height for the initial {@link FrameBuffer} used by this {@link JFrame} 033 @param fps the frames-per-second rate for this animation 034 */ 035 public AnimationFrame(String title, int fbWidth, int fbHeight, int fps) 036 { 037 super(title, fbWidth, fbHeight); 038 039 this.fps = fps; 040 timer = new Timer(1000/fps, this); 041 timer.start(); 042 } 043 044 045 /** 046 Use this method to get the current frames-per-second rate 047 048 @return the frames-per-second rate for this animation 049 */ 050 public int getFPS() 051 { 052 return fps; 053 } 054 055 056 /** 057 Use this method to speed up or slow down the animation. 058 059 @param fps the frames-per-second rate for this animation 060 */ 061 public void setFPS(int fps) 062 { 063 this.fps = fps; 064 timer.stop(); 065 if (fps > 0) 066 { 067 timer = new Timer(1000/fps, this); 068 timer.start(); 069 } 070 } 071 072 073 /** 074 Implement the {@link ActionListener} interface for the {@link Timer}. 075 <p> 076 Override this method to specify how an animation updates its 077 {@link renderer.scene.Scene} object in response to the firing 078 of an {@link ActionEvent} from the {@link Timer}. 079 080 @param e {@link ActionEvent} from this animation's {@link Timer} object 081 */ 082 @Override public void actionPerformed(ActionEvent e) 083 { 084 System.out.println(e); 085 this.repaint(); 086 } 087}