KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > animation > examples > intro > Intro


1 /*
2  * Copyright (c) 2001-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.animation.examples.intro;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.Container JavaDoc;
35 import java.awt.Dimension JavaDoc;
36
37 import javax.swing.JFrame JavaDoc;
38 import javax.swing.JOptionPane JavaDoc;
39
40 import com.jgoodies.animation.AnimationUtils;
41 import com.jgoodies.animation.Animator;
42
43 /**
44  * The main class for the JGoodies Animation's Intro demo.
45  * Builds a frame, forwards the content building to the
46  * <code>IntroPage</code> and runs the application.
47  *
48  * @author Karsten Lentzsch
49  * @version $Revision: 1.4 $
50  */

51 public final class Intro {
52     
53     /**
54      * The frame rate used if no runtime argument has been specified.
55      */

56     private static final int DEFAULT_FRAME_RATE = 30;
57
58     /**
59      * Refers to the page that renders the intro.
60      */

61     private IntroPage introPage;
62     
63     
64     public static void main(String JavaDoc[] args) {
65         // Try to get the framerate from the command-line.
66
int fps = DEFAULT_FRAME_RATE;
67         if (args.length > 0) {
68             try {
69                 fps = Integer.parseInt(args[0]);
70             } catch (NumberFormatException JavaDoc e) {
71                 System.out.println("Could not parse the custom frame rate: " + args[0]);
72             }
73         }
74         System.out.println("The desired framerate is " + fps + '.');
75         new Intro(fps);
76     }
77     
78     
79     /**
80      * Constructs an animation demo using the specified frame rate.
81      *
82      * @param fps the animation's frame rate
83      */

84     private Intro(int fps) {
85         buildFrame();
86
87         Runnable JavaDoc runnable = new Runnable JavaDoc() {
88             public void run() {
89                 JOptionPane.showMessageDialog(null, "The End.");
90             }
91         };
92         
93         AnimationUtils.invokeOnStop(introPage.animation(), runnable);
94         Animator animator = new Animator(introPage.animation(), fps);
95         animator.start();
96     }
97
98
99     // Building ***************************************************************
100

101     /**
102      * Builds and opens the demo frame.
103      */

104     private void buildFrame() {
105         JFrame JavaDoc frame = new JFrame JavaDoc();
106         frame.setContentPane(buildContent());
107         frame.setSize(350, 150);
108         frame.setTitle("JGoodies Animation :: Intro");
109         frame.setDefaultCloseOperation(3); // EXIT_ON_CLOSE
110
locateOnScreenCenter(frame);
111         frame.setVisible(true);
112     }
113     
114
115     /**
116      * Builds and answers the frame's content pane.
117      *
118      * @return the frame's content pane
119      */

120     private Container JavaDoc buildContent() {
121         introPage = new IntroPage();
122         return introPage.build();
123     }
124
125
126     // Helper Code ***********************************************************
127

128     /**
129      * Locates the given component on the screen's center.
130      *
131      * @param component the component to be centered
132      */

133     private void locateOnScreenCenter(Component JavaDoc component) {
134         Dimension JavaDoc paneSize = component.getSize();
135         Dimension JavaDoc screenSize = component.getToolkit().getScreenSize();
136         component.setLocation(
137             (screenSize.width - paneSize.width) / 2,
138             (screenSize.height - paneSize.height) / 2);
139     }
140
141
142 }
Popular Tags