KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > demos > TraceTester


1 /*
2  * TraceTester, an incredible old but remarkable weird visual test for
3  * the automatic scaling routines of jchart2d.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  */

22
23 package info.monitorenter.gui.chart.demos;
24
25 import info.monitorenter.gui.chart.Chart2D;
26 import info.monitorenter.gui.chart.ITrace2D;
27 import info.monitorenter.gui.chart.TracePoint2D;
28 import info.monitorenter.gui.chart.traces.Trace2DBijective;
29 import info.monitorenter.gui.chart.traces.Trace2DLtd;
30 import info.monitorenter.gui.chart.traces.Trace2DLtdReplacing;
31 import info.monitorenter.gui.chart.traces.Trace2DLtdSorted;
32 import info.monitorenter.gui.chart.traces.Trace2DReplacing;
33 import info.monitorenter.gui.chart.traces.Trace2DSimple;
34 import info.monitorenter.gui.chart.traces.Trace2DSorted;
35
36 import java.awt.event.WindowAdapter JavaDoc;
37 import java.awt.event.WindowEvent JavaDoc;
38
39 import javax.swing.JFrame JavaDoc;
40
41
42 /**
43  * A testclass that steps through all <code>ITrace2D</code> implementations
44  * and adds random or "half- random" <code>TracePoint2D</code> -instances.
45  * <p>
46  *
47  * You may see, that not all <code>ITrace2D</code> implementations work as
48  * proposed (Trace2DLimitedReplacing). This will be fixed.
49  * <p>
50  *
51  * @author Achim Westermann <a
52  * HREF='mailto:Achim.Westermann@gmx.de'>Achim.Westermann@gmx.de </a>
53  *
54  * @version $Revision: 1.1 $
55  */

