/Users/lyon/j4p/src/bookExamples/ch26Graphics/carl/phasor/PhasorJFrame2.java

1    package bookExamples.ch26Graphics.carl.phasor; 
2     
3    /*  Illustrates the sum of two phasors in real-time. 
4     
5       omega1 and amp1 are associated with the green vector and 
6       omega2 and amp2 with the red vector. 
7     The tips of the vectors are black dots for omega1 and bluish for omega2. 
8     Interpret in terms of the text in pages 15 and 16 of Steiglitz. 
9     
10    If you un-comment one of the other two lines in the "paint" method, you will see 1) 
11    the individual steps of the vector addition or 2)the sine-wave 
12    beat, making sure that only one line is uncommented and the other 
13    two are commented out. 
14    
15   */ 
16    
17   import javax.swing.*; 
18   import java.awt.*; 
19   import java.awt.event.*; 
20   class PhasorJFrame2 extends JFrame{ 
21    
22   // Geometry of starting points of vectors 
23       int x1 = 150; 
24       int y1 = 150; 
25       int x2 = 150; 
26       int y2 = 150; 
27    
28       int xcen = 250; 
29       int ycen = 250; 
30       int xOffset = 0; 
31    
32    
33   // Initial parameters of 2 phasors 
34       static double omega1 = 1; 
35       static double omega2 = 1.1; 
36       double amp1 = 60; 
37       double amp2 = 80; 
38       double phi1 = 0;    // phase lag 
39       double phi2 = 0;    // phase lag 
40    
41    
42       double theta = 0; 
43    
44       static Timer ticker; 
45       static TickListener tocker; 
46       double t = 0; 
47       static double delt = .05;  // time increment per display sample 
48    
49       static int milliseconds = 100; //Display timer 
50    
51       boolean firstStep; 
52    
53       PhasorJFrame2(double om1, double om2, double am1, double am2, double ph1, double ph2){ 
54           super(" This is phasor demo" ); 
55           // set up phasor parameters 
56           omega1 = om1; 
57           omega2 = om2; 
58           amp1 = am1; 
59           amp2 = am2; 
60           phi1 = ph1; 
61           phi2 = ph2; 
62    
63           firstStep = true; 
64    
65           Container c = getContentPane(); 
66           tocker = new TickListener(); 
67    
68           setLocation(400,300); 
69           setSize(500,500); 
70           show(); 
71       } 
72    
73    
74       private class TickListener implements ActionListener { 
75           public void actionPerformed(ActionEvent e){ 
76               System.out.print (" Tick "); 
77               upDateGeometry(); 
78               repaint(); 
79           } 
80       } 
81       public void paint(Graphics g){ 
82           //super.paint(g); 
83           drawPhasor(g); 
84           drawSine(g); 
85       } 
86    
87       public void drawPhasor(Graphics g1){ 
88           if ( firstStep ) { 
89               firstStep = false; 
90               return; 
91           } 
92           g1.setColor(Color.green); 
93           int xtip = xcen + x1; 
94           int ytip = ycen + y1; 
95           g1.drawLine(xcen, ycen, xtip, ytip ); 
96           g1.setColor(Color.red); 
97           g1.drawLine( xtip, ytip, xtip + x2, ytip + y2); 
98           g1.setColor(Color.cyan); 
99           g1.fillOval(xtip, ytip, 4, 4); 
100          g1.setColor(Color.black); 
101          g1.fillOval(xtip + x2, ytip + y2, 4, 4); 
102      } 
103   
104      public void drawSine(Graphics g2){ 
105   
106          if ( firstStep ) return; 
107          g2.setColor(Color.black); 
108          xOffset += 2; 
109          g2.drawOval( xOffset, ycen +( y1 + y2), 4, 4); 
110   
111          if ( xOffset > 400) { 
112              super.paint(g2); 
113  //      g2.clearRect(0,0,500,500); 
114              xOffset = 0; 
115          } 
116      } 
117   
118      public void upDateGeometry(){ 
119   
120          t += delt; 
121   
122          double theta1 = t*omega1; 
123          double theta2 = t*omega2; 
124   
125          x1 = (int)(amp1*Math.cos(-theta1-phi1)); 
126          y1 = (int)(amp1*Math.sin(-theta1-phi1)); 
127   
128          x2 = (int)(amp2*Math.cos(-theta2-phi2)); 
129          y2 = (int)(amp2*Math.sin(-theta2-phi2)); 
130   
131      } 
132   
133      static void setSampleTime( double st ){ 
134          // sample time expressed as number of samples per omega wave 
135          delt = 2*Math.PI/(st*omega1); 
136      } 
137   
138      static void setDisplayTick( int tick ){ 
139          milliseconds = tick; 
140          ticker =  new Timer(milliseconds, tocker); 
141      } 
142   
143   
144      public static void main (String args[]){ 
145          // omega1, omega2, amp1, amp2, phi1, phi2 
146          PhasorJFrame2 pjf = new PhasorJFrame2( -1., -1.1, 60., 60.,0.,0  ); 
147   
148          pjf.setSampleTime(77.77); 
149   
150          // slow down for closer look (argument is in milliseconds) 
151          pjf.setDisplayTick( 50 ); 
152          ticker.start(); 
153   
154   
155          // Orderly window closing: 
156          pjf.addWindowListener(new WindowAdapter() { 
157              public void windowClosing(WindowEvent e) { 
158                  System.exit(0); 
159              } 
160        }); 
161      } 
162   
163  }