1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.jbeans.physics.model.standingwave;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26 import java.io.*;
27 import java.util.*;
28 import javax.media.j3d.*;
29 import javax.vecmath.*;
30 import javax.swing.event.*;
31
32 import net.jbeans.j3d.data.*;
33 import net.jbeans.j3d.event.*;
34 import net.jbeans.j3d.modeler.geometry.*;
35
36 import net.jbeans.j3d.renderer.*;
37 import net.jbeans.ui.*;
38 import net.jbeans.ui.window.*;
39
40 import javax.swing.*;
41 import java.awt.event.*;
42 import javax.swing.event.*;
43
44
45
46
47 /***
48 * generally describe StandingWave in here
49 *
50 * @version $Revision: 1.1 $
51 * @author Masahiro Takatsuka (masa@takatsuka.org)
52 * @see Serializable
53 */
54
55 public class StandingWave implements Serializable {
56 private static double sZmax = -Double.MAX_VALUE;
57 private static double sZmin = Double.MAX_VALUE;
58
59 transient protected EventListenerList modelListeners;
60 transient protected EventListenerList dimListeners;
61
62 int x = 20;
63 int y = 20;
64 double xmin = -5;
65 double ymin = -5;
66 double xmax = 5;
67 double ymax = 5;
68 double[] coords;
69 double[][] coordsC;
70
71 private static double f(double x, double y, double t) {
72 double r = Math.sqrt(x*x + y*y);
73 double ans = Math.sin(r + t) * Math.sqrt(r);
74 if (ans > sZmax) {
75 sZmax = ans;
76 } if (ans < sZmin) {
77 sZmin = ans;
78 }
79 return ans;
80 }
81
82 public StandingWave() {
83 initialize();
84 }
85
86 private void initialize() {
87 this.modelListeners = new EventListenerList();
88 this.dimListeners = new EventListenerList();
89 setX(this.x);
90 setY(this.y);
91 }
92
93 /***
94 * Serialization methods
95 */
96 private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
97 s.defaultReadObject();
98 initialize();
99 }
100
101 /***
102 * adds an GeometryListener to the shape
103 */
104 public void addSurfaceListener(ChangeListener l) {
105 this.modelListeners.add(ChangeListener.class, l);
106 }
107
108 /***
109 * removes an GeometryListener from the shape.
110 */
111 public void removeSurfaceListener(ChangeListener l) {
112 this.modelListeners.remove(ChangeListener.class, l);
113 }
114
115 /***
116 * Notify all listeners that have registered interest for
117 * notification on this event type.
118 */
119 protected void fireSurfaceChanged() {
120 Object[] listeners = this.modelListeners.getListenerList();
121
122
123 for (int i = listeners.length - 2; i >= 0; i -= 2) {
124 if (listeners[i] == ChangeListener.class) {
125
126 ((ChangeListener)listeners[i+1]).stateChanged(new ChangeEvent(this));
127 }
128 }
129 }
130
131 /***
132 * adds an GeometryListener to the shape
133 */
134 public void addSurfaceDimensionListener(ChangeListener l) {
135 this.dimListeners.add(ChangeListener.class, l);
136 }
137
138 /***
139 * removes an GeometryListener from the shape.
140 */
141 public void removeSurfaceDimensionListener(ChangeListener l) {
142 this.dimListeners.remove(ChangeListener.class, l);
143 }
144
145 /***
146 * Notify all listeners that have registered interest for
147 * notification on this event type.
148 */
149 protected void fireSurfaceDimensionChanged() {
150 Object[] listeners = this.dimListeners.getListenerList();
151
152
153 for (int i = listeners.length - 2; i >= 0; i -= 2) {
154 if (listeners[i] == ChangeListener.class) {
155
156 ((ChangeListener)listeners[i+1]).stateChanged(new ChangeEvent(this));
157 }
158 }
159 }
160
161 public void setX(int x) {
162 this.x = x;
163 int cnt = this.x * this.y;
164 this.coords = new double[cnt * 4];
165 this.coordsC = new double[4][cnt];
166 fireSurfaceDimensionChanged();
167 }
168
169 public int getX() {
170 return this.x;
171 }
172
173 public void setY(int y) {
174 this.y = y;
175 int cnt = this.x * this.y;
176 this.coords = new double[cnt * 4];
177 this.coordsC = new double[4][cnt];
178 fireSurfaceDimensionChanged();
179 }
180
181 public int getY() {
182 return this.y;
183 }
184
185 public void setTime(double t) {
186 t /= 10.0;
187 int cnt = this.x * this.y;
188 double dx = (this.xmax-this.xmin) / (this.x-1);
189 double dy = (this.ymax-this.ymin) / (this.y-1);
190
191
192 for (int i = 0; i < this.y; i++) {
193 for (int j = 0; j < this.x; j++) {
194 int idx = i * this.x + j;
195 int index = idx * 4;
196 this.coords[index ] = j * dx;
197 this.coords[index + 1] = i * dy;
198 this.coords[index + 2] = f(this.coords[index], this.coords[index + 1], t);
199 this.coords[index + 3] = (this.coords[index + 2] - sZmin) / (sZmax - sZmin);
200 this.coordsC[0][idx] = this.coords[index];
201 this.coordsC[1][idx] = this.coords[index + 1];
202 this.coordsC[2][idx] = this.coords[index + 2];
203 this.coordsC[3][idx] = this.coords[index + 3];
204 }
205 }
206 fireSurfaceChanged();
207 }
208
209 public double[] getSurfaceCoordinatesWithColorsInSingle() {
210 return this.coords;
211 }
212
213 public double[] getSurfaceCoordinatesX() {
214 return this.coordsC[0];
215 }
216
217 public double[] getSurfaceCoordinatesY() {
218 return this.coordsC[1];
219 }
220
221 public double[] getSurfaceCoordinatesZ() {
222 return this.coordsC[2];
223 }
224
225 public double[] getSurfaceColors() {
226 return this.coordsC[3];
227 }
228 }
229