Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / core / util / Crono.java @ 10626

History | View | Annotate | Download (4.95 KB)

1
/*
2
 * Created on 20-feb-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: Crono.java 10626 2007-03-06 16:55:54Z caballero $
47
 * $Log$
48
 * Revision 1.2  2007-03-06 16:47:58  caballero
49
 * Exceptions
50
 *
51
 * Revision 1.1  2006/05/24 21:12:36  azabala
52
 * primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
53
 *
54
 * Revision 1.1  2006/02/20 19:43:27  azabala
55
 * *** empty log message ***
56
 *
57
 *
58
 */
59
package com.iver.cit.gvsig.geoprocess.core.util;
60

    
61
import java.awt.BasicStroke;
62
import java.awt.Color;
63
import java.awt.Dimension;
64
import java.awt.Graphics;
65
import java.awt.Graphics2D;
66
import java.awt.event.ActionEvent;
67
import java.awt.event.ActionListener;
68
import java.awt.geom.Line2D;
69

    
70
import javax.swing.JComponent;
71
import javax.swing.Timer;
72

    
73
/**
74
 * Component to see time (used to monitoring long processes).
75
 * 
76
 * @author azabala
77
 * 
78
 */
79
public class Crono extends JComponent implements ActionListener {
80
        /**
81
         * 
82
         */
83
        private static final long serialVersionUID = -7534000624022602134L;
84

    
85
        private final int ox = 4;
86

    
87
        private final int oy = 4;
88

    
89
        private int tam;
90

    
91
        private long start;
92

    
93
        private long end;
94

    
95
        private boolean counting;
96

    
97
        private Timer timer;
98

    
99
        /**
100
         * @roseuid 3CA83ED3006E
101
         */
102
        public Crono() {
103
                this(50);
104
        }
105

    
106
        /**
107
         * @roseuid 3CA83ED3006F
108
         */
109
        public Crono(int tam) {
110
                this.tam = tam;
111
                start = end = System.currentTimeMillis();
112
                timer = new Timer(1000, this);
113
                setPreferredSize(new Dimension(tam + 8, tam + 30));
114
                counting = false;
115
        }
116

    
117
        /**
118
         * @roseuid 3CA83ED3007E
119
         */
120
        public void actionPerformed(ActionEvent e) {
121
                end = System.currentTimeMillis();
122
                repaint();
123
        }
124

    
125
        /**
126
         * @roseuid 3CA83ED30080
127
         */
128
        private float posx(int radio, int grados) {
129
                return (float) (radio * Math.cos(Math.toRadians(grados - 90)));
130
        }
131

    
132
        /**
133
         * @roseuid 3CA83ED3008D
134
         */
135
        private float posy(int radio, int grados) {
136
                return (float) (radio * Math.sin(Math.toRadians(grados - 90)));
137
        }
138

    
139
        /**
140
         * @roseuid 3CA83ED30090
141
         */
142
        public void paintComponent(Graphics gr) {
143
                this.paintBorder(gr);
144
                Graphics2D g = (Graphics2D) gr;
145
                if (counting)
146
                        g.setColor(Color.red);
147
                else
148
                        g.setColor(Color.green);
149
                g.setStroke(new BasicStroke(3));
150
                g.fillArc(ox, oy, tam, tam, 0, 360);
151
                g.setColor(getForeground());
152
                g.drawArc(ox, oy, tam, tam, 0, 360);
153
                int radio = tam / 2;
154
                int cx = radio + ox;
155
                int cy = radio + oy;
156
                for (int i = 0; i < 360; i += 30) {
157
                        float x0 = cx + posx(radio, i);
158
                        float y0 = cy + posy(radio, i);
159
                        float x1 = cx + posx(radio - 5, i);
160
                        float y1 = cy + posy(radio - 5, i);
161
                        g.draw(new Line2D.Double(x0, y0, x1, y1));
162
                }
163

    
164
                long v = end - start;
165
                long m = v / 1000;
166
                long mili = v - m * 1000;
167
                long s = m / 60;
168
                long secs = m - s * 60;
169
                long mi = s / 60;
170
                long mins = s - mi * 60;
171

    
172
                float mx = cx + posx(radio - 10, (int) secs * 6);
173
                float my = cy + posy(radio - 10, (int) secs * 6);
174
                g.draw(new Line2D.Float(cx, cy, mx, my));
175
                mx = cx + posx(radio - 20, (int) mins * 6);
176
                my = cy + posy(radio - 20, (int) mins * 6);
177
                g.draw(new Line2D.Float(cx, cy, mx, my));
178

    
179
                String str = mins + " mins " + secs + " secs";
180
                g.drawString(str, 0, tam + 20);
181
        }
182

    
183
        /**
184
         * Para el reloj y vuelve a la situacion inicial.
185
         * 
186
         * @roseuid 3CA83ED3009C
187
         */
188
        public void reset() {
189
                timer.stop();
190
                counting = false;
191
                start = end = System.currentTimeMillis();
192
                repaint();
193
        }
194

    
195
        /**
196
         * Comienza a contar.
197
         * 
198
         * @roseuid 3CA83ED3009D
199
         */
200
        public void start() {
201
                timer.start();
202
                counting = true;
203
                start = System.currentTimeMillis();
204
        }
205

    
206
        /**
207
         * Para de contar
208
         * 
209
         * @roseuid 3CA83ED3009E
210
         */
211
        public void stop() {
212
                end = System.currentTimeMillis();
213
                timer.stop();
214
                counting = false;
215
                repaint();
216
        }
217

    
218
        /**
219
         * retorna el tiempo transcurrido en mili-segundos.
220
         * 
221
         * @roseuid 3CA83ED3009F
222
         */
223
        public long getElapsed() {
224
                if (counting)
225
                        return System.currentTimeMillis() - start;
226
                else
227
                        return (end - start);
228
        }
229
}