-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphPanel.java
More file actions
322 lines (290 loc) · 11.2 KB
/
GraphPanel.java
File metadata and controls
322 lines (290 loc) · 11.2 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
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GraphPanel extends JPanel implements MouseListener
{
String expression;
GraphingCalculator address;
JFrame miniXYdisplay = new JFrame();
JPanel miniXYdisplayWindow = new JPanel();
JTextField xTextField = new JTextField();
JTextField yTextField = new JTextField();
double xPixelsToValueConversionFactor;
double yPixelsToValueConversionFactor;
JTextArea inputArea = new JTextArea("x= ");
JTextArea outputArea = new JTextArea("y= ");
double[] xValues;
double[] yValues;
boolean negX=false;
boolean negY=false;
double xRange;
double yRange;
int xSize;
int ySize;
double yScale;
double xMin;
double xMax;
double yMin;
double yMax;
double xScale;
public GraphPanel (String expression, // CONSTRUCTOR
double[] xValues,
double[] yValues,
GraphingCalculator calculatorProgram)
throws IllegalArgumentException
{
// To-dos for this constructor method:
// 1 Verify arrays are same size
xSize = xValues.length;
ySize = yValues.length;
if(xSize != ySize){
throw new IllegalArgumentException("Error: x and y coordinate sizes do not match");
}
this.xValues = xValues;
this.yValues = yValues;
// Find x values range
xMin=xValues[0];
xMax=xValues[0];
for(int i=0;i<xSize;i++){
if(xValues[i]<0)
negX=true;
if(xValues[i]<xMin)
xMin = xValues[i];
else if(xValues[i]>xMax)
xMax = xValues[i];
}
xRange = (int) (xMax-xMin);
// Find y values range
yMin=yValues[0];
yMax=yValues[0];
for(int j=0;j<ySize;j++){
if(yValues[j]<0)
negY=true;
if(yValues[j]<yMin)
yMin = yValues[j];
else if(yValues[j]>yMax)
yMax = yValues[j];
}
yRange = (int) (yMax-yMin);
// 2 Verify x increment is positive
if(calculatorProgram.getxIncrement()<=0)
throw new IllegalArgumentException("Error: x-Increment is not positive");
// 3 Save Calculator address for call back
address = calculatorProgram;
// 4 Save expression for call back
this.expression = expression;
address.GraphFrame.setTitle(this.expression);
// 5 Register with the panel as MouseListener
address.Graph.addMouseListener(this);
// 6 Calculate Y scale values (and save them)
yScale = scaleValues(yMin, yMax, yRange);
yScale = Math.round(yScale);
// 7 Build miniXYdisplayWindow (reuse for each mouse click!)
miniXYdisplayWindow.setLayout(new GridLayout(2,2));
miniXYdisplayWindow.add(inputArea);
miniXYdisplayWindow.add(xTextField);
miniXYdisplayWindow.add(outputArea);
miniXYdisplayWindow.add(yTextField);
miniXYdisplay.add(miniXYdisplayWindow);
miniXYdisplay.setSize(100, 100);
}
@Override
public void paint(Graphics g) // overrides paint() in JPanel!
{
// 1 Calculate x and y pixels-to-value conversion factors
// 2 Do ALL drawing here in paint()
Rectangle r = address.Graph.getBounds();
g.clearRect(address.Graph.getX(), address.Graph.getY(), r.width, r.height);
int xAxisSize = (r.width-25);
int yAxisSize = r.height-20;
if(negY)
g.drawLine(25, r.height/2, r.width-25, r.height/2);
else
g.drawLine(25, r.height-20, r.width-25, r.height-20);
if(negX)
g.drawLine(r.width/2, r.height-20, r.width/2, 20);
else
g.drawLine(25, r.height-20, 25, 20);
xScale = xAxisSize/(xSize+1);
yScale = yAxisSize/(ySize+1);
xPixelsToValueConversionFactor = xScale/(xRange/(double)xSize);
yPixelsToValueConversionFactor = yScale/(yRange/(double)ySize);
for(int i = 0;i<(xSize+1);i++){
if(negY){
g.drawLine(25+i*(int)xScale, r.height/2, 25+i*(int)xScale, (r.height/2)-5);
g.drawString(new DecimalFormat("#.##").format(xMin+(xRange/(double)xSize)*i),28+(int)xScale*i ,(r.height/2)-5);
}
else{
g.drawLine(25+i*(int)xScale, r.height-20, 25+i*(int)xScale, r.height-5);
g.drawString(new DecimalFormat("#.##").format(xMin+(xRange/(double)xSize)*i),28+(int)xScale*i ,r.height-5);
}
if(negX){
g.drawLine((r.width/2)-5, r.height-20-(int)yScale*i, r.width/2, r.height-20-(int)yScale*i);
g.drawString(new DecimalFormat("#.##").format(yMin+(yRange/(double)ySize)*i), r.width/2+5, r.height-12-(int)yScale*i);
}
else{
g.drawLine(15, r.height-20-(int)yScale*i, 25, r.height-20-(int)yScale*i);
g.drawString(new DecimalFormat("#.##").format(yMin+(yRange/(double)ySize)*i), 0, r.height-12-(int)yScale*i);
}
if(i<xSize){
if(negX){
if(negY){
if(i>0){
g.drawLine((int)(xPixelsToValueConversionFactor*xValues[i])+(r.width/2), (r.height/2)-(int)(yPixelsToValueConversionFactor*yValues[i]), (int)(xPixelsToValueConversionFactor*xValues[i-1])+(r.width/2), (r.height/2)-(int)(yPixelsToValueConversionFactor*yValues[i-1]));
}
g.drawOval((int)(xPixelsToValueConversionFactor*xValues[i])+(r.width/2),(r.height/2)-(int)(yPixelsToValueConversionFactor*yValues[i]), 4, 4);
}
else{
if(i>0){
g.drawLine((int)(xPixelsToValueConversionFactor*xValues[i])+(r.width/2),r.height-22-(int)(yPixelsToValueConversionFactor*yValues[i]), (int)(xPixelsToValueConversionFactor*xValues[i-1])+(r.width/2),r.height-22-(int)(yPixelsToValueConversionFactor*yValues[i-1]));
}
g.drawOval((int)(xPixelsToValueConversionFactor*xValues[i])+(r.width/2),r.height-22-(int)(yPixelsToValueConversionFactor*yValues[i]), 4, 4);
}
}
else{
if(negY){
if(i>0){
g.drawLine((int)(xPixelsToValueConversionFactor*xValues[i])+23,(r.height/2)-(int)(yPixelsToValueConversionFactor*yValues[i]), (int)(xPixelsToValueConversionFactor*xValues[i-1])+23, (r.height/2)-(int)(yPixelsToValueConversionFactor*yValues[i-1]));
}
g.drawOval((int)(xPixelsToValueConversionFactor*xValues[i])+23,(r.height/2)-(int)(yPixelsToValueConversionFactor*yValues[i]), 4, 4);
}
else{
if(i>0){
g.drawLine((int)(xPixelsToValueConversionFactor*xValues[i])+23,r.height-22-(int)(yPixelsToValueConversionFactor*yValues[i]), (int)(xPixelsToValueConversionFactor*xValues[i-1])+23, r.height-22-(int)(yPixelsToValueConversionFactor*yValues[i-1]));
}
g.drawOval((int)(xPixelsToValueConversionFactor*xValues[i])+23,r.height-22-(int)(yPixelsToValueConversionFactor*yValues[i]), 4, 4);
}
}
}
}
}
@Override
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
@Override
public void mousePressed(MouseEvent me) {
// xTextField and yTextField are in the miniXYdisplayWindow
if(address.dim!=address.graphPanel.getSize()){
address.graphics = address.Graph.getGraphics();
paint(address.graphics);
address.dim = address.graphPanel.getSize();
}
int xInPixels =me.getX();
double xValue = 0.00;
if(negX){
int x = address.Graph.getBounds().width;
xValue = xInPixels-(x/2);
xValue /= xPixelsToValueConversionFactor;
}
else{
xValue = xInPixels-23;
xValue /= xPixelsToValueConversionFactor;
}
String xValueString = String.valueOf(xValue);
if(xValueString.contains("."))
xValueString = xValueString.substring(0, xValueString.indexOf(".")+3);
xTextField.setText(xValueString);
String yValueString = address.calculate(expression,xValueString);
if(yValueString.contains("."))
yValueString = yValueString.substring(0, yValueString.indexOf(".")+3);
yTextField.setText(yValueString);
// show mini x,y display window
miniXYdisplay.setLocation(me.getXOnScreen(), me.getYOnScreen());
miniXYdisplay.setVisible(true);
}
@Override
public void mouseReleased(MouseEvent arg0) {
// "erase" mini x,y display window
miniXYdisplay.setVisible(false);
}
private double scaleValues(double min, double max, double yRange2) {
int initialIncrement;
int upperIncrement;
int lowerIncrement;
int selectedIncrement;
String zeros = "0000000000";
// 1) Determine the RANGE to be plotted.
// already done
// 2) Determine an initial increment value.
if (yRange2 >= 10)
{
yRange2 = (int)yRange2;
}
else
{
System.out.println("Add handling of small plot range!");
throw new IllegalArgumentException("Error: Small plot range handling not created");
}
/*ASSUME*/ // 10 scale values as a starting assumption.
initialIncrement = (int) (yRange2/10);
// Please excuse this clumsy "math"!
String initialIncrementString = String.valueOf(initialIncrement);
//System.out.println("InitialIncrementString = " + initialIncrementString + " (length = " + initialIncrementString.length() + ")");
// 3) Find even numbers above and below the initial increment.
String leadingDigit = initialIncrementString.substring(0,1);
int leadingNumber = Integer.parseInt(leadingDigit);
int bumpedLeadingNumber = leadingNumber + 1;
String bumpedLeadingDigit = String.valueOf(bumpedLeadingNumber);
String upperIncrementString = bumpedLeadingDigit + zeros.substring(0,initialIncrementString.length()-1);
String lowerIncrementString = leadingDigit + zeros.substring(0,initialIncrementString.length()-1);
upperIncrement = Integer.parseInt(upperIncrementString);
lowerIncrement = Integer.parseInt(lowerIncrementString);
// 4) Pick the upper or lower even increment depending on which is closest.
int distanceToUpper = upperIncrement - initialIncrement;
int distanceToLower = initialIncrement - lowerIncrement;
if (distanceToUpper > distanceToLower)
selectedIncrement = lowerIncrement;
else
selectedIncrement = upperIncrement;
// 5) Determine lowest Y scale value
int numberOfScaleValues = 0;
int lowestScaleValue = 0;
int highestScaleValue;
if (min < 0)
{
for (; lowestScaleValue > min; lowestScaleValue-=selectedIncrement)
numberOfScaleValues++;
}
if (min > 0)
{
for (; lowestScaleValue < min; lowestScaleValue+=selectedIncrement)
numberOfScaleValues++;
numberOfScaleValues--;
lowestScaleValue -= selectedIncrement;
}
// 6) Determine upper Y scale value
numberOfScaleValues = 1;
for (highestScaleValue = lowestScaleValue; highestScaleValue < max; highestScaleValue+=selectedIncrement)
numberOfScaleValues++;
if ((numberOfScaleValues < 5) || (numberOfScaleValues > 20))
{
throw new IllegalArgumentException("Error: Number of scale click marks is too few or too many!");
}
// 7) Determine if Y scale will be extended to include the 0 point.
if (!((lowestScaleValue < 0) && (highestScaleValue > 0))) // Y scale does not include 0.
{ // Should it be extended to include the 0 point?
if ((lowestScaleValue > 0) && (lowestScaleValue/selectedIncrement <= 3))
{
lowestScaleValue = 0;
}
if ((highestScaleValue < 0) && (highestScaleValue/selectedIncrement <= 3))
{
highestScaleValue = 0;
}
}
int scaleValue = lowestScaleValue;
while(scaleValue < highestScaleValue)
{
scaleValue += selectedIncrement;
}
return scaleValue;
}
}