1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SpriteRunner.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 import com
.sun
.star
.rendering
.*;
35 // HOWTO get a Graphics2D from a window
38 // import javax.swing.*;
40 // import java.awt.geom.*;
42 // public class Stroke1 extends JFrame {
43 // Stroke drawingStroke = new BasicStroke(5);
45 // new Rectangle2D.Double(20, 40, 100, 40);
47 // public void paint(Graphics g) {
48 // Graphics2D g2d = (Graphics2D)g;
49 // g2d.setStroke(drawingStroke);
53 // public static void main(String args[]) {
54 // JFrame frame = new Stroke1();
55 // frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
56 // frame.setSize(200, 100);
62 public class SpriteRunner
65 private CanvasSprite sprite
;
66 private XAnimation spriteAnimation
;
67 private JavaCanvas canvas
;
68 private double startTime
;
69 private double currentSpeed
;
70 private double currentT
;
71 private boolean animationActive
;
72 private boolean stayAlive
;
74 //----------------------------------------------------------------------------------
76 public SpriteRunner( CanvasSprite _sprite
, XAnimation _animation
, JavaCanvas _canvas
)
78 CanvasUtils
.printLog( "SpriteRunner constructor called!" );
81 spriteAnimation
= _animation
;
86 animationActive
= false;
89 // Set priority to lower-than-normal, as this thread runs the
90 // animation in a busy loop.
91 setPriority( MIN_PRIORITY
);
94 //----------------------------------------------------------------------------------
104 // perform the animation rendering (as fast as possible, for now)
108 while( animationActive
)
110 // to determine the current animation step to render, calc the
111 // elapsed time since this animation was started
112 double elapsedTime
= getCurrentTime() - startTime
;
114 // the frame to render is determined by mapping the cycle
115 // duration (currentSpeed) to the [0,1] range of the animation.
116 currentT
= (elapsedTime
% currentSpeed
) / currentSpeed
;
118 // delegate animation rendering to SpriteCanvas, as
119 // only this instance can enforce consistency (on-time
120 // saving of background etc.)
121 canvas
.renderAnimation( sprite
, spriteAnimation
, currentT
);
123 // TODO: Consolidate _all_ sprites per canvas into one
124 // thread, call updateScreen() only after all sprites
125 // have been processed.
127 //Make changes visible
128 canvas
.updateScreen( false );
130 // TODO: Evaluate vs. setPriority and other means
133 // // give other tasks some time to breathe (10ms - this is only meant symbolically)
135 // } catch( InterruptedException e ) { }
138 // wait until animation is activated again
143 catch( InterruptedException e
) { }
144 catch( IllegalMonitorStateException e
) { }
148 public synchronized void startAnimation( double speed
)
151 currentSpeed
= speed
;
153 // start us, if not done already
157 // enable animation thread
158 animationActive
= true;
163 } catch( IllegalMonitorStateException e
) { }
166 public synchronized void stopAnimation()
168 // stop animation after the current frame has been completely
170 animationActive
= false;
173 public synchronized void resetAnimation()
175 startTime
= getCurrentTime();
178 public synchronized boolean isAnimationActive()
180 return animationActive
;
183 public synchronized void quit()
189 public synchronized double getCurrentT()
194 //----------------------------------------------------------------------------------
197 static double getCurrentTime()
199 // determine current time in seconds
200 java
.util
.Calendar cal
= new java
.util
.GregorianCalendar();
201 return cal
.getTimeInMillis() / 1000.0;