-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGeoPainter.java
313 lines (270 loc) · 7.29 KB
/
GeoPainter.java
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
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.font.*;
import java.awt.event.*;
import java.awt.geom.*;
public class GeoPainter extends Painter{
private GeoWindow window;
private ASH manager;
private Structure geometry;
private ViewPoint lookout;
private View scene;
private int updatedScene = 0;
private boolean drawcell = false;
private boolean drawaxis = false;
private boolean followCenter = true;
private boolean displayFrame = false;
public static boolean MOUSE_INFO = false;
public static boolean MOUSE_PICK = false;
private boolean initDone = false;
private int width;
private int height;
public GeoPainter(GeoWindow window, ASH manager){
this.window = window;
this.width = window.getWidth();
this.height = window.getHeight();
this.manager = manager;
this.geometry = null;
this.lookout = null;
this.scene = null;
}
public void setGeometry(Structure geo){
this.geometry = geo;
}
public void setViewPoint(ViewPoint look){
this.lookout = look;
}
public ASH getManager(){
return this.manager;
}
public Structure getGeometry(){
return this.geometry;
}
public ViewPoint getViewPoint(){
return this.lookout;
}
public View getView(){
return this.scene;
}
public void resetViewPoint(){
try{
this.lookout = ViewPoint.resetViewPoint(this.geometry,this.lookout);
updateGeo();
} catch(Exception error){
if(!ASH.runningScript){
System.out.println("could not reset the view");
}
}
}
public void initGeo(Structure geo, ViewPoint look){
this.geometry = geo;
this.lookout = look;
this.scene = new View(geo,look,width,height);
stillActive = true;
updatedScene = 5;
initDone = true;
}
public void updateGeo(Structure geo, ViewPoint look){
this.geometry = geo;
this.lookout = look;
this.scene = new View(geo,look,width,height);
updatedScene = 5;
}
public void updateGeo(Structure geo){
this.geometry = geo;
this.scene = new View(geo,lookout,width,height);
updatedScene = 5;
}
public void updateGeo(ViewPoint look){
this.lookout = look;
this.scene = new View(geometry,look,width,height);
updatedScene = 5;
}
public void updateGeo(){
this.scene = new View(this.geometry,this.lookout,width,height);
updatedScene = 5;
}
public void resolveCommands(){
double sideD = 0.0;
double upD = 0.0;
if(leftPressed){
sideD -= 0.1;
}
if(rightPressed){
sideD += 0.1;
}
if(upPressed){
upD += 0.1;
}
if(downPressed){
upD -= 0.1;
}
if(wheelClicks != 0){
upD -= 0.3*wheelClicks;
wheelClicks = 0;
}
if(mousePressed){
if((dragX-dragStartX) != 0 || (dragY-dragStartY) != 0){
if(shiftPressed){
int natoms = geometry.countParticles();
Vector side = lookout.getSide();
Vector up = lookout.getUp();
Vector shift = side.times(0.1*(dragX-dragStartX)).plus(up.times(-0.1*(dragY-dragStartY)));
for(int i=0; i<natoms; i++){
if(geometry.getParticle(i).isPicked()){
geometry.getParticle(i).shiftCoordinates(shift);
}
}
geometry.forcePeriodicBounds();
updateGeo();
dragStartX = dragX;
dragStartY = dragY;
} else if(rotPressed) {
int natoms = geometry.countParticles();
Vector side = lookout.getSide();
Vector up = lookout.getUp();
Quaternion rotator1 = new Quaternion(up, 0.01*(dragX-dragStartX));
Quaternion rotator2 = new Quaternion(side,0.01*(dragY-dragStartY));
Quaternion rotator = rotator1.times(rotator2);
for(int i=0; i<natoms; i++){
if(geometry.getParticle(i).isPicked()){
geometry.getParticle(i).rotate(rotator);
}
}
updateGeo();
dragStartX = dragX;
dragStartY = dragY;
} else {
try{
lookout.rotateViewPoint((double)(dragX-dragStartX)*-0.01,(double)(dragY-dragStartY)*0.01,geometry.getCenter());
updateGeo();
dragStartX = dragX;
dragStartY = dragY;
} catch(Exception error){
}
}
}
}
if(sideD != 0 || upD != 0){
try{
lookout.shiftViewPoint(0.4*sideD,10*upD);
//lookout.rotateViewPoint(sideD,upD,geometry.getCenter());
updateGeo();
} catch(Exception error){}
}
if(nextPressed){
if(updatedScene < 3){
this.manager.nextFrame();
updateGeo();
updatedScene = 7;
}
}
if(prevPressed){
if(updatedScene < 3){
this.manager.previousFrame();
updateGeo();
updatedScene = 7;
}
}
}
public void mouseClicked(int x, int y, int number){
if(MOUSE_INFO){
manager.listMouseInfo(x,y);
}
if(MOUSE_PICK){
manager.pickByMouse(x,y,number);
}
}
public void setShowAxis(boolean yesno){
this.drawaxis = yesno;
}
public void setShowCell(boolean yesno){
this.drawcell = yesno;
}
public void setShowFrame(boolean yesno){
this.displayFrame = yesno;
}
public void aimAtCenter(){
if(followCenter && !ASH.runningScript){
this.lookout.aimAtCenter(this.geometry);
}
}
public void recordSize(int x, int y){
//System.out.println("new dimensions: "+x+", "+y);
this.width = x;
this.height = y;
this.setBounds(0,0,x,y);
this.updateGeo();
}
private void arrange(Graphics2D g){
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(View.STROKE_WIDTH));
if(!initDone){
g.setColor(Color.black);
g.fillRect(0,0,width,height);
} else {
g.setColor(View.bg);
g.fillRect(0,0,width,height);
Projection[] atomList = scene.getProjections();
for(int i=0; i<atomList.length; i++){
if(atomList[i].isVisible()){
Shape blob = atomList[i].getAtomicShape(width,height);
g.setPaint(atomList[i].getLineColor());
g.draw(blob);
g.setPaint(atomList[i].getColor());
g.fill(blob);
}
}
if(drawcell){
g.setPaint(View.transRed);
g.draw(scene.getCellAxisLine(0));
g.setPaint(View.transGreen);
g.draw(scene.getCellAxisLine(1));
g.setPaint(View.transBlue);
g.draw(scene.getCellAxisLine(2));
}
if(drawaxis){
g.setPaint(View.transLightRed);
g.draw(scene.getUnitAxisLine(0));
g.setPaint(View.transLightGreen);
g.draw(scene.getUnitAxisLine(1));
g.setPaint(View.transLightBlue);
g.draw(scene.getUnitAxisLine(2));
}
if(displayFrame){
FontRenderContext frc = g.getFontRenderContext();
Font f = new Font("Helvetica",Font.PLAIN, 10);
String s = "frame "+(this.manager.getCurrentFrame()+1)+" / "+this.manager.getNumberOfFrames()+" "+this.manager.getCurrentFrameName();
TextLayout tl = new TextLayout(s, f, frc);
Dimension theSize=getSize();
g.setColor(View.black);
tl.draw(g, 20, 20);
}
}
}
public BufferedImage getPainting(){
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = bi.createGraphics();
arrange(graphics);
return bi;
}
public void paint(long dt){
if(updatedScene > 2){
updatedScene--;
if(updatedScene < 5){
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
arrange(g);
// we've completed drawing so clear up the graphics
// and flip the buffer over
g.dispose();
strategy.show();
}
}
}
}