56 public final class TraceTester {
57
58   /**
59    * Generates "half random" points.
60    * <p>
61    * Unlike random points half random points are not totally random samples
62    * within the given bounds but repeat the same x value 10 times.
63    * <p>
64    *
65    * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
66    *
67    *
68    * @version $Revision: 1.1 $
69    */

70   static class HalfRandomPoints extends TraceTester.RandomPoints {
71
72     /** The old x value. */
73     private double m_oldx = 0;
74     /**
75      * Defines how often the same x value is returned from subsequent calls to
76      * {@link #nextPoint(ITrace2D)}.
77      */

78     private double m_samexcount = 0;
79
80     /**
81      * Creates a half random point generator that will create random points
82      * within ([minx..minx+maxx], [miny..miny+maxy]).
83      * <p>
84      *
85      * @param minx
86      * the lower x bound for points to generate.
87      *
88      * @param maxx
89      * the x range for points to generate.
90      *
91      * @param miny
92      * the lower y bound for points to generate.
93      *
94      * @param maxy
95      * the y range for points to generate.
96      *
97      */

98     HalfRandomPoints(final int minx, final int maxx, final int miny, final int maxy) {
99       super(minx, maxx, miny, maxy);
100       this.m_oldx = (maxx - minx) / 2;
101     }
102
103     /**
104      * @see info.monitorenter.gui.chart.demo.TraceTester.RandomPoints#nextPoint()
105      */

106     public TracePoint2D nextPoint() {
107       if (this.m_samexcount == 10) {
108         this.m_samexcount = 0;
109         this.m_oldx = this.m_rand.nextDouble() * this.m_xrange + this.m_xmin;
110       }
111       this.m_samexcount++;
112       return new TracePoint2D(this.m_oldx, this.m_rand.nextDouble() * this.m_yrange + this.m_ymin);
113     }
114   }
115
116   /**
117    * Helper that creates random points.
118    * <p>
119    *
120    * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
121    *
122    * @version $Revision: 1.1 $
123    */

124   private static class RandomPoints {
125     /** Used for randomization. */
126     protected java.util.Random JavaDoc m_rand = new java.util.Random JavaDoc();
127
128     /** Minimum bound for x. */
129     protected double m_xmin;
130
131     /** Range for x. */
132     protected double m_xrange;
133
134     /** Minimum bound for y. */
135     protected double m_ymin;
136
137     /** Range for y. */
138     protected double m_yrange;
139
140     /**
141      * Creates a random point generator that will create random points within
142      * ([minx..minx+maxx], [miny..miny+maxy]).
143      * <p>
144      *
145      * @param minx
146      * the lower x bound for points to generate.
147      *
148      * @param maxx
149      * the x range for points to generate.
150      *
151      * @param miny
152      * the lower y bound for points to generate.
153      *
154      * @param maxy
155      * the y range for points to generate.
156      *
157      */

158     RandomPoints(final int minx, final int maxx, final int miny, final int maxy) {
159       if (minx >= maxx) {
160         throw new IllegalArgumentException JavaDoc("minx>=maxx!");
161       }
162       if (miny >= maxy) {
163         throw new IllegalArgumentException JavaDoc("miny>=maxy!");
164       }
165       this.m_xmin = minx;
166       this.m_xrange = maxx - minx;
167       this.m_ymin = miny;
168       this.m_yrange = maxy - miny;
169     }
170
171     /**
172      * Returns the next random point.
173      * <p>
174      *
175      * @return the next random point.
176      */

177     public TracePoint2D nextPoint() {
178       return new TracePoint2D(this.m_rand.nextDouble() * this.m_xrange + this.m_xmin, this.m_rand
179           .nextDouble()
180           * this.m_yrange + this.m_ymin);
181     }
182
183   }
184
185   /**
186    * Main entry.
187    * <p>
188    *
189    * @param args
190    * ignored.
191    */

192   public static void main(final String JavaDoc[] args) {
193     try {
194       Class JavaDoc[] traces = new Class JavaDoc[] {Trace2DSimple.class, Trace2DBijective.class,
195           Trace2DReplacing.class, Trace2DSorted.class, Trace2DLtd.class, Trace2DLtdReplacing.class,
196           Trace2DLtdSorted.class };
197       RandomPoints rand = new RandomPoints(0, 3, 0, 3);
198
199       Chart2D test = new Chart2D();
200       JFrame JavaDoc frame = new JFrame JavaDoc("TraceTester");
201       frame.addWindowListener(new WindowAdapter JavaDoc() {
202         public void windowClosing(final WindowEvent JavaDoc e) {
203           System.exit(0);
204         }
205       });
206
207       frame.getContentPane().add(test);
208       frame.setSize(600, 500);
209       frame.setLocation(200, 200);
210       frame.setVisible(true);
211       ITrace2D current = null;
212       for (int i = 0; i < traces.length; i++) {
213         current = (ITrace2D) traces[i].newInstance();
214         test.addTrace(current);
215         frame.setTitle("TraceTester: full-random, current: " + traces[i].getName());
216
217         for (int j = 0; j < 200; j++) {
218           current.addPoint(rand.nextPoint());
219           Thread.sleep(50);
220         }
221         Thread.sleep(2000);
222         test.removeTrace(current);
223       }
224       rand = new HalfRandomPoints(0, 3, 0, 3);
225       for (int i = 0; i < traces.length; i++) {
226         current = (ITrace2D) traces[i].newInstance();
227         test.addTrace(current);
228         frame.setTitle("TraceTester: repeating x 10 times, current: " + traces[i].getName());
229
230         for (int j = 0; j < 200; j++) {
231           current.addPoint(rand.nextPoint());
232           Thread.sleep(50);
233         }
234         Thread.sleep(2000);
235         test.removeTrace(current);
236       }
237
238       System.exit(0);
239
240     } catch (Throwable JavaDoc f) {
241       f.printStackTrace();
242       System.exit(0);
243     }
244   }
245
246   /**
247    * Defcon.
248    * <p>
249    */

250   private TraceTester() {
251     // nop
252
}
253
254 }
255
Popular Tags