KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RunningChart, a test for the Chart2D.
3  * Copyright (C) 2002 Achim Westermann, Achim.Westermann@gmx.de
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.layout.ChartPanel;
28 import info.monitorenter.gui.chart.rangepolicies.RangePolicyMinimumViewport;
29 import info.monitorenter.gui.chart.traces.Trace2DLtd;
30 import info.monitorenter.reflection.ObjRecorder2Trace2DAdapter;
31 import info.monitorenter.util.Range;
32
33 import java.awt.BorderLayout JavaDoc;
34 import java.awt.Color JavaDoc;
35 import java.awt.Container JavaDoc;
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 test for the <code>Chart2D</code> that constantly adds new tracepoints to
44  * a <code> Trace2DLtd</code>. Mainly the runtime- scaling is interesting.
45  * <p>
46  * Furthermore this is an example on how to connect other components to the
47  * <code>Chart2D</code> using an adaptor- class. If interested have a look
48  * on {@link info.monitorenter.reflection.ObjRecorder2Trace2DAdapter}.
49  * <p>
50  *
51  * @author <a HREF='mailto:Achim.Westermann@gmx.de'> Achim Westermann </a>
52  * @version $Revision: 1.2 $
53  */

54 public class RunningChart extends JFrame JavaDoc {
55   /**
56    * Helper class that holds an internal number that is randomly modified by a
57    * Thread.
58    * <p>
59    *
60    * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
61    * @version $Revision: 1.2 $
62    */

63   static class RandomBumper extends Thread JavaDoc {
64     /** Streches or compresses the grade of jumping of the internal number. */
65     protected double m_factor;
66
67     /** The bumping number. */
68     protected double m_number = 0;
69
70     /** The propability of an increase versus a decrease of the bumped number. */
71     protected double m_plusminus = 0.5;
72
73     /** Needed for randomization of bumping the number. */
74     protected java.util.Random JavaDoc m_randomizer = new java.util.Random JavaDoc();
75
76     /**
77      * Creates an instance.
78      * <p>
79      *
80      * @param plusminus
81      * probability to increase or decrease the number each step.
82      * @param factor
83      * affects the amplitude of the number (severity of jumps).
84      */

85     public RandomBumper(final double plusminus, final int factor) {
86
87       if (plusminus < 0 || plusminus > 1) {
88         System.out.println(this.getClass().getName()
89             + " ignores constructor-passed value. Must be between 0.0 and 1.0!");
90       } else {
91         this.m_plusminus = plusminus;
92       }
93       this.m_factor = factor;
94       this.start();
95     }
96
97     /**
98      * @see java.lang.Runnable#run()
99      */

100     public void run() {
101
102       while (true) {
103         double rand = this.m_randomizer.nextDouble();
104         if (rand < this.m_plusminus) {
105           this.m_number += this.m_randomizer.nextDouble() * this.m_factor;
106         } else {
107           this.m_number -= this.m_randomizer.nextDouble() * this.m_factor;
108         }
109
110         try {
111           sleep(40);
112         } catch (InterruptedException JavaDoc e) {
113           // nop
114
}
115
116       }
117     }
118   }
119
120   /**
121    * Generated for <code>serialVersionUID</code>.
122    */

123   private static final long serialVersionUID = 3545231432038627123L;
124
125   /**
126    * Main entry.
127    * <p>
128    *
129    * @param args
130    * ignored.
131    */

132   public static void main(final String JavaDoc[] args) {
133
134     Chart2D chart = new Chart2D();
135     ITrace2D data = new Trace2DLtd(300);
136     data.setColor(Color.RED);
137     data.setName("random");
138     data.setPhysicalUnits("hertz", "ms");
139     chart.addTrace(data);
140     RunningChart wnd = new RunningChart(chart, "RunningChart");
141     chart.getAxisX().setPaintGrid(true);
142     chart.getAxisY().setPaintGrid(true);
143
144     chart.getAxisX().setPaintScale(true);
145     chart.getAxisX().setPaintScale(true);
146
147     // force ranges:
148
chart.getAxisY().setRangePolicy(new RangePolicyMinimumViewport(new Range(-1e4, +1e4)));
149     // chart.setFont(new Font(null,0,12));
150
wnd.setLocation(200, 300);
151     wnd.setSize(700, 210);
152     wnd.setResizable(true);
153     wnd.setVisible(true);
154     new ObjRecorder2Trace2DAdapter(data, new RandomBumper(0.5, 1000), "m_number", 100);
155   }
156
157   /** The chart to use. */
158   protected Chart2D m_chart = null;
159
160   /**
161    * Creates an instance that will dynamically paint on the chart to a trace
162    * with the given label.
163    * <p>
164    *
165    * @param chart
166    * the chart to use.
167    * @param label
168    * the name of the trace too display.
169    */

170   public RunningChart(final Chart2D chart, final String JavaDoc label) {
171
172     super(label);
173     this.m_chart = chart;
174     addWindowListener(new WindowAdapter JavaDoc() {
175
176       public void windowClosing(final WindowEvent JavaDoc e) {
177
178         System.exit(0);
179       }
180     });
181     Container JavaDoc contentPane = getContentPane();
182     contentPane.setLayout(new BorderLayout JavaDoc());
183     contentPane.add(new ChartPanel(this.m_chart), BorderLayout.CENTER);
184   }
185 }
186
Popular Tags