-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimulatorMine.java
More file actions
343 lines (301 loc) · 9.69 KB
/
Copy pathSimulatorMine.java
File metadata and controls
343 lines (301 loc) · 9.69 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.Arrays;
@SuppressWarnings("serial")
public class SimulatorMine2 extends JFrame
{
private JTextField xField;
private JTextField yField;
private JTextField zField;
private JSlider xSlider;
private JSlider ySlider;
private JSlider zSlider;
private TopPanel topPanel;
private SidePanel sidePanel;
private double x=0; // The figure below details h locations
private double y=0; // | Z | Z is to the rear
private double z=0; // Y | |
private double hx=0;// | --- X
private double hy=0;// |/ \|
private double hz=0;// |\___/|
//Physical dimensions
private double diagonal=215;//From carriage pivot to effector pivot
private double railOffset=137;//From center of printer to center of carriage rail
//private double effectorOffset=19.9;//Distance from where diagonal attaches to center printer
//private double carriageOffset=19.5;//Distance from diagonal attaches to carriage to center of carriage rail
//Printable area user measured
private double buildX=170;
private double buildY=170;
private double buildZ=200;//Possibly calculated?
//Calculated dimensions
private double buildXMid=buildX/2;
private double buildYMid=buildY/2;
private double perimeter=2*(railOffset*Math.cos(Math.toRadians(30)));
private double diagonalSq=Math.pow(diagonal,2);
private double[] railX = {perimeter/2,-Math.sqrt(Math.pow(railOffset,2)-Math.pow(perimeter,2)/4)};
private double[] railY = {-perimeter/2,-Math.sqrt(Math.pow(railOffset,2)-Math.pow(perimeter,2)/4)};
private double[] railZ = {0,perimeter*Math.sqrt(3)/2-Math.sqrt(Math.pow(railOffset,2)-Math.pow(perimeter,2)/4)};
private int timeStep=5;
public SimulatorMine2()
{
ActionHandler ah=new ActionHandler();
ChangeHandler ch=new ChangeHandler();
JMenuBar jmb=new JMenuBar();
setJMenuBar(jmb);
JMenu fileMenu=new JMenu("File");
jmb.add(fileMenu);
JMenuItem fileOpenItem=new JMenuItem("Open...");
fileOpenItem.addActionListener(ah);
fileMenu.add(fileOpenItem);
JPanel mainPanel=new JPanel();
mainPanel.setLayout(new GridLayout(1,2));
add(mainPanel);
JPanel inputPanel=new JPanel();
inputPanel.setLayout(new GridLayout(4,1));
add(inputPanel,BorderLayout.NORTH);
JPanel xPanel=new JPanel();
inputPanel.add(xPanel);
xField=new JTextField(5);
xField.setText(""+x);
xPanel.add(new JLabel("X:"));
xPanel.add(xField);
xSlider=new JSlider((int)-diagonal,(int)diagonal,0);
xSlider.addChangeListener(ch);
xPanel.add(xSlider);
JPanel yPanel=new JPanel();
inputPanel.add(yPanel);
yField=new JTextField(5);
yField.setText(""+y);
yPanel.add(new JLabel("Y:"));
yPanel.add(yField);
ySlider=new JSlider((int)-diagonal,(int)diagonal,0);
ySlider.addChangeListener(ch);
yPanel.add(ySlider);
JPanel zPanel=new JPanel();
inputPanel.add(zPanel);
zField=new JTextField(5);
zField.setText(""+z);
zPanel.add(new JLabel("Z:"));
zPanel.add(zField);
zSlider=new JSlider(-100,100,0);
zSlider.addChangeListener(ch);
zPanel.add(zSlider);
topPanel=new TopPanel();
mainPanel.add(topPanel);
sidePanel=new SidePanel();
mainPanel.add(sidePanel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800,600);
setVisible(true);
new Timer().start();
}
private class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser jfc=new JFileChooser();
jfc.setCurrentDirectory(new File("."));
int result=jfc.showOpenDialog(SimulatorMine2.this);
if(result==JFileChooser.CANCEL_OPTION) return;
File f=jfc.getSelectedFile();
if(!f.exists()) return;
try
{
BufferedReader in=new BufferedReader(new FileReader(f));
String line;
ArrayList<String> vertexList=new ArrayList<String>();
ArrayList<String> faceList=new ArrayList<String>();
while((line=in.readLine())!=null)
{
if(line.startsWith("G1")) vertexList.add(line);
}
in.close();
System.out.println(vertexList);
String[] line2 = new String[10];
double[] point = new double[10];
for(String item: vertexList){
line2=item.split(" ");
if(line2[1].substring(0,1).equals("X")){
point[0]=Double.parseDouble(line2[1].substring(1));
if(line2[2].substring(0,1).equals("Y")){
point[1]=Double.parseDouble(line2[2].substring(1));
if(line2[3].substring(0,1).equals("Z")){
point[2]=Double.parseDouble(line2[3].substring(1));
}
}
}
boolean paint=false;
if((int)point[0]!=0){
xSlider.setValue((int)point[0]);
paint=true;
}
if((int)point[1]!=0){
ySlider.setValue((int)point[1]);
paint=true;
}
if((int)point[2]!=0){
zSlider.setValue((int)point[2]);
paint=true;
}
if(paint==true){
try {
Thread.sleep(80);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
topPanel.paint(topPanel.getGraphics());
sidePanel.paint(sidePanel.getGraphics());
paint=false;
}
}
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}
public double getBaseX(){
return Math.sqrt(Math.pow(railX[0]-xSlider.getValue(),2) + Math.pow(railX[1]-ySlider.getValue(),2));
}
public double getBaseY(){
return Math.sqrt(Math.pow(railY[0]-xSlider.getValue(),2) + Math.pow(railY[1]-ySlider.getValue(),2));
}
public double getBaseZ(){
return Math.sqrt(Math.pow(railZ[0]-xSlider.getValue(),2) + Math.pow(railZ[1]-ySlider.getValue(),2));
}
public double getHX(){
return Math.sqrt(this.diagonalSq - Math.pow(getBaseX(),2));
}
public double getHY(){
return Math.sqrt(this.diagonalSq - Math.pow(getBaseY(),2));
}
public double getHZ(){
return Math.sqrt(this.diagonalSq - Math.pow(getBaseZ(),2));
}
private class TopPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.black);
int width=getWidth();
int height=getHeight();
if(width==0) return;
int x1=(int)(width/2+width*x/buildX);
int y1=(int)(height/2-height*y/buildY);
g.fillOval(x1-5,y1-5,10,10);
g.drawLine(x1,y1,50,height-100);
g.drawLine(x1,y1,width-50,height-100);
g.drawLine(x1,y1,width/2,50);
g.drawRect(0,0,width-1,height-1);
}
}
private class SidePanel extends JPanel
{
public void paintComponent(Graphics g)
{
/*
System.out.println("hx"+getHX());
System.out.println("hy"+getHY());
System.out.println("hz"+getHZ());
System.out.println("basex"+getBaseX());
System.out.println("basey"+getBaseY());
System.out.println("basez"+getBaseZ());
System.out.println("perimeter"+perimeter);
System.out.println("railX"+Arrays.toString(railX));
System.out.println("railY"+Arrays.toString(railY));
System.out.println("railZ"+Arrays.toString(railZ));
*/
super.paintComponent(g);
int width=getWidth();
int height=getHeight();
if(width==0) return;
int x1=(int)(width/2+width*x/buildX);
int y1=(int)(height/2-height*z/buildY);
//pillars
g.setColor(Color.red);
g.drawLine(50,0,50,height);
g.drawLine(width/2,0,width/2,height);
g.drawLine(width-50,0,width-50,height);
//effector
g.setColor(Color.black);
g.fillOval(x1-5,y1-5,10,10);
//rods
g.drawLine(x1,y1,width-50,y1-(int)getHX());
g.drawLine(x1,y1,50,y1-(int)getHY());
g.drawLine(x1,y1,width/2,y1-(int)getHZ());
g.drawRect(0,0,width-1,height-1);
g.setColor(Color.gray);
g.fillRect(width-55,y1-(int)getHX(),10,10);
g.fillRect(45,y1-(int)getHY(),10,10);
g.fillRect(width/2-5,y1-(int)getHZ(),10,10);
}
}
private class ChangeHandler implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
if(e.getSource()==xSlider)
{
x=xSlider.getValue();
xField.setText(""+x);
topPanel.repaint();
sidePanel.repaint();
}
else if(e.getSource()==ySlider)
{
y=ySlider.getValue();
yField.setText(""+y);
topPanel.repaint();
sidePanel.repaint();
}
else if(e.getSource()==zSlider)
{
z=zSlider.getValue();
zField.setText(""+z);
topPanel.repaint();
sidePanel.repaint();
}
}
}
private class Timer extends Thread
{
public void run()
{
long currTime;
long prevTime;
try
{
prevTime=System.currentTimeMillis();
for(int i=0;i<10000;i++)
{
z=20*Math.sin(i/100f);
x=20*Math.sin(i/70f);
y=20*Math.sin(i/85f);
topPanel.repaint();
sidePanel.repaint();
currTime=System.currentTimeMillis();
int t=(int)(currTime-prevTime);
if(t<=timeStep) sleep(timeStep-t);
else System.out.println("lagging "+(t-timeStep)+"ms at interval "+i);
prevTime+=timeStep;
}
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
}
public static void main(String[] args)
{
new SimulatorMine2();
}
}