/Users/lyon/j4p/src/video/SoundDigitizerPanel.java

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