summaryrefslogtreecommitdiffstats
path: root/voronoi/GeomCanvas.java
blob: 33dc4313addc67f8b4b8871985670c8765505e92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package voronoi;
import java.awt.AWTEvent;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;


public class GeomCanvas extends Canvas implements Runnable {

	private static final long serialVersionUID = 1L;

	static final int VSIZE = 6;

	private cVertexList list;

	private DelaunayTri delTri;

	private ConvexHull2D myCH;

	private voronoi myVor;

	private boolean toClear, simde, simvo, toPaint, toDelTri, toCH, toVoronoi;

	private cVertex movingV;

	private Graphics gContext; // variables for double buffering

	private Image buffer;

	private int CanW, CanH; // width and height of the drawing canvas

	private int w = VSIZE, h = VSIZE; // w. and h. of vertices

	Thread animator;

	GeomCanvas(int cw, int ch, CompGeomTest frame) {
		setSize(cw, ch);
		CanW = cw;
		CanH = ch;
		setBackground(Color.lightGray);
		toClear = simde = simvo = false;
		toPaint = toDelTri = toCH = false;
		list = new cVertexList();
		myCH = new ConvexHull2D(list);
		delTri = new DelaunayTri(list);
		myVor = new voronoi();
		enableEvents(AWTEvent.MOUSE_EVENT_MASK);
		enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
		start();
	}

	public void SetRegime(String state) {
	}

	public void ToBeCleared() {
		toClear = true;
		repaint();
	}

	public void ToDelTri(boolean iftrue) {
		toPaint = true;
		toDelTri = iftrue;
		repaint();
	}

	public void ToVoronoi(boolean iftrue) {
		toPaint = true;
		toVoronoi = iftrue;
		repaint();
	}

	public void ToCH(boolean iftrue) {
		toPaint = true;
		toCH = iftrue;
		repaint();
	}

	public void CoorClear() {
		list.ClearVertexList();
		myCH.ClearHull();
		delTri.ClearDelaunay();
		simde = false;
		simvo = false;
		toPaint = false;
		repaint();
	}

	public void SimDe() {
		simde = true;
		toPaint = true;
		repaint();
	}

	public void SimVo() {
		simvo = true;
		toPaint = true;
		repaint();
	}

	public int GetCount() {
		return list.n;
	}

	public void paint(Graphics g) {
		if (toClear) {
			g.setColor(Color.lightGray);
			g.fillRect(0, 0, CanW, CanH);
			CoorClear();
			toClear = false;
		}

		if (toPaint) {
			if (list == null)
				return;
			if (list.head == null)
				return;
			if (simde) {
				g.drawImage(buffer, 0, 0, this);
				return;
			}
			if(simvo){
				g.drawImage(buffer, 0, 0, this);
				return;
			}
			buffer = createImage(CanW, CanH);
			gContext = buffer.getGraphics();

			if (toDelTri) {
				delTri.Start(list);
				if (delTri.toDraw) {
					gContext.setColor(Color.blue);
					delTri.DrawDelaunayTri(gContext, w, h);
				}
			}
			if (toVoronoi) {
				myVor = new voronoi();
				gContext.setColor(Color.red);
				myVor.Sort(list);
				myVor.DrawVor(gContext);
			}
			if (toCH) {
				myCH = new ConvexHull2D(list);
				myCH.RunHull();
				gContext.setColor(Color.green);
				myCH.DrawHull(gContext, w, h);
			} 
			list.DrawPoints(gContext, w, h);

			g.drawImage(buffer, 0, 0, this);
			myCH.ClearHull();
			delTri.ClearDelaunay();
			myVor.dVoronoiDiagramGenerator();
			toPaint = false;

		}
	}

	public void update(Graphics g) {
		paint(g);
	}

	public void SetPaint() // needed for the applet
	{
		repaint();
	}

	public void processMouseMotionEvent(MouseEvent e) {
		int xset = 0, yset = 0;
		cVertexList listCur = list;
		if (e.getID() != MouseEvent.MOUSE_DRAGGED)
			return;
		xset = e.getX();
		yset = e.getY();
		if (movingV == null) {
			movingV = listCur.FindVertex(xset, yset, w, h);
			if (movingV == null) {
				System.out.println("There is no vertex to be moved");
				return;
			}
		}
		listCur.ResetVertex(movingV, xset, yset);
		toPaint = true;
		repaint();

	}

	public void processMouseEvent(MouseEvent e) {
		int xset = 0, yset = 0;
		cVertex vNear = new cVertex();
		cVertexList listCur = list;

		if (e.getID() != MouseEvent.MOUSE_CLICKED
				&& e.getID() != MouseEvent.MOUSE_RELEASED)
			return;
		if (e.getID() == MouseEvent.MOUSE_RELEASED)
			movingV = null;

		xset = e.getX();
		yset = e.getY();
		if (e.getID() == MouseEvent.MOUSE_CLICKED
				&& e.getButton() == MouseEvent.BUTTON1) {
			listCur.SetVertex(xset, yset);
			toPaint = true;
			// list.PrintVertices();
			repaint();
		}
		if (e.getID() == MouseEvent.MOUSE_CLICKED
				&& e.getButton() == MouseEvent.BUTTON3) {
			vNear = listCur.GetNearVertex(xset, yset);
			if (vNear != null) {
				listCur.Delete(vNear);
				if (listCur.n == 0) {
					toClear = true;
					CoorClear();
				}
			} else
				System.out.println("No vertex can be deleted.");
			toPaint = true;
			repaint();
		}
	}

	public void run() {
		while (Thread.currentThread() == animator) {
			if (simde) {
				gContext.setColor(Color.lightGray);
				gContext.fillRect(0, 0, CanW, CanH);
				list.DrawPoints(gContext, w, h);
				delTri.DeSim(true, gContext, w, h,this);
				toPaint = true;
				delTri.Start(list);
				delTri.DeSim(false, null, 0, 0,null);
				if (delTri.toDraw) {
					gContext.setColor(Color.blue);
					delTri.DrawDelaunayTri(gContext, w, h);
					delTri.DrawCircle(gContext, w, h);
					toPaint = true;
					list.DrawPoints(gContext, w, h);
					repaint();
					try {
						Thread.sleep(1200);
					} 
					catch (InterruptedException e) {
						break;
					}
				}
				simde = false;
				buffer = createImage(CanW, CanH);
				gContext = buffer.getGraphics();
				delTri.ClearDelaunay();
				toPaint = false;
			} 
			else if (simvo) {
				gContext.setColor(Color.lightGray);
				gContext.fillRect(0, 0, CanW, CanH);
				list.DrawPoints(gContext, w, h);
				myVor = new voronoi();
				myVor.VorSim(true, gContext, w, h,this);
				myVor.Sort(list);
				gContext.setColor(Color.red);
				myVor.DrawVor(gContext);
				list.DrawPoints(gContext, w, h);
				toPaint = true;
				repaint();
				try {

					Thread.sleep(1200);
				} catch (InterruptedException e) {
					break;
				}
				simvo = false;
				buffer = createImage(CanW, CanH);
				gContext = buffer.getGraphics();
				toPaint = false;
			}
			try {

				Thread.sleep(100);
			} catch (InterruptedException e) {
				break;
			}

		}
	}

	public void start() {
		animator = new Thread(this);
		animator.start();
	}

	public void stop() {
		animator = null;
		CoorClear();	
	}
}