-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImportActionListener.java
More file actions
277 lines (253 loc) · 11.3 KB
/
Copy pathImportActionListener.java
File metadata and controls
277 lines (253 loc) · 11.3 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
/** Author: Shikhar K Gupta, Foram Joshi
* Project: DNA Pen
* Mentor: Prof. Manish K Gupta
*/
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.picking.PickCanvas;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.media.j3d.*;
import javax.swing.JFileChooser;
import javax.vecmath.Vector3f;
public class ImportActionListener extends MouseAdapter implements ActionListener {
static int pressed=0;
public static int lastx;
public static int lasty;
public static int lastz;
private static final float[] colors = {
// front face (yellow)
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
// back face (green)
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
// right face (blue)
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
// left face (red)
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
// top face (magenta)
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
// bottom face (cyan)
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
};
public static PickCanvas pickCanvas;
public static BranchGroup objRoot;
public static Transform3D transform;
public static TransformGroup tg;
public static SimpleUniverse simpleU;
public static MouseRotate rotateBehaviour;
public static TransformGroup masterTrans;
public static BranchGroup scene;
public static float canvasx;
public static float canvasy;
public static float canvasz;
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JFileChooser fc=new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc = new JFileChooser();
int value = fc.showOpenDialog(null);
if(value == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
MainFrame.projectPath=f.getAbsolutePath();
int x,y,z;
String st;
try {
st=br.readLine();
MainFrame.height=Integer.parseInt(st);
st=br.readLine();
MainFrame.width=Integer.parseInt(st);
st=br.readLine();
MainFrame.depth=Integer.parseInt(st);
for(int i=0;i<MainFrame.width;i++)
for(int j=0;j<MainFrame.height;j++)
for (int k=0;k<MainFrame.depth;k++)
MainFrame.deletedCoordinates[i][j][k]=false;
/*st=br.readLine();
x=Integer.parseInt(st);
st=br.readLine();
y=Integer.parseInt(st);
st=br.readLine();
z=Integer.parseInt(st);*/
while(((st = br.readLine()) != null)){
x=Integer.parseInt(st);
st=br.readLine();
//if(st!=null)
y=Integer.parseInt(st);
st=br.readLine();
//if(st!=null)
z=Integer.parseInt(st);
MainFrame.deletedCoordinates[x][y][z]=true;
}
MainFrame.isImported=true;
System.out.println("Canvas height: "+MainFrame.height);
System.out.println("Canvas width: "+MainFrame.width);
System.out.println("Canvas depth: "+MainFrame.depth);
createCanvas();
}
catch (IOException ep) {
ep.printStackTrace();
}
}
}
public void createCanvas(){
MainFrame.TrueEnableContent();
// Canvas3D is where all the action will be taking place, don't worry, after adding it
// to your layout, you don't have to touch it.
Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
// add Canvas3D
MainFrame.vPanel.setTopComponent(canvas);
simpleU = new SimpleUniverse(canvas); // setup the SimpleUniverse, attach the Canvas3D
//This is very important, the SceneGraph (where all the action takes place) is created
//by calling a function which here is called 'createSceneGraph'.
//The function is not necessary, you can put all your code here, but it is a
//standard in Java3D to create your SceneGraph contents in the function 'createSceneGraph'
scene = createSceneGraph();
//set the ViewingPlatform by setting the canvasx, canvasy, canvasz values as 0.0,0.0,2.0
canvasx = 0.0f;
canvasy = 0.0f;
canvasz = 2.0f;
ViewingPlatform vp = simpleU.getViewingPlatform(); // get the ViewingPlatform of the SimpleUniverse
TransformGroup View_TransformGroup = vp.getMultiTransformGroup().getTransformGroup(0); // get the TransformGroup associated
Transform3D View_Transform3D = new Transform3D(); // create a Transform3D for the ViewingPlatform
View_TransformGroup.getTransform(View_Transform3D); // get the current 3d from the ViewingPlatform
View_Transform3D.setTranslation(new Vector3f(canvasx, canvasy, canvasz)); // set 3d to x=0, y=-1, z= 5
View_TransformGroup.setTransform(View_Transform3D); // assign Transform3D to ViewPlatform
// this will optimize your SceneGraph, not necessary, but it will allow your program to run faster.
scene.compile();
simpleU.addBranchGraph(scene); //add your SceneGraph to the SimpleUniverse
//We now add a pick canvas so that we can get
pickCanvas = new PickCanvas(canvas, scene);
pickCanvas.setMode(PickCanvas.GEOMETRY);
canvas.addMouseListener(this);
}
public static BranchGroup createSceneGraph() {
// This BranchGroup is the root of the SceneGraph, 'objRoot' is the name I use,
// and it is typically the standard name for it, but can be named anything.
objRoot = new BranchGroup();
objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
objRoot.setCapability(BranchGroup.ALLOW_PICKABLE_WRITE);
objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
objRoot.setCapability(BranchGroup.ALLOW_DETACH);
objRoot.setCapability(Node.ENABLE_PICK_REPORTING);
getNewBoxPlane();//creates a plane of cubes, planes can be combined to form the full fledged canvas.
return objRoot;
}
public static void getNewBoxPlane(){
masterTrans=new TransformGroup();
masterTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
masterTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
masterTrans.setCapability(Node.ENABLE_PICK_REPORTING);
masterTrans.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
masterTrans.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
masterTrans.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
float vposition=0.105f;
float hposition=0.105f;
float zposition=0.105f;
for(int i=0;i<MainFrame.width;i++){
for(int j=0;j<MainFrame.height;j++){
for (int k=0;k<MainFrame.depth;k++){
if(MainFrame.deletedCoordinates[i][j][k]==true){
DNAColorCube c1 = new DNAColorCube(0.05f);
c1.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
c1.setCapability(Shape3D.ALLOW_APPEARANCE_OVERRIDE_WRITE);
c1.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
c1.setCapability(Node.ENABLE_PICK_REPORTING);
c1.setCordinate(i, j-MainFrame.height+1, k-MainFrame.depth+1);
tg = new TransformGroup();
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
transform = new Transform3D();
Vector3f vector = new Vector3f(0f+i*hposition,(0f+j*vposition)*-1, (0f+k*zposition)*-1);
transform.setTranslation(vector);
tg.addChild(c1);
//c1.printDetails();
tg.setTransform(transform);
masterTrans.addChild(tg);
}
}
}
}
objRoot.addChild(masterTrans);
rotateBehaviour=new MouseRotate();
rotateBehaviour.setTransformGroup(masterTrans);
rotateBehaviour.setSchedulingBounds(new BoundingSphere());
masterTrans.addChild(rotateBehaviour);
//}
// if(pressed==1)
// importingFunction();
}
public void destroy() { // this function will allow Java3D to clean up upon quiting
simpleU.removeAllLocales();
}
public void importingFunction(){
pressed=0;
}
public void mouseClicked(MouseEvent e)
{
MainFrame.isPDFSaved=false;
MainFrame.isCSVSaved=false;
pickCanvas.setShapeLocation(e);
PickResult result = pickCanvas.pickClosest();
if (result == null) {
System.out.println("Nothing picked");
int xCord=e.getX();
int yCord=e.getY();
System.out.println("Nothing selected at xCord "+xCord+" yCord"+yCord);
}
else {
DNAColorCube s = (DNAColorCube)result.getNode(PickResult.SHAPE3D);
if (s != null) {
MainFrame.deletedCoordinates[s.getX()][(s.getY()*-1)][(s.getZ()*-1)]=true;
lastx=s.getX();
lasty=s.getY();
lastz=s.getZ();
System.out.println(s.getClass().getName());
System.out.println("X cordinate"+s.getX());
System.out.println("Y cordinate"+s.getY());
System.out.println("Z cordinate"+s.getZ());
QuadArray cube = new QuadArray(24, QuadArray.COORDINATES|QuadArray.COLOR_3);
cube.setColors(0, colors);
s.setGeometry(cube);
}
else{
System.out.println("null");
}
}
}
}