View Javadoc
1   /*
2    * Copyright (c) 2005, The JUNG Authors
3    * All rights reserved.
4    *
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or https://github.com/jrtom/jung/blob/master/LICENSE for a description.
7    *
8    * 
9    */
10  package edu.uci.ics.jung.visualization.util;
11  
12  import edu.uci.ics.jung.algorithms.util.IterativeContext;
13  
14  /**
15   * 
16   * 
17   * @author Tom Nelson - tomnelson@dev.java.net
18   *
19   */
20  public class Animator implements Runnable {
21  	
22  	protected IterativeContext process;
23  	protected boolean stop;
24  	protected Thread thread;
25  	
26  	/**
27  	 * how long the relaxer thread pauses between iteration loops.
28  	 */
29  	protected long sleepTime = 10L;
30  
31  	
32  	public Animator(IterativeContext process) {
33  		this(process, 10L);
34  	}
35  
36  	public Animator(IterativeContext process, long sleepTime) {
37  		this.process = process;
38  		this.sleepTime = sleepTime;
39  	}
40  
41  	/**
42  	 * @return the relaxer thread sleep time
43  	 */
44  	public long getSleepTime() {
45  		return sleepTime;
46  	}
47  
48  	/**
49  	 * @param sleepTime the relaxer thread sleep time to set
50  	 */
51  	public void setSleepTime(long sleepTime) {
52  		this.sleepTime = sleepTime;
53  	}
54  	
55  	public void start() {
56  		// in case its running
57  		stop();
58  		stop = false;
59  		thread = new Thread(this);
60  		thread.setPriority(Thread.MIN_PRIORITY);
61  		thread.start();
62  	}
63  	
64  	public synchronized void stop() {
65  		stop = true;
66  	}
67  
68  	public void run() {
69  		while (!process.done() && !stop) {
70  
71  			process.step();
72  
73  			if (stop)
74  				return;
75  
76  			try {
77  				Thread.sleep(sleepTime);
78  			} catch (InterruptedException ie) {
79  			}
80  		}
81  	}
82  }