/Users/lyon/j4p/src/sound/DualOscopePanel.java

1    package sound; 
2     
3    /** 
4     * Created by 
5     * User: lyon 
6     * Date: Nov 29, 2003 
7     * Time: 11:11:38 AM 
8     * 
9     */ 
10    
11   import gui.run.RunScroll; 
12    
13   import javax.swing.JPanel; 
14   import java.awt.BorderLayout; 
15   import java.awt.Color; 
16   import java.awt.Component; 
17   import java.awt.Container; 
18   import java.awt.Dimension; 
19   import java.awt.Graphics; 
20   import java.awt.Graphics2D; 
21   import java.awt.GridLayout; 
22   import java.awt.Rectangle; 
23   import java.awt.Scrollbar; 
24   import java.awt.geom.Line2D; 
25    
26   public class DualOscopePanel 
27           extends JPanel { 
28       private RunScroll topSb; 
29       private RunScroll bottomSb; 
30       private RunScroll leftSb; 
31       private RunScroll rightSb; 
32       private RunScroll bottomSb2; 
33       private RunScroll leftSb2; 
34       private RunScroll rightSb2; 
35       private double[] trace1; 
36       private double[] trace2; 
37       private int dx = 0; 
38       private int dx2 = 0; 
39       private int oldDx = 0; 
40       private int oldDx2 = 0; 
41       private int dy = 0; 
42       private int dy2 = 0; 
43       private int oldDy = 0; 
44       private int oldDy2 = 0; 
45       private final double 
46               xScaleFactors[] = {50000, 25000, 10000, 5000, 2500, 1000, 
47                                  500, 250, 100, 50, 25, 10, 
48                                  5, 2.5, 1, .5, .25, .1, 
49                                  .05, .025, .01, .005, .0025, .001, 
50                                  .0005}; 
51       private final String xSFLabels[] = {"0.05 u", "0.1 u", "0.25 u", 
52                                           "0.5 u", "1 u", "2.5 u", 
53                                           "5 u", "10 u", "25 u", 
54                                           "50 u", "100 u", "250 u", 
55                                           "500 u", "1 m", "2.5 m", 
56                                           "5 m", "10 m", "25 m", 
57                                           "50 m", "100 m", "250 m", 
58                                           "500 m", "1 ", "2.5 ", 
59                                           "5 "}; 
60       private final int xsfStartIndex = 14; 
61       private double xScaleFactor = xScaleFactors[xsfStartIndex]; 
62       private double oldXScaleFactor = xScaleFactors[xsfStartIndex]; 
63       private String xSFLabel = new String(xSFLabels[xsfStartIndex]); 
64       private boolean labelsVisible = true; 
65       private String title = null; 
66    
67       public void setLabelsVisible(boolean b) { 
68           labelsVisible = b; 
69       } 
70    
71       public static final double 
72               yScaleFactors[] = { 
73                   50000, 
74                   25000, 
75                   10000, 
76                   5000, 
77                   2000, 
78                   1000, 
79                   500, 
80                   200, 
81                   100, 
82                   50, 
83                   20, 
84                   10, 
85                   5, 2, 1, .5, .2, .1}; 
86       public static final String 
87               ySFLabels[] = { 
88                   "1 m", "2.5 m", 
89                   "5 m", "10 m", 
90                   "25 m", "50 m", 
91                   "100 m", "250 m", 
92                   "500 m", 
93                   "1 ", "2.5 ", 
94                   "5 ", "10 ", 
95                   "20 ", "50 ", 
96                   "100 ", "250 ", "500 "}; 
97       private final int yScaleFactorStartIndex = 8; 
98       private double yScaleFactor = 1; 
99       private double yScaleFactor2 = 1; 
100      private double oldYScaleFactor = 1; 
101      private double oldYScaleFactor2 = 1; 
102      private String ySFLabel = new String(ySFLabels[yScaleFactorStartIndex]); 
103      private String ySFLabel2 = new String(ySFLabels[yScaleFactorStartIndex]); 
104      private TracePanel tracePanel = new TracePanel(); 
105   
106      /** 
107       * This constructor uses a 440 Hz sine wave 
108       * as the default waveform. 
109       */ 
110      public DualOscopePanel() { 
111          this(new Oscillator(440, 1000).getSineWave(), 
112                  "440 HZ sineWave"); 
113      } 
114   
115      public Dimension getPreferredSize() { 
116          return new Dimension(400, 200); 
117      } 
118   
119      /** 
120       * A read only variable 
121       * 
122       * @return the title as set by the constructor 
123       */ 
124      public String getTitle() { 
125          return title; 
126      } 
127   
128      /** 
129       * This constructor uses the <code>doubleData</code> 
130       * as the source for the default waveform. 
131       * 
132       * @param doubleData an array of data to be displayed 
133       * @param title      title of the panel. 
134       */ 
135      //public OscopePanel(double doubleData[], String title) { 
136      public DualOscopePanel(double doubleData[], String title) { 
137          this.trace1 = doubleData; 
138          this.trace2 = new Oscillator(220, 1000).getSineWave(); 
139          this.title = title; 
140          setLayout(new BorderLayout()); 
141          topSb = 
142                  new RunScroll(Scrollbar.HORIZONTAL) { 
143                      public void run() { 
144                          int i = getValue(); 
145                          xScaleFactor = xScaleFactors[i]; 
146                          if (xScaleFactor != oldXScaleFactor) { 
147                              xSFLabel = xSFLabels[i]; 
148                              tracePanel.repaint(500); 
149                              oldXScaleFactor = xScaleFactor; 
150                          } 
151                      } 
152                  }; 
153          // Set top scrollbar characteristics 
154          topSb.setValues(xsfStartIndex, 0, 0, 24); 
155          //topSb2.setValues(xsfStartIndex, 0, 0, 24); 
156          bottomSb2 = 
157                  new RunScroll(Scrollbar.HORIZONTAL) { 
158                      public void run() { 
159                          dx2 = getValue(); 
160                          if (dx2 != oldDx2) { 
161                              tracePanel.repaint(500); 
162                              oldDx2 = dx2; 
163                          } 
164                      } 
165                  }; 
166          bottomSb = 
167                  new RunScroll(Scrollbar.HORIZONTAL) { 
168                      public void run() { 
169                          dx = getValue(); 
170                          if (dx != oldDx) { 
171                              tracePanel.repaint(500); 
172                              oldDx = dx; 
173                          } 
174                      } 
175                  }; 
176          leftSb2 = 
177                  new RunScroll(Scrollbar.VERTICAL) { 
178                      public void run() { 
179                          dy2 = getValue(); 
180                          if (dy2 != oldDy2) { 
181                              tracePanel.repaint(500); 
182                              oldDy2 = dy2; 
183                          } 
184                      } 
185                  }; 
186          leftSb = 
187                  new RunScroll(Scrollbar.VERTICAL) { 
188                      public void run() { 
189                          dy = getValue(); 
190                          if (dy != oldDy) { 
191                              tracePanel.repaint(500); 
192                              oldDy = dy; 
193                          } 
194                      } 
195                  }; 
196          rightSb2 = 
197                  new RunScroll(Scrollbar.VERTICAL) { 
198                      public void run() { 
199                          int i = getValue(); 
200                          yScaleFactor2 = yScaleFactors[i]; 
201                          if (yScaleFactor2 != oldYScaleFactor2) { 
202                              ySFLabel2 = ySFLabels[i]; 
203                              tracePanel.repaint(500); 
204                              oldYScaleFactor2 = yScaleFactor2; 
205                          } 
206                      } 
207                  }; 
208          rightSb = 
209                  new RunScroll(Scrollbar.VERTICAL) { 
210                      public void run() { 
211                          int i = getValue(); 
212                          yScaleFactor = yScaleFactors[i]; 
213                          if (yScaleFactor != oldYScaleFactor) { 
214                              ySFLabel = ySFLabels[i]; 
215                              tracePanel.repaint(500); 
216                              oldYScaleFactor = yScaleFactor; 
217                          } 
218                      } 
219                  }; 
220          // Set right scrollbar characteristics 
221          rightSb.setValues(yScaleFactorStartIndex, 0, 0, 
222                  yScaleFactors.length - 1); 
223          rightSb2.setValues(yScaleFactorStartIndex, 0, 0, 
224                  yScaleFactors.length - 1); 
225   
226   
227   
228          // Set left scrollbar characteristics 
229          leftSb.setValues(0, 0, -300, 300); 
230          leftSb2.setValues(0, 0, -300, 300); 
231          add(topSb, BorderLayout.NORTH); 
232          add(bottomSb, BorderLayout.SOUTH); 
233          add(leftSb, BorderLayout.WEST); 
234          add(rightSb, BorderLayout.EAST); 
235          add(tracePanel, BorderLayout.CENTER); 
236          setBackground(Color.white); 
237   
238          // Set bottom scrollbar characteristics based on sample length 
239          bottomSb2.setValues(0, 0, 0, this.trace2.length); 
240          bottomSb.setValues(0, 0, 0, this.trace1.length); 
241      } 
242   
243      public void setTrace1(double d[]) { 
244          trace1 = d; 
245      } 
246   
247      public RunScroll getTopSb() { 
248          return topSb; 
249      } 
250   
251      public RunScroll getBottomSb() { 
252          return bottomSb; 
253      } 
254   
255      public RunScroll getBottomSb2() { 
256          return bottomSb2; 
257      } 
258   
259      public RunScroll getLeftSb() { 
260          return leftSb; 
261      } 
262   
263      public RunScroll getLeftSb2() { 
264          return leftSb2; 
265      } 
266   
267      public RunScroll getRightSb() { 
268          return rightSb; 
269      } 
270   
271      public RunScroll getRightSb2() { 
272          return rightSb2; 
273      } 
274   
275      public Component getTracePanel() { 
276          return tracePanel; 
277      } 
278   
279      public void setTopSb(RunScroll topSb) { 
280          this.topSb = topSb; 
281      } 
282   
283      class TracePanel extends JPanel { 
284          private boolean drawGrid = true; 
285          private Color gridColor = new Color(0, 255, 0); 
286          private Dimension dim; 
287          private int height, width; 
288   
289          public void setGridColor(Color c) { 
290              gridColor = c; 
291          } 
292   
293          public void grid(int grid_spacing, Graphics g, Color c) { 
294              Rectangle r = g.getClipBounds(); 
295              int w = r.width; 
296              int h = r.height; 
297              Color oldColor = g.getColor(); 
298              g.setColor(c); 
299              for (int x = 0; x < w; x = x + grid_spacing) { 
300                  g.drawLine(x, 0, x, h); 
301              } 
302              for (int y = 0; y < h; y = y + grid_spacing) { 
303                  g.drawLine(0, y, w, y); 
304              } 
305              g.setColor(oldColor); 
306          } 
307   
308          public void paint(Graphics g) { 
309              drawData(g); 
310              if (!labelsVisible) return; 
311              drawXScaleLabel(g); 
312              drawYScaleLabel2(g); 
313              drawYScaleLabel(g); 
314          } 
315   
316          private void drawData(Graphics gOld) { 
317              Graphics2D g = (Graphics2D) gOld; 
318              double limit = (trace1.length * xScaleFactor); 
319              double limit2 = (trace2.length * xScaleFactor); 
320              dim = getSize(); 
321              height = dim.height; 
322              width = dim.width; 
323              g.clearRect(0, 0, width, height); 
324              if (drawGrid) 
325                  grid(20, g, gridColor); 
326              if (limit2 > width) limit2 = (double) width; 
327              int k = dx2; 
328              drawTrace2(limit2, k, g,trace2); 
329              if (limit > width) limit = (double) width; 
330              int j = dx; 
331              drawTrace1(limit, j, g); 
332          } 
333   
334          private void drawTrace1(double limit, int j, Graphics2D g) { 
335              done: 
336              for (double i = 0; i < limit; i += xScaleFactor) { 
337                  if (j >= (trace1.length - 1)) 
338                      break done; 
339                  double x1 = i; 
340                  double y1 = (height - ( 
341                          (trace1[j] * yScaleFactor) 
342                          * height / 4 + height / 2)) - dy; 
343                  double x2 = (i + xScaleFactor); 
344                  double y2 = (height - ( 
345                          (trace1[j + 1] * yScaleFactor) 
346                          * height / 4 + height / 2)) - dy; 
347                  g.draw(new Line2D.Double(x1, y1, x2, y2)); 
348                  g.setColor(Color.red); 
349                  j++; 
350              } 
351          } 
352   
353          private void drawTrace2(double limit2, int k, Graphics2D g, double d[]) { 
354              done: 
355              for (double i = 0; i < limit2; i += xScaleFactor) { 
356                  if (k >= (trace2.length - 1)) 
357                      break done; 
358                  double x11 = i; 
359                  double y11 = height - d[k] * yScaleFactor2 * height / 4 + height / 2 - dy2; 
360                  double x22 = (i + xScaleFactor); 
361                  double y22 = (height - ( 
362                          (d[k + 1] * yScaleFactor2) 
363                          * height / 4 + height / 2)) - dy2; 
364                  g.draw(new Line2D.Double(x11, y11, x22, y22)); 
365                  k++; 
366              } 
367          } 
368   
369          private void drawXScaleLabel(Graphics g) { 
370              g.setColor(Color.black); 
371              int height = 10; 
372              int xloc = 20; 
373              int yloc = 20; 
374              String xsfLabel = xSFLabel + "sec/div"; 
375              int string_width = getFontMetrics(g.getFont()).stringWidth(xsfLabel); 
376              int string_height = getFontMetrics(g.getFont()).getHeight(); 
377              g.clearRect(xloc, yloc, string_width, string_height); 
378              g.drawString(xsfLabel, xloc, height + yloc); 
379          } 
380   
381          private void drawYScaleLabel2(Graphics g) { 
382              int height = 10; 
383              int xloc = 30; 
384              int yloc = dim.height - 80; 
385              String ysfLabel2 = ySFLabel2 + "v/div"; 
386              int string_width = getFontMetrics(g.getFont()).stringWidth(ysfLabel2); 
387              int string_height = getFontMetrics(g.getFont()).getHeight(); 
388              g.clearRect(xloc, yloc, string_width, string_height); 
389              g.drawString(ysfLabel2, xloc, height + yloc); 
390          } 
391   
392          private void drawYScaleLabel(Graphics g) { 
393              g.setColor(Color.red); 
394              int height = 10; 
395              int xloc = 20; 
396              int yloc = dim.height - 60; 
397              String ysfLabel = ySFLabel + "v/div"; 
398              int string_width = getFontMetrics(g.getFont()).stringWidth(ysfLabel); 
399              int string_height = getFontMetrics(g.getFont()).getHeight(); 
400              g.clearRect(xloc, yloc, string_width, string_height); 
401              g.drawString(ysfLabel, xloc, height + yloc); 
402          } 
403      } 
404   
405      public static void main(String args[]) { 
406          OscopePanel osp = new OscopePanel(); 
407          gui.ClosableJFrame cf = new gui.ClosableJFrame(); 
408          Container c = cf.getContentPane(); 
409          c.add(osp); 
410          c.setLayout(new GridLayout(1, 0)); 
411          cf.setSize(400, 400); 
412          cf.show(); 
413      } 
414  } 
415   
416