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
|
package bezier;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.BevelBorder;
import bezier.geom.Bezier;
import bezier.geom.TDPoint;
/**
* Main class for Bezier graphing.
*
* @author acm
*
*/
public class CulkinAsssignmentNine {
/**
* The current bezier curve.
*/
public final Holder<Bezier> currentCurve;
/**
* The directory of all bezier curves.
*/
public final Map<String, Bezier> curveDirectory;
/**
* Create a new main class.
*/
public CulkinAsssignmentNine() {
curveDirectory = new HashMap<>();
/*
* Set current curve.
*/
currentCurve = new Holder<>();
currentCurve.setVal(new Bezier());
/*
* Install current curve into directory.
*/
curveDirectory.put("Default", currentCurve.getVal());
}
/**
* Main method.
* @param args Unused CLI args.
*/
public static void main(String[] args) {
CulkinAsssignmentNine assign = new CulkinAsssignmentNine();
/*
* Use the native look and feel.
*/
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
JFrame fram = assign.setupFrame();
/*
* Display the GUI.
*/
fram.setSize(640, 480);
fram.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
fram.setVisible(true);
}
/*
* Setup the main JFrame
*/
private JFrame setupFrame() {
JFrame fram = new JFrame("Bezier Grapher");
fram.setLayout(new BorderLayout());
JPanel canvasPanel = new JPanel();
canvasPanel.setLayout(new GridLayout(1, 1));
BezierPanel canvas = new BezierPanel(curveDirectory.values());
canvasPanel.add(canvas);
JPanel points = new JPanel();
points.setLayout(new BorderLayout());
JPanel listPanel = new JPanel();
listPanel.setLayout(new GridLayout(1, 1));
DefaultListModel<TDPoint> pointModel = new DefaultListModel<>();
/*
* Repaint the canvas whenever the list changes.
*/
pointModel.addListDataListener(new CanvasRepainter(canvas));
/*
* Update the list whenever the curve changes.
*/
currentCurve.addHolderListener((val) -> {
pointModel.clear();
for (TDPoint punkt : val.controls) {
pointModel.addElement(punkt);
}
});
JList<TDPoint> pointList = new JList<>(pointModel);
/*
* Use special point renderer.
*/
pointList.setCellRenderer(new TDPointRenderer());
JScrollPane listScroller = new JScrollPane(pointList);
listPanel.add(listScroller);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2));
/*
* Setup action buttons.
*/
JButton addPoint = new JButton("Add Control Points...");
addPoint.addActionListener(new PointAdder(pointModel, currentCurve, fram));
JButton remPoint = new JButton("Remove Control Point");
remPoint.addActionListener(new PointRemover(fram, pointList, pointModel, currentCurve));
buttonPanel.add(addPoint);
buttonPanel.add(remPoint);
points.add(listPanel, BorderLayout.CENTER);
points.add(buttonPanel, BorderLayout.PAGE_END);
JSplitPane main = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, points, canvasPanel);
JPanel menuPanel = new JPanel();
menuPanel.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 1));
JPanel editingPanel = new JPanel();
editingPanel.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 1));
editingPanel.setBorder(new BevelBorder(BevelBorder.RAISED));
JButton editCurveProperties = new JButton("Edit Curve Properties");
editCurveProperties.addActionListener(new CurveEditor(fram, currentCurve, canvas));
editingPanel.add(editCurveProperties);
JPanel destructivePanel = new JPanel();
destructivePanel.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 1));
destructivePanel.setBorder(new BevelBorder(BevelBorder.RAISED));
/*
* Clear all curve points.
*/
JButton clearPoints = new JButton("Clear Points");
clearPoints.addActionListener((ev) -> {
int confirm = JOptionPane.showConfirmDialog(fram, "Are you sure you want to clear the points?",
"Clear Points", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
currentCurve.getVal().controls.clear();
pointModel.clear();
}
});
/*
* Reset to use new curve.
*/
JButton resetCurve = new JButton("Reset Curve");
resetCurve.addActionListener((ev) -> {
int confirm = JOptionPane.showConfirmDialog(fram, "Are you sure you want to reset the curve?",
"Clear Points", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
currentCurve.getVal().controls.clear();
currentCurve.setVal(new Bezier());
curveDirectory.put("Default", currentCurve.getVal());
canvas.repaint();
}
});
destructivePanel.add(clearPoints);
destructivePanel.add(resetCurve);
/*
* This isn't implemented yet.
*/
JPanel curvesPanel = new JPanel();
curvesPanel.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 1));
curvesPanel.setBorder(new BevelBorder(BevelBorder.RAISED));
menuPanel.add(editingPanel);
menuPanel.add(destructivePanel);
// menuPanel.add(curvesPanel);
fram.add(main, BorderLayout.CENTER);
fram.add(menuPanel, BorderLayout.PAGE_START);
return fram;
}
}
|