forked from paulinebrisset/JavaGrapher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuiGrapher.java
More file actions
190 lines (172 loc) · 7.62 KB
/
GuiGrapher.java
File metadata and controls
190 lines (172 loc) · 7.62 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
import javax.swing.*;
import Calculator.*;
import Calculator.exception.DivisionByZeroException;
import Calculator.exception.LogByZeroException;
import Calculator.exception.SyntaxeErrorException;
import Calculator.models.Node;
import settings.ColorPalette;
import views.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
public class GuiGrapher extends JFrame {
protected JPanel rightPanel;
protected PositionPanel positionPanel;
protected ActionPanel actionPanel;
protected EvalPanel evalPanel;
protected GrapherPanel grapherPanel;
public GuiGrapher() {
this.setTitle("Grapher");
this.setSize(1200, 700);
// Display it in the middle of the screen
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(1, 2));
ActionListener actionPanelListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("Refresh".equals(e.getActionCommand())) {
handleBtnSubmitClick();
}
if ("Clear".equals(e.getActionCommand())) {
handleBtnClearClick();
}
if ("ZoomIn".equals(e.getActionCommand())) {
handleBtnZoomInClick();
} else if ("ZoomOut".equals(e.getActionCommand())) {
handleBtnZoomOutClick();
}
}
};
// Called when we just zommed out or generated a new graph
ActionListener repaintListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("Repaint".equals(e.getActionCommand())) {
updateActionPanelFields();
handleBtnSubmitClick();
}
}
};
// Initialization of elements:
grapherPanel = new GrapherPanel(repaintListener);
GraphMouse graphMouse = new GraphMouse(grapherPanel);
positionPanel = new PositionPanel(graphMouse);
actionPanel = new ActionPanel(grapherPanel, actionPanelListener);
// Create an ActionListener for the "Submit" button
ActionListener submitListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("Submit".equals(e.getActionCommand())) {
handleBtnSubmitClick();
}
}
};
evalPanel = new EvalPanel(submitListener);
rightPanel = new JPanel();
rightPanel.setBackground(ColorPalette.getSecondColor());
// Build the whole panel
Container content = this.getContentPane();
content.setBackground(ColorPalette.getLabelForegroundColor());
content.setLayout(new BorderLayout());
content.add(rightPanel, BorderLayout.EAST);
content.add(positionPanel, BorderLayout.NORTH);
content.add(actionPanel, BorderLayout.WEST);
content.add(grapherPanel, BorderLayout.CENTER);
content.add(evalPanel, BorderLayout.SOUTH);
this.setVisible(true);
}
public void handleBtnZoomInClick() {
this.grapherPanel.actionZoomIn();
}
public void handleBtnZoomOutClick() {
this.grapherPanel.actionZoomOut();
}
public void updateActionPanelFields() {
actionPanel.setXminField(grapherPanel.getMinX());
actionPanel.setXmaxField(grapherPanel.getMaxX());
actionPanel.setYminField(grapherPanel.getMinY());
actionPanel.setYmaxField(grapherPanel.getMaxY());
actionPanel.setStepField(grapherPanel.getStep());
}
public void handleBtnClearClick() {
grapherPanel.setcheckedEval(false);
grapherPanel.unsetxyPairs();
evalPanel.clearFunction();
grapherPanel.clearGraph();
positionPanel.emptyFx();
}
public void handleBtnSubmitClick() {
String expression = this.evalPanel.getExpression();
try {
String xminStr = actionPanel.getXminField();
String xmaxStr = actionPanel.getXmaxField();
String yminStr = actionPanel.getYminField();
String ymaxStr = actionPanel.getYmaxField();
String stepStr = actionPanel.getStepField();
String xGridStr = actionPanel.getXGridField();
String yGridStr = actionPanel.getYGridField();
grapherPanel.setAutoStep(actionPanel.getAutoStep());
float xmin = xminStr.isEmpty() ? grapherPanel.getMinX() : Float.parseFloat(xminStr);
float xmax = xmaxStr.isEmpty() ? grapherPanel.getMaxX() : Float.parseFloat(xmaxStr);
float ymin = yminStr.isEmpty() ? grapherPanel.getMinY() : Float.parseFloat(yminStr);
float ymax = ymaxStr.isEmpty() ? grapherPanel.getMaxY() : Float.parseFloat(ymaxStr);
float step = stepStr.isEmpty() ? grapherPanel.getStep() : Float.parseFloat(stepStr);
float xGrid = xGridStr.isEmpty() ? grapherPanel.getGridX() : Float.parseFloat(xGridStr);
float yGrid = yGridStr.isEmpty() ? grapherPanel.getGridY() : Float.parseFloat(yGridStr);
grapherPanel.setMinMaxX(xmin, xmax);
grapherPanel.setMinMaxY(ymin, ymax);
grapherPanel.setStep(step);
grapherPanel.setGridX(xGrid);
grapherPanel.setGridY(yGrid);
// update step according to checkbox
step = grapherPanel.getStep();
actionPanel.setStepField(step);
// ADD if expression is not empty
// Create a Map to store the x-y pairs
if (!expression.isEmpty()) {
Map<Float, Float> xyPairs = new HashMap<>();
// Loop through the range of X values and evaluate the function for each X
for (float currentX = xmin; currentX <= xmax; currentX += step) {
float y;
try {
y = Calculator.evaluateExpression(expression, currentX);
} catch (NumberFormatException ex) {
displayErrorMessage("Your input could no be evaluated.");
return;
} catch (DivisionByZeroException ex) {
displayErrorMessage("Division by zero error.");
return;
} catch (LogByZeroException ex) {
displayErrorMessage("Logarithm by zero error.");
return;
}
xyPairs.put(currentX, y); // Store the x-y pair in the Map
}
// If no exceptions occurred, update the graph
this.grapherPanel.setcheckedEval(true);
this.grapherPanel.setxyPairs(xyPairs);
this.grapherPanel.repaint();
}
} catch (SyntaxeErrorException ex) {
displayErrorMessage("Syntax Error: " + ex.getMessage());
}
}
// Helper method to display error messages
private void displayErrorMessage(String message) {
JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
}
public static void main(String[] args) throws SyntaxeErrorException, DivisionByZeroException, LogByZeroException {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GuiGrapher();
}
});
//exemple pour l'évaluateur
Node nd = Calculator.analyse("cos(x)+x");
float result = nd.eval(2);
System.out.println("result: "+ result);
}
}