-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathHighlightableDataset.java
More file actions
365 lines (332 loc) · 9.88 KB
/
HighlightableDataset.java
File metadata and controls
365 lines (332 loc) · 9.88 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.display;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import org.opensourcephysics.controls.XML;
import org.opensourcephysics.controls.XMLControl;
import org.opensourcephysics.controls.XMLLoader;
/**
* A Dataset that can highlight selected points.
*
* @author Doug Brown
* @created Dec 14 2005
* @version 1.0
*/
public class HighlightableDataset extends Dataset implements Interactive {
// instance fields
boolean[] highlighted = new boolean[1]; // true if highlighted
boolean[] previous;
Color highlightColor = new Color(255, 255, 0, 128);
Shape highlightShape;
Shape[] hitShapes;
int hitIndex = -1;
/**
* Default constructor.
*/
public HighlightableDataset() {
super();
}
/**
* Constructor specifying the marker color.
*
* @param markerColor marker color
*/
public HighlightableDataset(Color markerColor) {
super(markerColor);
}
/**
* Constructor specifying the marker color, line color, and whether
* points are connected.
*
* @param markerColor marker color
* @param lineColor line color
* @param connected true to connect points with line
*/
public HighlightableDataset(Color markerColor, Color lineColor, boolean connected) {
super(markerColor, lineColor, connected);
}
/**
* Appends an (x,y) datum to the Dataset.
*
* @param x the x value
* @param y the y value
*/
public void append(double x, double y) {
super.append(x, y);
adjustCapacity(xpoints.length);
}
/**
* Appends (x,y) arrays to the Dataset.
*
* @param xarray the x array
* @param yarray the y array
*/
public void append(double[] xarray, double[] yarray) {
super.append(xarray, yarray);
adjustCapacity(xpoints.length);
}
/**
* Clear all data from this Dataset.
*/
public void clear() {
super.clear();
previous = highlighted;
highlighted = new boolean[xpoints.length];
}
/**
* Restores previous highlights.
*/
public void restoreHighlights() {
if((previous!=null)&&(previous.length==highlighted.length)) {
highlighted = previous;
}
}
/**
* Clears highlights.
*/
public void clearHighlights() {
for(int i = 0; i<highlighted.length; i++) {
highlighted[i] = false;
}
}
/**
* Sets the highlighted flag for the specified point.
*
* @param i the array index
* @param highlight true to highlight the point
*/
public void setHighlighted(int i, boolean highlight) {
if(i>=highlighted.length) {
adjustCapacity(i+1);
}
highlighted[i] = highlight;
}
/**
* Gets the highlighted flag for the specified point.
*
* @param i the array index
* @return true if point is highlighted
*/
public boolean isHighlighted(int i) {
if(i>=highlighted.length) {
adjustCapacity(i+1);
}
return highlighted[i];
}
/**
* Sets the highlight color.
*
* @param color the color
*/
public void setHighlightColor(Color color) {
highlightColor = new Color(color.getRed(), color.getGreen(), color.getBlue(), 128);
}
/**
* Move an out-of-place datum into its correct position.
*
* @param loc the datum
*/
protected void moveDatum(int loc) {
super.moveDatum(loc);
}
/**
* Sets the highlighted array size to larger of xpoints.length and minLength.
*
* @param minLength minimum capacity required
*/
private synchronized void adjustCapacity(int minLength) {
int len = Math.max(xpoints.length, minLength);
if(highlighted.length==len) {
return;
}
boolean[] temp = highlighted;
highlighted = new boolean[len];
int count = Math.min(temp.length, len);
System.arraycopy(temp, 0, highlighted, 0, count);
}
/**
* Draw this Dataset in the drawing panel.
*
* @param drawingPanel the drawing panel
* @param g the graphics
*/
public void draw(DrawingPanel drawingPanel, Graphics g) {
super.draw(drawingPanel, g);
Graphics2D g2 = (Graphics2D) g;
int offset = getMarkerSize()+4;
int edge = 2*offset;
// increase the clip to include the entire highlight
Shape clipShape = g2.getClip();
g2.setClip(drawingPanel.leftGutter-offset-1, drawingPanel.topGutter-offset-1, drawingPanel.getWidth()-drawingPanel.leftGutter-drawingPanel.rightGutter+2+2*offset, drawingPanel.getHeight()-drawingPanel.bottomGutter-drawingPanel.topGutter+2+2*offset);
Rectangle viewRect = drawingPanel.getViewRect();
if(viewRect!=null) { // decrease the clip if we are in a scroll pane
g2.clipRect(viewRect.x, viewRect.y, viewRect.x+viewRect.width, viewRect.y+viewRect.height);
}
hitShapes = new Shape[index];
for(int i = 0; i<index; i++) {
if(Double.isNaN(ypoints[i])) {
continue;
}
double xp = drawingPanel.xToPix(xpoints[i]);
double yp = drawingPanel.yToPix(ypoints[i]);
hitShapes[i] = new Rectangle2D.Double(xp-offset, yp-offset, edge, edge);
if(!isHighlighted(i)) {
continue;
}
g2.setColor(highlightColor);
g2.fill(hitShapes[i]);
}
g2.setClip(clipShape); // restore the original clip
}
/**
* Returns the Interactive object at a specified pixel position.
* Implements Interactive.
*
* @param panel the drawing panel
* @param xpix the x pixel position on the panel
* @param ypix the y pixel position on the panel
* @return the object
*/
public Interactive findInteractive(DrawingPanel panel, int xpix, int ypix) {
// return hits only within active plot area
int l = panel.getLeftGutter();
int r = panel.getRightGutter();
int t = panel.getTopGutter();
int b = panel.getBottomGutter();
Dimension dim = panel.getSize();
if((xpix<l)||(xpix>dim.width-r)) {
return null;
}
if((ypix<t)||(ypix>dim.height-b)) {
return null;
}
hitIndex = -1;
for(int i = 0; i<index; i++) {
if((hitShapes!=null)&&(i<hitShapes.length)&&(hitShapes[i]!=null)&&hitShapes[i].contains(xpix, ypix)) {
hitIndex = i;
return this;
}
}
return null;
}
public int getHitIndex() {
return hitIndex;
}
/**
* Implements Interactive.
*
* @param enabled ignored
*/
public void setEnabled(boolean enabled) {}
/**
* Implements Interactive.
*
* @return true
*/
public boolean isEnabled() {
return true;
}
/**
* Implements Interactive.
*
* @param x ignored
* @param y ignored
*/
public void setXY(double x, double y) {}
/**
* Implements Interactive.
*
* @param x ignored
*/
public void setX(double x) {}
/**
* Implements Interactive.
*
* @param y ignored
*/
public void setY(double y) {}
/**
* Implements Interactive.
*
* @return the x value at the current hit index, or Double.NaN if none
*/
public double getX() {
if(hitIndex>-1) {
return xpoints[hitIndex];
}
return Double.NaN;
}
/**
* Implements Interactive.
*
* @return the y value at the current hit index, or Double.NaN if none
*/
public double getY() {
if(hitIndex>-1) {
return ypoints[hitIndex];
}
return Double.NaN;
}
/**
* Returns the XML.ObjectLoader for this class.
*
* @return the object loader
*/
public static XML.ObjectLoader getLoader() {
return new Loader();
}
/**
* A class to save and load Dataset data in an XMLControl.
*/
private static class Loader extends XMLLoader {
public void saveObject(XMLControl control, Object obj) {
XML.getLoader(Dataset.class).saveObject(control, obj);
HighlightableDataset data = (HighlightableDataset) obj;
control.setValue("highlighted", data.highlighted); //$NON-NLS-1$
}
public Object createObject(XMLControl control) {
return new HighlightableDataset();
}
public Object loadObject(XMLControl control, Object obj) {
XML.getLoader(Dataset.class).loadObject(control, obj);
HighlightableDataset data = (HighlightableDataset) obj;
boolean[] highlighted = (boolean[]) control.getObject("highlighted"); //$NON-NLS-1$
if(highlighted!=null) {
data.highlighted = highlighted;
}
return data;
}
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/