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 CMeineCanvas extends KamiWaAiCanvas {
21  	public CMeineCanvas(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  		} // End of if ... point
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  		this.kamiwaai.rdlist.clear();
84  	}
85  
86  	/***
87  	 * @param e
88  	 */
89  	private void pointMode(MouseEvent e) {
90  		if (kamiwaai.sideview == false) {
91  			k = 0;
92  			PointC ptc = new PointC();
93  			ptc.sete1(e.getX());
94  			ptc.sete2(e.getY());
95  			ptc.draw(this.kamiwaai.m_malflaeche.getGraphics());
96  			this.kamiwaai.pnum = this.kamiwaai.pnum + 1;
97  			String key = "P" + String.valueOf(this.kamiwaai.pnum);
98  			this.kamiwaai.ENames.put(key, ptc);
99  			kamiwaai.sideview = true;
100 		}
101 	}
102 
103 	public void mouseDragged(MouseEvent e) {
104 
105 		// Herausfinden, welche Box gerade aktiviert ist
106 		aktuell = kamiwaai.ModeChoice.getSelection();
107 
108 		// Da nach dem Programmstart keine Box aktiviert ist, muß
109 		// dies getestet werden. Dann wird kein Objekt zurückgeben,
110 		// sondern ein null-Wert
111 		if (aktuell == null)
112 			return;
113 
114 		label = aktuell.getActionCommand();
115 
116 		// Nur wenn die move-funktion ausgewählt ist, die
117 		// Mausposition merken und neu zeichnen
118 		if (label.equals("move")) {
119 			if (this.kamiwaai.selectedPoint != " ") {
120 				// deleting the selected disk from the two panels
121 				PointC movingPt = (PointC) this.kamiwaai.ENames
122 						.get(this.kamiwaai.selectedPoint);
123 				movingPt.erase(this.kamiwaai.m_malflaeche.getGraphics());
124 				movingPt.eraseZ(this.kamiwaai.m_malflaecheZ.getGraphics());
125 				movingPt.sete1(e.getX());
126 				movingPt.sete2(e.getY());
127 				this.kamiwaai.ENames.put(this.kamiwaai.selectedPoint, movingPt);
128 				this.kamiwaai.mvdPoint = this.kamiwaai.selectedPoint;
129 				this.kamiwaai.rdlist.add(this.kamiwaai.mvdPoint);
130 
131 				redrawPoints();
132 			}
133 
134 		} // End of if ... move
135 
136 	} // End mouseDragged
137 
138 	public void paint(Graphics gF) {
139 		// Clearing the two canvas
140 		Graphics2D g = (Graphics2D) gF;
141 		g.clearRect(0, 0, 300, 300);
142 		//Graphics2D gZ = (Graphics2D) m_malflaecheZ.getGraphics();
143 		//gZ.clearRect( 0,0,300,300 );
144 
145 		// Redrawing all points and lines
146 		for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
147 			StringTokenizer st = new StringTokenizer(
148 					(String) this.kamiwaai.Clist.get(i), ",");
149 			String element = st.nextToken();
150 			SwingDrawable renewObject = (SwingDrawable) this.kamiwaai.ENames
151 					.get(element);
152 			renewObject.draw(g);
153 		}
154 	} // End of paint()
155 
156 	/***
157 	 * Return key of point that contains pt, if any, or " "otherwise.
158 	 */
159 	String pickPoint(Point pt) {
160 		double x = getXCoordinate(pt);
161 		double y = getYCoordinate(pt);
162 
163 		for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
164 			StringTokenizer st = new StringTokenizer(
165 					(String) this.kamiwaai.Clist.get(i), ",");
166 			String element = st.nextToken();
167 			if (element.startsWith("P")) {
168 				if (((PointC) this.kamiwaai.ENames.get(element))
169 						.isXYpoint(x, y))
170 					return element;
171 			}
172 		}
173 		return " ";
174 	}
175 
176 	/***
177 	 * Return key of the line that contains pt, if any, or " " otherwise.
178 	 *  
179 	 */
180 	protected String pickLine(Point pt) {
181 		double[] picker = new double[3];
182 		double delta = 3.0;
183 		String linekey = " ";
184 		picker[0] = pt.getX();
185 		picker[1] = getYCoordinate(pt);
186 		picker[2] = 0.0;
187 
188 		for (int i = 0; i < this.kamiwaai.Clist.size(); i++) {
189 			StringTokenizer st = new StringTokenizer(
190 					(String) this.kamiwaai.Clist.get(i), ",");
191 			String element = st.nextToken();
192 			if (element.startsWith("L")) {
193 				Line oneline = (Line) this.kamiwaai.ENames.get(element);
194 				double dxy = oneline.distanceXY(picker);
195 				if (dxy <= delta) {
196 					linekey = element;
197 					delta = dxy;
198 				}
199 			}
200 		}
201 		return linekey;
202 	}
203 
204 	/***
205 	 * @param pt
206 	 * @param Poles
207 	 * @return
208 	 */
209 	public double getDPoles(Point pt, MultiVector Poles) {
210 		return Math.sqrt((getXCoordinate(pt) - getPolesX(Poles))
211 				* (getXCoordinate(pt) - getPolesX(Poles))
212 				+ (getYCoordinate(pt) - getPolesY(Poles))
213 				* (getYCoordinate(pt) - getPolesY(Poles)));
214 	}
215 
216 	/***
217 	 * @param pt
218 	 * @param Pole
219 	 * @return
220 	 */
221 	public double getDPole(Point pt, MultiVector Pole) {
222 		return Math.sqrt((getXCoordinate(pt) - getPoleX(Pole))
223 				* (getXCoordinate(pt) - getPoleX(Pole))
224 				+ (getYCoordinate(pt) - getPoleY(Pole))
225 				* (getYCoordinate(pt) - getPoleY(Pole)));
226 	}
227 }