-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCanvas.java
More file actions
158 lines (147 loc) · 4.54 KB
/
MyCanvas.java
File metadata and controls
158 lines (147 loc) · 4.54 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.text.DecimalFormat;
import java.lang.StringBuffer;
import java.util.ArrayList;
public class MyCanvas extends JPanel {
JButton graph, clrEquation;
JTextField inputField1, inputField2, inputField3;
String equation, eq1, eq2, eq3;
StringBuffer equationBuffer;
DecimalFormat num = new DecimalFormat( "0.00" );
GetLine g = new GetLine();
Solve s = new Solve();
ArrayList<Double> xList = new ArrayList<Double>();
ArrayList<Double> yList = new ArrayList<Double>();
ArrayList<Double> xList1 = new ArrayList<Double>();
ArrayList<Double> yList1 = new ArrayList<Double>();
ArrayList<Double> xList2 = new ArrayList<Double>();
ArrayList<Double> yList2 = new ArrayList<Double>();
ArrayList<Double> xList3 = new ArrayList<Double>();
ArrayList<Double> yList3 = new ArrayList<Double>();
public MyCanvas()
{
setBorder( BorderFactory.createLoweredBevelBorder() );
setBackground( Color.white );
setLayout( new BorderLayout() );
add( new Axes() );
inputField1 = new JTextField( 10 );
inputField2 = new JTextField( 10 );
inputField3 = new JTextField( 10 );
graph = new JButton( "Graph!" );
graph.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
equation = inputField1.getText();
if ( !equation.equals("") ) {
g.eq = equation;
g.getLine();
eq1 = g.buff.toString();
}
equation = inputField2.getText();
if ( !equation.equals("") ) {
System.out.println(equation);
g.eq = equation;
g.getLine();
eq2 = g.buff.toString();
}
equation = inputField3.getText();
if ( !equation.equals("") ) {
g.eq = equation;
g.getLine();
eq3 = g.buff.toString();
}
graph();
}
}
);
clrEquation = new JButton( "Clear" );
clrEquation.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
inputField1.setText( "" );
inputField2.setText( "" );
inputField3.setText( "" );
reset();
repaint();
}
}
);
}
public void reset() { xList.clear(); yList.clear(); }
public void paintComponent( Graphics g )
{
super.paintComponent( g );
Graphics2D g2 = ( Graphics2D ) g;
int offsetX = getWidth() / 2;
int offsetY = getHeight() / 2;
for (int j = 0; j < xList1.size()-1; j++) {
g2.setColor(Color.red);
g2.draw(new Line2D.Double(20*xList1.get(j)+offsetX, -yList1.get(j)*20+offsetY,
20*xList1.get(j+1)+offsetX, -yList1.get(j+1)*20+offsetY));
}
for (int j = 0; j < xList2.size()-1; j++) {
g2.setColor(Color.blue);
g2.draw(new Line2D.Double(20*xList2.get(j)+offsetX, -yList2.get(j)*20+offsetY,
20*xList2.get(j+1)+offsetX, -yList2.get(j+1)*20+offsetY));
}
for (int j = 0; j < xList3.size()-1; j++) {
g2.setColor(Color.green);
g2.draw(new Line2D.Double(20*xList3.get(j)+offsetX, -yList3.get(j)*20+offsetY,
20*xList3.get(j+1)+offsetX, -yList3.get(j+1)*20+offsetY));
}
}
public void graph()
{
if (!eq1.equals("")) {
equationBuffer = new StringBuffer(eq1);
reset();
getYs();
xList1 = xList;
yList1 = yList;
System.out.println(xList1.size());
}
if (!eq2.equals("")) {
equationBuffer = new StringBuffer(eq2);
reset();
getYs();
xList2 = xList;
yList2 = yList;
}
if (!eq3.equals("")) {
equationBuffer = new StringBuffer(eq3);
reset();
getYs();
xList3 = xList;
yList3 = yList;
}
System.out.println(xList1.size());
repaint();
}
public void getYs()
{
if (equationBuffer.charAt(0) == '-')
equationBuffer.insert(0, "0");
String tempEq = equationBuffer.toString();
for (double x = -100; x < 100; x+=0.05) {
int xOccurences = 0;
equationBuffer = new StringBuffer(tempEq);
for (int n = 0; n < equationBuffer.length(); n++) {
if (equationBuffer.charAt(n) == 'x')
xOccurences++;
}
while (xOccurences > 0) {
int index = equationBuffer.indexOf("x");
equationBuffer.deleteCharAt(equationBuffer.indexOf("x"));
equationBuffer.insert(index, num.format(x));
xOccurences--;
}
s.expBuff = equationBuffer;
s.getNegatives();
xList.add(x);
yList.add(s.runningAnswer);
}
}
}