1 30 31 package com.jgoodies.animation.examples.basic; 32 33 import java.awt.*; 34 import java.util.Iterator ; 35 import java.util.LinkedList ; 36 import java.util.List ; 37 38 import javax.swing.BorderFactory ; 39 import javax.swing.JFrame ; 40 import javax.swing.JLabel ; 41 import javax.swing.JPanel ; 42 43 import com.jgoodies.animation.*; 44 import com.jgoodies.animation.animations.BasicTextAnimation; 45 import com.jgoodies.animation.components.BasicTextLabel; 46 47 56 public final class Basic { 57 58 private static final int DEFAULT_FRAME_RATE = 30; 59 60 private JLabel reportLabel; 61 private BasicTextLabel label1; 62 private BasicTextLabel label2; 63 private BasicTextLabel label3; 64 65 66 public static void main(String [] args) { 67 int fps = DEFAULT_FRAME_RATE; 69 if (args.length > 0) { 70 try { 71 fps = Integer.parseInt(args[0]); 72 } catch (NumberFormatException e) { 73 System.out.println("Could not parse the custom frame rate: " + args[0]); 74 } 75 } 76 System.out.println("The desired framerate is " + fps + '.'); 77 new Basic(fps); 78 } 79 80 81 86 private Basic(int fps) { 87 createComponents(); 88 buildFrame(); 89 90 Animator animator = new Animator(createComposedAnimation(), fps); 91 animator.start(); 92 } 93 94 95 97 100 private void createComponents() { 101 Font font = new Font("dialog", Font.BOLD, 16); 102 103 reportLabel = new JLabel (); 104 105 label1 = new BasicTextLabel(" "); 106 label1.setFont(font); 107 108 label2 = new BasicTextLabel(" "); 109 label2.setFont(font); 110 111 label3 = new BasicTextLabel(" "); 112 label3.setFont(font); 113 } 114 115 116 119 private void buildFrame() { 120 JFrame frame = new JFrame (); 121 frame.setContentPane(buildContent()); 122 frame.setSize(400, 300); 123 frame.setTitle("JGoodies Animation :: Intro Example"); 124 frame.setDefaultCloseOperation(3); locateOnScreenCenter(frame); 126 frame.setVisible(true); 127 } 128 129 130 135 private Container buildContent() { 136 JPanel panel = new JPanel (new GridLayout(4, 0)); 137 panel.add(reportLabel); 138 panel.add(label1); 139 panel.add(label2); 140 panel.add(label3); 141 panel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); 142 return panel; 143 } 144 145 146 148 154 private Animation createComposedAnimation() { 155 List animations = new LinkedList (); 156 157 animations.add(Animations.repeat(5f, new SimpleReportAnimation(reportLabel, 2000))); 158 animations.add(BasicTextAnimation.defaultFade(label1, 159 5000, 160 "Default Fade", 161 Color.darkGray)); 162 animations.add(BasicTextAnimation.defaultScale(label2, 163 4000, 164 "Default Scale", 165 Color.darkGray)); 166 animations.add(BasicTextAnimation.defaultSpace(label3, 3000, 167 "Default Space", 168 Color.darkGray)); 169 170 int count = 1; 171 for (Iterator i = animations.iterator(); i.hasNext();) { 172 Animation element = (Animation) i.next(); 173 element.addAnimationListener(new AnimationLogger("Animation " + count++)); 174 } 175 176 Animation composedAnimation = Animations.parallel(animations); 177 composedAnimation.addAnimationListener(new AnimationLogger("Composed animation")); 178 return composedAnimation; 179 } 180 181 182 184 189 private void locateOnScreenCenter(Component component) { 190 Dimension paneSize = component.getSize(); 191 Dimension screenSize = component.getToolkit().getScreenSize(); 192 component.setLocation( 193 (screenSize.width - paneSize.width) / 2, 194 (screenSize.height - paneSize.height) / 2); 195 } 196 197 private static class SimpleReportAnimation extends AbstractAnimation { 198 199 private final JLabel target; 200 201 private long frameNumber = 0; 202 private long startTime = -1; 203 204 210 private SimpleReportAnimation(JLabel target, long duration) { 211 super(duration); 212 this.target = target; 213 } 214 215 221 protected void applyEffect(long time) { 222 frameNumber++; 223 long totalTime = totalTime(); 224 float fps = totalTime < 1000 ? 0 : frameNumber * 1000 / totalTime; 225 int iteration = (int) (totalTime / duration()); 226 227 target.setText( 228 "time=" + time / 1000 + "s" 229 + "; total=" + totalTime / 1000 + "s" 230 + "; iteration=" + iteration 231 + "; frame=" + frameNumber 232 + "; fps= " + fps); 233 } 234 235 private long totalTime() { 236 if (startTime == -1) { 237 startTime = System.currentTimeMillis(); 238 } 239 return System.currentTimeMillis() - startTime; 240 } 241 242 } 243 244 245 private static class AnimationLogger extends AnimationAdapter { 247 private final String name; 248 249 AnimationLogger(String name) { this.name = name; } 250 251 public void animationStarted(AnimationEvent e) { 252 System.out.println(name + " started."); 253 } 254 public void animationStopped(AnimationEvent e) { 255 System.out.println(name + " stopped."); 256 } 257 } 258 259 260 } | Popular Tags |