View Javadoc

1   /*
2    * Created on Feb 28, 2004
3    * 
4    * To change the template for this generated file go to Window - Preferences -
5    * Java - Code Generation - Code and Comments
6    */
7   package net.sourceforge.kamiwaai;
8   
9   import java.awt.Graphics;
10  import java.awt.Graphics2D;
11  import java.awt.Point;
12  import java.awt.event.MouseEvent;
13  import java.util.StringTokenizer;
14  
15  import net.sourceforge.kamiwaai.geometricalgebra.Line;
16  import net.sourceforge.kamiwaai.geometricalgebra.MultiVector;
17  import net.sourceforge.kamiwaai.geometricalgebra.PointC;
18  import net.sourceforge.kamiwaai.geometricalgebra.SwingDrawable;
19  
20  class CMeineCanvasZ extends KamiWaAiCanvas {
21  	public CMeineCanvasZ(KamiWaAi ai) {
22  		super(ai);
23  	}
24  
25  	public void mousePressed(MouseEvent e) {
26  
27  		// Herausfinden, welche Box gerade aktiviert ist
28  		aktuell = kamiwaai.ModeChoice.getSelection();
29  
30  		// Da nach dem Programmstart keine Box aktiviert ist, muß
31  		// dies getestet werden. Dann wird kein Objekt zurückgeben,
32  		// sondern ein null-Wert
33  		if (aktuell == null)
34  			return;
35  
36  		label = aktuell.getActionCommand();
37  
38  		// Nur wenn die draw-funktion ausgewählt ist, die
39  		// Mausposition merken und zeichnen
40  		if (label.equals("point")) {
41  			pointMode(e);
42  		}
43  
44  		if (label.equals("move")) {
45  			moveMode(e);
46  		}
47  
48  		if (label.equals("circle")) {
49  			circleMode(e);
50  
51  		} // End of if ... circle
52  
53  		if (label.equals("sphere")) {
54  			sphereMode(e);
55  
56  		} // End of if ... sphere
57  
58  		if (label.equals("sphere C,r")) {
59  			sphereRadiusMode(e);
60  		} // End of if sphere C,r
61  
62  		if (label.equals("line")) {
63  			lineMode(e);
64  
65  		} // End of if ... line
66  
67  		if (label.equals("S int l")) {
68  			sphereIntersectLine(e);
69  		} // End of if ... S int l
70  
71  		if (label.equals("S int S")) {
72  			sphereIntersectSphere(e);
73  		} // End of if ... S int S
74  
75  	} // End of mousePressed
76  
77  	/***
78  	 * @param e
79  	 */
80  	private void moveMode(MouseEvent e) {
81  		Point pt = e.getPoint();
82  		this.kamiwaai.selectedPoint = pickPoint(pt);
83  	}
84  
85  	/***
86  	 * @param e
87  	 */
88  	private void pointMode(MouseEvent e) {
89  		if (kamiwaai.sideview == true) {
90  			k = 0;
91  			// pnum is already stepped up in the front view point setting
92  			String key = "P" + String.valueOf(this.kamiwaai.pnum);
93  			PointC ptc3 = (PointC) this.kamiwaai.ENames.get(key);
94  			// This way the coordinate system seems to be left handed (the
95  			// e.getX() makes the z-axis
96  			// to point away from the user, i.e. in the direction of view of
97  			// the user.
98  			ptc3.sete3(e.getX());
99  			ptc3.drawZ(this.kamiwaai.m_malflaecheZ.getGraphics());
100 			this.kamiwaai.ENames.put(key, ptc3);
101 			// For simple free points no input string with the comma
102 			// as string separator for the later use of StringTokenizer
103 			// is required. For dependent objects this will be needed,
104 			// e.g. "P10,S2,L4" for the point P10 created by the intersection
105 			// of sphere S2 and line L4.
106 			this.kamiwaai.Clist.add(key);
107 			kamiwaai.sideview = false;
108 		}
109 	}
110 
111 	public void mouseDragged(MouseEvent e) {
112 
113 		// Herausfinden, welche Box gerade aktiviert ist
114 		aktuell = kamiwaai.ModeChoice.getSelection();
115 
116 		// Da nach dem Programmstart keine Box aktiviert ist, muß
117 		// dies getestet werden. Dann wird kein Objekt zurückgeben,
118 		// sondern ein null-Wert
119 		if (aktuell == null)
120 			return;
121 
122 		label = aktuell.getActionCommand();
123 
124 		// Nur wenn die move-funktion ausgewählt ist, die
125 		// Mausposition merken und neu zeichnen
126 		if (label.equals("move")) {
127 			if (this.kamiwaai.selectedPoint != " ") {
128 				// deleting the selected disk from the two panels
129 				PointC movingPt = (PointC) this.kamiwaai.ENames
130 						.get(this.kamiwaai.selectedPoint);
131 				movingPt.erase(this.kamiwaai.m_malflaeche.getGraphics());
132 				movingPt.eraseZ(this.kamiwaai.m_malflaecheZ.getGraphics());
133 				movingPt.sete3(e.getX());
134 				movingPt.sete2(e.getY());
135 				this.kamiwaai.ENames.put(this.kamiwaai.selectedPoint, movingPt);
136 				this.kamiwaai.mvdPoint = this.kamiwaai.selectedPoint;
137 				this.kamiwaai.rdlist.add(this.kamiwaai.mvdPoint);
138 
139 				redrawPoints();
140 			}
141 
142 		} // End of if ... move
143 
144 	} // End mouseDragged
145 
146 	public void paint(Graphics gS) {
147 		// Clearing the two canvas
148 		//Graphics2D g = (Graphics2D) gF;
149 		//g.clearRect( 0,0,300,300 );
150 		Graphics2D gZ = (Graphics2D) gS;
151 		gZ.clearRect(0, 0, 300, 300);
152 
153 		// Redrawing all points and lines
154 		for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
155 			StringTokenizer st = new StringTokenizer(
156 					(String) this.kamiwaai.Clist.get(i), ",");
157 			String element = st.nextToken();
158 			SwingDrawable renewObject = (SwingDrawable) this.kamiwaai.ENames
159 					.get(element);
160 			renewObject.drawZ(gZ);
161 		}
162 	} // End of paint()
163 
164 	String pickPoint(Point pt) {
165 		double z = getXCoordinate(pt);
166 		double y = getYCoordinate(pt);
167 
168 		for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
169 			StringTokenizer st = new StringTokenizer(
170 					(String) this.kamiwaai.Clist.get(i), ",");
171 			String element = st.nextToken();
172 			if (element.startsWith("P")) {
173 				if (((PointC) this.kamiwaai.ENames.get(element))
174 						.isYZpoint(y, z))
175 					return element;
176 			}
177 		}
178 		return " ";
179 	}
180 
181 	/***
182 	 * Return key of the line that contains pt, if any, or " " otherwise.
183 	 *  
184 	 */
185 	protected String pickLine(Point pt) {
186 		double[] picker = new double[3];
187 		double delta = 3.0;
188 		String linekey = " ";
189 		picker[0] = 0.0;
190 		picker[1] = getYCoordinate(pt);
191 		picker[2] = pt.getX();
192 
193 		for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
194 			StringTokenizer st = new StringTokenizer(
195 					(String) this.kamiwaai.Clist.get(i), ",");
196 			String element = st.nextToken();
197 			if (element.startsWith("L")) {
198 				Line oneline = (Line) this.kamiwaai.ENames.get(element);
199 				double dyz = oneline.distanceYZ(picker);
200 				if (dyz <= delta) {
201 					linekey = element;
202 					delta = dyz;
203 				}
204 			}
205 		}
206 		return linekey;
207 	}
208 
209 	/***
210 	 * @param pt
211 	 * @param Poles
212 	 * @return
213 	 */
214 	public double getDPoles(Point pt, MultiVector Poles) {
215 		return Math.sqrt((getXCoordinate(pt) - getPolesZ(Poles))
216 				* (getXCoordinate(pt) - getPolesZ(Poles))
217 				+ (getYCoordinate(pt) - getPolesY(Poles))
218 				* (getYCoordinate(pt) - getPolesY(Poles)));
219 	}
220 
221 	/***
222 	 * @param pt
223 	 * @param Pole
224 	 * @return
225 	 */
226 	public double getDPole(Point pt, MultiVector Pole) {
227 		return Math.sqrt((getXCoordinate(pt) - getPoleZ(Pole))
228 				* (getXCoordinate(pt) - getPoleZ(Pole))
229 				+ (getYCoordinate(pt) - getPoleY(Pole))
230 				* (getYCoordinate(pt) - getPoleY(Pole)));
231 	}
232 
233 }