-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphGUI.java
371 lines (274 loc) · 13.5 KB
/
GraphGUI.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
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
366
367
368
369
370
371
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
//nir nicole, mmn13
//Graph GUI genarator
public class GraphGUI extends JFrame {
private static ArrayList<Polynom> polynoms; //this list holds the current polynoms in use
private static AxisesScaleGUIExtention graph = new AxisesScaleGUIExtention(10,10); //the graph itself
private JPanel buttons = new JPanel(); //will contain all jbuttons
private JPanel polynomynalPanel = new JPanel(); // will contain all polynom related jbuttons
private JButton cmdSetX = new JButton("Set X exis");
private JButton cmdSetY = new JButton("Set Y exis");
private JButton cmdClearAll = new JButton("Clear All");
private JButton cmdClearInstance = new JButton("Clear Instance");
private JButton cmdPolynom = new JButton("Add Polynom");
private JButton cmdDerivative = new JButton("Derivative");
private JButton cmdSum = new JButton("Sum");
private JButton cmdSubstruct = new JButton("Substruct");
private JButton cmdClosestPair = new JButton("Find Closest Pair");
private JButton cmdShortestPath = new JButton("Find Shortest Path From u To v");
private JButton cmdCurrentPolynoms = new JButton("Show Polynoms");
private static boolean scaleShift = false; //did a scale shift made
private static double xScale=10;
private static double yScale=10;
//costume constructor with only frame sizes
public GraphGUI(int width, int height) {
super("Graph: Coordinate System");
polynoms = new ArrayList<Polynom>(0);
if(!scaleShift)
graph = new AxisesScaleGUIExtention(5,5);
this.setLayout(new BorderLayout());
buttons.setLayout(new GridLayout(1,3,10,5));
buttons.add(cmdSetX);
buttons.add(cmdSetY);
buttons.add(cmdClearAll);
buttons.add(cmdClearInstance);
polynomynalPanel.setLayout(new GridLayout(7,1,1,1));
polynomynalPanel.add(cmdPolynom);
polynomynalPanel.add(cmdDerivative);
polynomynalPanel.add(cmdSum);
polynomynalPanel.add(cmdSubstruct);
polynomynalPanel.add(cmdClosestPair);
polynomynalPanel.add(cmdShortestPath);
polynomynalPanel.add(cmdCurrentPolynoms);
ButtonListenerCMD listener = new ButtonListenerCMD();
cmdSetX.addActionListener(listener);
cmdSetY.addActionListener(listener);
cmdClearAll.addActionListener(listener);
cmdClearInstance.addActionListener(listener);
cmdPolynom.addActionListener(listener);
cmdDerivative.addActionListener(listener);
cmdCurrentPolynoms.addActionListener(listener);
cmdSum.addActionListener(listener);
cmdSubstruct.addActionListener(listener);
cmdClosestPair.addActionListener(listener);
cmdShortestPath.addActionListener(listener);
polynomynalPanel.setBackground(Color.lightGray);
buttons.setBackground(Color.lightGray);
this.add(buttons, BorderLayout.SOUTH);
this.add(polynomynalPanel, BorderLayout.WEST);
this.add(graph, BorderLayout.CENTER);
this.setSize(width,height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scaleShift = false;
}
//costume constructor with a given array of graph instances
public GraphGUI(int width, int height, ArrayList<GraphInstance> arrayGraphs) {
super("Graph: Coordinate System");
polynoms = new ArrayList<Polynom>(0);
if(!scaleShift)
graph = new AxisesScaleGUIExtention(5,5,arrayGraphs);
this.setLayout(new BorderLayout());
buttons.setLayout(new GridLayout(1,3,10,5));
buttons.add(cmdSetX);
buttons.add(cmdSetY);
buttons.add(cmdClearAll);
buttons.add(cmdClearInstance);
polynomynalPanel.setLayout(new GridLayout(7,1,1,1));
polynomynalPanel.add(cmdPolynom);
polynomynalPanel.add(cmdDerivative);
polynomynalPanel.add(cmdSum);
polynomynalPanel.add(cmdSubstruct);
polynomynalPanel.add(cmdClosestPair);
polynomynalPanel.add(cmdShortestPath);
polynomynalPanel.add(cmdCurrentPolynoms);
ButtonListenerCMD listener = new ButtonListenerCMD();
cmdSetX.addActionListener(listener);
cmdSetY.addActionListener(listener);
cmdClearAll.addActionListener(listener);
cmdClearInstance.addActionListener(listener);
cmdPolynom.addActionListener(listener);
cmdDerivative.addActionListener(listener);
cmdCurrentPolynoms.addActionListener(listener);
cmdSum.addActionListener(listener);
cmdSubstruct.addActionListener(listener);
cmdClosestPair.addActionListener(listener);
cmdShortestPath.addActionListener(listener);
polynomynalPanel.setBackground(Color.lightGray);
buttons.setBackground(Color.lightGray);
this.add(buttons, BorderLayout.SOUTH);
this.add(polynomynalPanel, BorderLayout.WEST);
this.add(graph, BorderLayout.CENTER);
this.setSize(width,height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scaleShift = false;
}
public class ButtonListenerCMD implements ActionListener {
public void actionPerformed(ActionEvent e) {
//sets x scale on the graph for all instances
if(e.getSource() == cmdSetX) {
double tempXscale = Double.parseDouble(JOptionPane.showInputDialog("Enter x axis scale:"));
System.out.println("x changed");
System.out.println("x: " + xScale + "=> " + tempXscale);
for(int pIndex = 0; pIndex<graph.getGraphs().size() ; pIndex++)
{
for(int i=0 ; i<graph.getGraphs().get(pIndex).getListOfPoints().size() ; i++)
{ System.out.println( graph.getGraphs().get(pIndex).getListOfPoints().get(i).getX()+" " + graph.getXScale()+ " " + tempXscale);
double oldXmax = graph.getXScale() * graph.getNumberOfGaps();
double newXmax = tempXscale * graph.getNumberOfGaps();
graph.getGraphs().get(pIndex).getListOfPoints().get(i).setX((double)(0.5 + (oldXmax*graph.getGraphs().get(pIndex).getListOfPoints().get(i).getX())/newXmax) - oldXmax/(2*newXmax) );
System.out.println("(" +graph.getGraphs().get(pIndex).getListOfPoints().get(i).getX() + "," + graph.getGraphs().get(pIndex).getListOfPoints().get(i).getY() + ")");
}
}
xScale = tempXscale;
graph.setXScale(tempXscale);
scaleShift=true;
}
//sets y scale on the graph for all instances
if(e.getSource() == cmdSetY) {
double tempYScale = Double.parseDouble(JOptionPane.showInputDialog("Enter y axis scale:"));
System.out.println("y changed");
System.out.println("y: " + yScale + "=> " + tempYScale);
// a*oldscale = b*newscale => b = a*oldscale/newscale
for(int pIndex = 0; pIndex<graph.getGraphs().size() ; pIndex++)
{
for(int i=0 ; i<graph.getGraphs().get(pIndex).getListOfPoints().size() ; i++)
{ System.out.println( graph.getGraphs().get(pIndex).getListOfPoints().get(i).getY()+" " + graph.getYScale()+ " " + tempYScale);
double oldYmax = graph.getYScale() * graph.getNumberOfGaps();
double newYmax = tempYScale * graph.getNumberOfGaps();
graph.getGraphs().get(pIndex).getListOfPoints().get(i).setY((double)(0.5 - oldYmax/(2*newYmax) + (oldYmax*graph.getGraphs().get(pIndex).getListOfPoints().get(i).getY())/newYmax));
System.out.println("(" +graph.getGraphs().get(pIndex).getListOfPoints().get(i).getX() + "," + graph.getGraphs().get(pIndex).getListOfPoints().get(i).getY() + ")");
}
}
yScale = tempYScale;
graph.setYScale(tempYScale);
scaleShift=true;
}
//clearing instances of graph
if(e.getSource() == cmdClearAll) {
System.out.println("cleared");
graph.cleanGraph();
polynoms.clear();
}
//clearing specific instance
if(e.getSource() == cmdClearInstance) {
String message = "Choose Instance to delete:\n";
message+="Instance" + 0 + ": " + " default graph\n";
for(int i=0; i<polynoms.size() ; i++)
message+="Instance " + (i+1) + ": " + polynoms.get(i) + "\n";
Integer choosePolynom = Integer.parseInt(JOptionPane.showInputDialog(message));
if(choosePolynom==0)
{
graph.removeGraph(0);
}else {
polynoms.remove(choosePolynom - 1);
graph.removeGraph(choosePolynom);
System.out.println("instance " + choosePolynom + " cleared.");
}
}
//adding new polynom
if(e.getSource() == cmdPolynom) {
polynoms.add(NumberReader.polynomReader());
ArrayList<Point> newAP = polynoms.get(polynoms.size()-1).polynomToPoints() ;
GraphInstance newG = new GraphInstance(newAP,false, graph.getMaxX(), graph.getMaxY(), getWidth(), getHeight() ) ;
graph.addToGraph( newG ) ;
}
//Derivative polynom and show it new instance on the graph in red
if(e.getSource() == cmdDerivative) {
String message = "Choose Polynom:\n";
for(int i=0; i<polynoms.size() ; i++)
message+="Polynom " + (i+1) + ": " + polynoms.get(i) + "\n";
Integer choosePolynom = Integer.parseInt(JOptionPane.showInputDialog(message));
Polynom derivativeP = polynoms.get(choosePolynom - 1).derivative();
polynoms.remove(choosePolynom - 1);
polynoms.add(choosePolynom - 1,derivativeP);
ArrayList<Point> newAP = polynoms.get(choosePolynom - 1).polynomToPoints() ;
GraphInstance newG = new GraphInstance(newAP,true, graph.getMaxX(), graph.getMaxY(), getWidth(), getHeight() ) ;
graph.setPolynomInGraph(choosePolynom, newG );
System.out.println("\n\n______\n");
for(int i=0; i<graph.getGraphs().size(); i++)
System.out.println(i + ": \n" + graph.getGraphs().get(i));
System.out.println(choosePolynom + " derivative.");
}
//sum to instances and show the new instance on the graph(and the new polynom created in the list)
if(e.getSource() == cmdSum) {
String message = "Choose Polynom 1:\n";
for(int i=0; i<polynoms.size() ; i++)
message+="Polynom " + (i+1) + ": " + polynoms.get(i) + "\n";
Integer choosePolynom1 = Integer.parseInt(JOptionPane.showInputDialog(message));
message = "Choose Polynom 2:\n";
for(int i=0; i<polynoms.size() ; i++)
message+="Polynom " + (i+1) + ": " + polynoms.get(i) + "\n";
Integer choosePolynom2 = Integer.parseInt(JOptionPane.showInputDialog(message));
Polynom polynomsSum = polynoms.get(choosePolynom1 - 1).plus(polynoms.get(choosePolynom2 - 1));
polynoms.remove(choosePolynom1 - 1);
polynoms.add(choosePolynom1 - 1,polynomsSum);
ArrayList<Point> newAP = polynoms.get(choosePolynom1 - 1).polynomToPoints() ;
GraphInstance newG = new GraphInstance(newAP,false, graph.getMaxX(), graph.getMaxY(), getWidth(), getHeight() ) ;
graph.setPolynomInGraph(choosePolynom1, newG );
graph.removeGraph(choosePolynom2);
polynoms.remove(choosePolynom2 - 1);
}
//substruct 2 polynoms and show the new instance on the graph(and the new polynom created in the list)
if(e.getSource() == cmdSubstruct) {
String message = "Choose Polynom 1:\n";
for(int i=0; i<polynoms.size() ; i++)
message+="Polynom " + (i+1) + ": " + polynoms.get(i) + "\n";
Integer choosePolynom1 = Integer.parseInt(JOptionPane.showInputDialog(message));
message = "Choose Polynom 2:\n";
for(int i=0; i<polynoms.size() ; i++)
message+="Polynom " + (i+1) + ": " + polynoms.get(i) + "\n";
Integer choosePolynom2 = Integer.parseInt(JOptionPane.showInputDialog(message));
Polynom polynomsSub = polynoms.get(choosePolynom1 - 1).minus(polynoms.get(choosePolynom2 - 1));
if( ! polynoms.get(choosePolynom1 - 1).equals(polynoms.get(choosePolynom2 - 1)))
{
polynoms.remove(choosePolynom1 - 1);
polynoms.add(choosePolynom1 - 1,polynomsSub);
ArrayList<Point> newAP = polynoms.get(choosePolynom1 - 1).polynomToPoints() ;
GraphInstance newG = new GraphInstance(newAP,false, graph.getMaxX(), graph.getMaxY(), getWidth(), getHeight() ) ;
graph.setPolynomInGraph(choosePolynom1, newG );
graph.removeGraph(choosePolynom2);
polynoms.remove(choosePolynom2 - 1);
}else
{
if(choosePolynom2>choosePolynom1) {
polynoms.remove(choosePolynom2 - 1);
graph.removeGraph(choosePolynom2);
polynoms.remove(choosePolynom1 - 1);
graph.removeGraph(choosePolynom1);
}else {
polynoms.remove(choosePolynom1 - 1);
graph.removeGraph(choosePolynom1);
polynoms.remove(choosePolynom2 - 1);
graph.removeGraph(choosePolynom2);
}
}
}
//show polynoms that are in display at the moment
if(e.getSource() == cmdCurrentPolynoms) {
String message = "Polynoms:\n";
for(int i=0; i<polynoms.size() ; i++)
message+="Polynom " + (i+1) + ": " + polynoms.get(i) + "\n";
JOptionPane.showMessageDialog(null, message);
System.out.println(" showing polynoms.");
}
//not finished
if(e.getSource() == cmdClosestPair) {
JOptionPane.showMessageDialog(null, "Function in prograss!");
}
//not finished
if(e.getSource() == cmdShortestPath) {
JOptionPane.showMessageDialog(null, "Function in prograss!");
}
repaint();
}//end of event handeler
}//end of action listner
}//end of class