-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTriangularByteLattice.java
More file actions
345 lines (319 loc) · 10.7 KB
/
TriangularByteLattice.java
File metadata and controls
345 lines (319 loc) · 10.7 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
/*
* 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.display2d;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Shape;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import org.opensourcephysics.display.DisplayRes;
import org.opensourcephysics.display.DrawingPanel;
import org.opensourcephysics.display.InteractivePanel;
import org.opensourcephysics.display.Measurable;
import org.opensourcephysics.display.axes.XAxis;
import org.opensourcephysics.display.axes.XYAxis;
/**
* A TriangularByteLattice is an array where each array element can assume one
* of 256 values. Values can be set between -128 and 127. Because byte values
* larger than 127 overflow to negative, values can also be set between 0 and
* 255.
*
* @author Joshua Gould
* @author Wolfgang Christian
* @created May 13, 2003
* @version 1.0
*/
public class TriangularByteLattice implements Measurable {
int nrow, ncol;
byte[][] data;
double xmin, ymin, xmax, ymax;
double xminLattice, yminLattice, xmaxLattice, ymaxLattice;
final static double SQRT3_OVER2 = Math.sqrt(3)/2.0;
boolean visible = true;
Color[] colors = new Color[256];
final static int radius = 3;
final static int diameter = radius*2;
JFrame legendFrame;
/**
* Constructs a byte lattice with the given size. Cell values are -128 to 127.
*
* @param _row the number of rows
* @param _col the number of columns
*/
public TriangularByteLattice(int _row, int _col) {
nrow = _row;
ncol = _col;
createDefaultColors();
data = new byte[nrow][ncol];
// col in x direction, row in y direction
xminLattice=xmin = 0;
xmaxLattice =xmax = ncol-0.5;
ymin = (nrow-1)*SQRT3_OVER2;
if(ymin==0) {
ymin = SQRT3_OVER2; // FIXME
}
yminLattice = ymin;
ymaxLattice=ymax = 0; // zero is on top
}
public void resizeLattice(int _row, int _col) {
nrow = _row;
ncol = _col;
data = new byte[nrow][ncol];
// col in x direction, row in y direction
xminLattice=xmin = 0;
xmaxLattice =xmax = ncol-0.5;
ymin = (nrow-1)*SQRT3_OVER2;
if(ymin==0) {
ymin = SQRT3_OVER2; // FIXME
}
yminLattice = ymin;
ymaxLattice=ymax = 0; // zero is on top
}
public void setVisible (boolean _vis) {
this.visible = _vis;
}
public void setMinMax(double xmin, double xmax, double ymin, double ymax) {
this.xmin = xmin;
this.xmax = xmax;
this.ymin = ymin;
this.ymax = ymax;
}
/**
* Draws the lattice.
*
* @param panel
* @param g
*/
public void draw(DrawingPanel panel, Graphics g) {
if(!visible) {
return;
}
Shape oldClip = g.getClip();
g.setClip(null);
double xScale=(xmax-xmin)/(xmaxLattice-xminLattice);
double yScale=-(ymax-ymin)/(ymaxLattice-yminLattice);
for(int yi = 0; yi<nrow; yi++) {
for(int xi = 0; xi<ncol; xi++) {
byte val = data[yi][xi];
g.setColor(colors[val&0xFF]);
// each row is sqrt(3)/2 offset previous row
if((yi%2)==1) {
double x=(xi+0.5)*xScale+xmin;
double y=yi*SQRT3_OVER2*yScale+ymin;
g.fillOval(panel.xToPix(x)-radius, panel.yToPix(y)-radius, diameter, diameter); // shift to right by 0.5
} else {
double x=xi*xScale+xmin;
double y=yi*SQRT3_OVER2*yScale+ymin;
g.fillOval(panel.xToPix(x)-radius, panel.yToPix(y)-radius, diameter, diameter);
}
}
}
g.setClip(oldClip);
}
/**
* Sets a block of cells to new values.
*
* @param row_offset
* @param col_offset
* @param val
*/
public void setBlock(int row_offset, int col_offset, byte val[][]) {
if((row_offset<0)||(row_offset+val.length>nrow)) {
throw new IllegalArgumentException("Row index out of range in binary lattice setBlock."); //$NON-NLS-1$
}
if((col_offset<0)||(col_offset+val[0].length>ncol)) {
throw new IllegalArgumentException("Column index out of range in binary lattice setBlock."); //$NON-NLS-1$
}
for(int rindex = row_offset, nr = val.length+row_offset; rindex<nr; rindex++) {
for(int cindex = col_offset, nc = val[0].length+col_offset; cindex<nc; cindex++) {
data[rindex][cindex] = val[rindex-row_offset][cindex-col_offset];
}
}
}
/**
* Sets a block of cells to new values.
*
* @param row_offset
* @param col_offset
* @param val
*/
public void setBlock(int row_offset, int col_offset, int val[][]) {
if((row_offset<0)||(row_offset+val.length>nrow)) {
throw new IllegalArgumentException("Row index out of range in binary lattice setBlock."); //$NON-NLS-1$
}
if((col_offset<0)||(col_offset+val[0].length>ncol)) {
throw new IllegalArgumentException("Column index out of range in binary lattice setBlock."); //$NON-NLS-1$
}
for(int rindex = row_offset, nr = val.length+row_offset; rindex<nr; rindex++) {
for(int cindex = col_offset, nc = val[0].length+col_offset; cindex<nc; cindex++) {
data[rindex][cindex] = (byte) val[rindex-row_offset][cindex-col_offset];
}
}
}
/**
* Sets a column of cells to new values.
*
* @param row_offset
* @param col
* @param val
*/
public void setCol(int row_offset, int col, byte val[]) {
if((row_offset<0)||(row_offset+val.length>nrow)) {
throw new IllegalArgumentException("Row index out of range in binary lattice setBlock."); //$NON-NLS-1$
}
if((col<0)||(col>=ncol)) {
throw new IllegalArgumentException("Column index out of range in binary lattice setBlock."); //$NON-NLS-1$
}
for(int rindex = row_offset, nr = val.length+row_offset; rindex<nr; rindex++) {
data[rindex][col] = val[rindex-row_offset];
}
}
/**
* Sets a row of cells to new values.
*
* @param row
* @param col_offset
* @param val
*/
public void setRow(int row, int col_offset, byte val[]) {
if((row<0)||(row>=nrow)) {
throw new IllegalArgumentException("Row index out of range in binary lattice setBlock."); //$NON-NLS-1$
}
if((col_offset<0)||(col_offset+val.length>ncol)) {
throw new IllegalArgumentException("Column index out of range in binary lattice setBlock."); //$NON-NLS-1$
}
for(int cindex = col_offset, nc = val.length+col_offset; cindex<nc; cindex++) {
data[row][cindex] = val[cindex-col_offset];
}
}
/**
* Sets a lattice cell to a new value.
*
* @param row
* @param col
* @param val
*/
public void setCell(int row, int col, byte val) {
data[row][col] = val;
// note that increasing x corresponds to an increaing column index
}
/**
* Gets a lattice cell value.
*
* @param row
* @param col
* @return the cell value.
*/
public int getCell(int row, int col) {
return data[row][col];
}
/** Ranomizes the lattice values. */
public void randomize() {
Random random = new Random();
for(int rindex = 0, nr = data.length; rindex<nr; rindex++) {
for(int cindex = 0, nc = data[0].length; cindex<nc; cindex++) {
data[rindex][cindex] = (byte) random.nextInt(256);
}
}
}
/** Shows the color associated with each value. */
public void showLegend() {
InteractivePanel dp = new InteractivePanel();
dp.setPreferredSize(new java.awt.Dimension(300, 66));
dp.setPreferredGutters(0, 0, 0, 35);
dp.setClipAtGutter(false);
if((legendFrame==null)||!legendFrame.isDisplayable()) {
legendFrame = new JFrame(DisplayRes.getString("GUIUtils.Legend")); //$NON-NLS-1$
}
legendFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
legendFrame.setResizable(false);
legendFrame.setContentPane(dp);
TriangularByteLattice lattice = new TriangularByteLattice(1, 256);
lattice.setMinMax(-128, 127, 0, 1);
byte[][] data = new byte[1][256];
for(int i = 0; i<256; i++) {
data[0][i] = (byte) i;
}
lattice.setBlock(0, 0, data);
dp.addDrawable(lattice);
XAxis xaxis = new XAxis(""); //$NON-NLS-1$
xaxis.setLocationType(XYAxis.DRAW_AT_LOCATION);
xaxis.setLocation(-0.5);
xaxis.setEnabled(true);
dp.addDrawable(xaxis);
legendFrame.pack();
legendFrame.setVisible(true);
}
/**
* Sets the color palette.
*
* @param colors
*/
public void setColorPalette(Color[] colors) {
for(int i = 0, n = colors.length; i<n; i++) {
this.colors[i] = colors[i];
}
for(int i = colors.length; i<256; i++) {
this.colors[i] = Color.black;
}
}
/**
* Sets the color for a single index.
*
* @param i
* @param color
*/
public void setIndexedColor(int i, Color color) {
// i = i % colors.length;
i = (i+256)%colors.length;
colors[i] = color;
}
public boolean isMeasured() {
return true;
}
public double getXMin() {
return xmin;
}
public double getYMin() {
return ymin;
}
public double getXMax() {
return xmax;
}
public double getYMax() {
return ymax;
}
public void createDefaultColors() {
for(int i = 0; i<256; i++) {
Color c = Color.getHSBColor((-0.07f+0.80f*i/255f)%1, 1, 1);
colors[i] = c;
}
}
}
/*
* 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
*/