/Users/lyon/j4p/src/bookExamples/ch26Graphics/carl/phasor/PhasorJFrame.java
|
1 package bookExamples.ch26Graphics.carl.phasor;
2
3 /* Hi All! As you can see, this text which you are reading is a comment in a java program, so you can take this whole e-mail message and paste it into a text file in a CodeWarrior project, and it will run if you name it "PhasorJFrame.java" and set the target of "Java Application Release Settings" to "PhasorJFrame". RUN THIS PROGRAM!!!! It illustrates the sum of two phasors in real-time. Omega1 and amp1 are associated with the green vector and omega2 and amp2 with the red vector. The tips of the vectors are black dots for omega1 and bluish for omega2. Change the values of omega1 and omega2 (to values such as 1 and -1, or 2 and 4, or 10 and 20) and observe what happens. Interpret these in terms of the text in pages 15 and 16 of Steiglitz. If you un-comment one of the other two lines in the "paint" method, you will see 1)the individual steps of the vector addition or 2)the sine-wave beat, making sure that only one line is uncommented and the other two are commented out.
4
5 Anyone who wants extra credit can create JButtons and JTextfields to modify the values of omega's and amp's (amplitudes), and branch the lines in the paint method.
6
7 */
8
9
10
11 import javax.swing.*;
12 import java.awt.*;
13 import java.awt.event.*;
14 class PhasorJFrame extends JFrame{
15
16 int x1, y1, x2, y2;
17 double omega1 = 1;
18 double omega2 = 1.1;
19
20 double amp1 = 60;
21 double amp2 = 80;
22
23 double theta = 0;
24 int xcen = 250;
25 int ycen = 250;
26
27 int xOffset = 0;
28
29 double t = 0;
30 double delt = .05;
31
32 boolean firstStep;
33
34 PhasorJFrame(){
35 super(" This is phasor demo" );
36 firstStep = true;
37
38 Container c = getContentPane();
39
40 x1 = 150;
41 y1 = 150;
42
43 x2 = 150;
44 y2 = 150;
45
46 xcen = 250;
47 ycen = 250;
48
49 TickListener tocker = new TickListener();
50 setLocation(400,300);
51 setSize(500,500);
52 show();
53
54 Timer ticker = new Timer(100, tocker);
55 ticker.start();
56 }
57
58 private class TickListener implements ActionListener {
59 public void actionPerformed(ActionEvent e){
60 //System.out.print (" Tick ");
61 upDateGeometry();
62 repaint();
63 }
64 }
65
66 public void paint(Graphics g){
67 // super.paintComponents(g);
68 drawPhasor(g);
69 // drawCosine(g);
70 }
71
72 public void drawPhasor(Graphics g1){
73 if ( firstStep ) {
74 firstStep = false;
75 return;
76 }
77 g1.setColor(Color.green);
78 int xtip = xcen + x1;
79 int ytip = ycen + y1;
80 g1.drawLine(xcen, ycen, xtip, ytip );
81 g1.setColor(Color.red);
82 g1.drawLine( xtip, ytip, xtip + x2, ytip + y2);
83 g1.setColor(Color.black);
84 g1.fillOval(xtip, ytip, 4, 4);
85 g1.setColor(Color.cyan);
86 g1.fillOval(xtip + x2, ytip + y2, 4, 4);
87 }
88
89 public void drawCosine(Graphics g2){
90
91 if ( firstStep ) return;
92 g2.setColor(Color.red);
93 xOffset += 2;
94 g2.drawOval( xOffset, ycen -( x1 + x2), 4, 4);
95
96 if ( xOffset > 400) {
97 g2.clearRect(0,0,500,500);
98 xOffset = 0;
99 }
100
101 }
102
103 public void upDateGeometry(){
104
105 t += delt;
106
107 double theta1 = Math.PI*t*omega1;
108 double theta2 = Math.PI*t*omega2;
109
110 x1 = (int)(amp1*Math.cos(-theta1));
111 y1 = (int)(amp1*Math.sin(-theta1));
112
113 x2 = (int)(amp2*Math.cos(-theta2));
114 y2 = (int)(amp2*Math.sin(-theta2));
115
116 }
117
118 public static void main (String args[]){
119 PhasorJFrame pjf = new PhasorJFrame();
120
121 // Orderly window closing:
122 pjf.addWindowListener(new WindowAdapter() {
123 public void windowClosing(WindowEvent e) {
124 System.exit(0);
125 }
126 });
127 }
128
129 }