diff options
| author | bjculkin <bjculkin@BECK-DZ9BJB2.wvu-ad.wvu.edu> | 2018-04-11 15:29:25 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@BECK-DZ9BJB2.wvu-ad.wvu.edu> | 2018-04-11 15:29:25 -0400 |
| commit | dcb6c2159446135a142cad41eec185bb24c45bfb (patch) | |
| tree | 965dcc07fc0256a9a8254d5f968e316a1e928617 /CSMath/src/bezier/PointAdder.java | |
| parent | 8ecf52b6c5821e5c3de8a0f5c9e7ed3e357cb282 (diff) | |
Split #9 into one class/file
Diffstat (limited to 'CSMath/src/bezier/PointAdder.java')
| -rw-r--r-- | CSMath/src/bezier/PointAdder.java | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/CSMath/src/bezier/PointAdder.java b/CSMath/src/bezier/PointAdder.java new file mode 100644 index 0000000..269ec7b --- /dev/null +++ b/CSMath/src/bezier/PointAdder.java @@ -0,0 +1,138 @@ +package bezier;
+
+import java.awt.BorderLayout;
+import java.awt.GridLayout;
+import java.awt.Dialog.ModalityType;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.DefaultListModel;
+import javax.swing.JButton;
+import javax.swing.JDialog;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+
+import bezier.PointAdder.AddListener;
+
+/**
+ * Listener for adding points to a bezier curve.
+ *
+ * @author bjculkin
+ *
+ */
+public class PointAdder implements ActionListener {
+ private final DefaultListModel<TDPoint> pointModel;
+ private final JFrame fram;
+
+ private Bezier curve;
+
+ /**
+ * Create a listener that adds points to a bezier curve.
+ *
+ * @param pointModel
+ * The place to store points.
+ * @param curveHolder
+ * The curve to add to.
+ * @param fram
+ * The frame to use.
+ */
+ public PointAdder(DefaultListModel<TDPoint> pointModel, Holder<Bezier> curveHolder, JFrame fram) {
+ this.pointModel = pointModel;
+ this.curve = curveHolder.getVal();
+ this.fram = fram;
+
+ /*
+ * Change our curve if the current one changes.
+ */
+ curveHolder.addHolderListener((curv) -> {
+ curve = curv;
+ });
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent ev) {
+ JDialog dia = new JDialog(fram);
+ dia.setTitle("Add Control Point");
+ dia.setModalityType(ModalityType.MODELESS);
+ dia.setLayout(new BorderLayout());
+
+ JPanel fields = new JPanel();
+ fields.setLayout(new GridLayout(2, 1));
+
+ LabeledInputPanel xPanel = new LabeledInputPanel("X Coordinate: ", 0.0);
+ LabeledInputPanel yPanel = new LabeledInputPanel("Y Coordinate: ", 0.0);
+
+ fields.add(xPanel);
+ fields.add(yPanel);
+
+ JPanel buttons = new JPanel();
+ buttons.setLayout(new GridLayout(1, 2));
+
+ JButton add = new JButton("Add Control Point");
+
+ AddListener addListener = new AddListener(xPanel, yPanel);
+ add.addActionListener(addListener);
+
+ JButton cancel = new JButton("Cancel");
+ cancel.addActionListener((aev) -> {
+ dia.dispose();
+ });
+
+ buttons.add(add);
+ buttons.add(cancel);
+
+ /*
+ * Change focus to each field on action
+ */
+ xPanel.field.addActionListener((aev) -> {
+ yPanel.field.requestFocusInWindow();
+ });
+ yPanel.field.addActionListener((aev) -> {
+ addListener.actionPerformed(null);
+ });
+
+ dia.add(fields, BorderLayout.CENTER);
+ dia.add(buttons, BorderLayout.PAGE_END);
+
+ dia.pack();
+ dia.setVisible(true);
+ }
+
+ /**
+ * Listener for adding points to a curve.
+ *
+ * @author bjculkin
+ *
+ */
+ class AddListener implements ActionListener {
+ private final LabeledInputPanel xPanel;
+ private final LabeledInputPanel yPanel;
+
+ public AddListener(LabeledInputPanel xPanel, LabeledInputPanel yPanel) {
+ this.xPanel = xPanel;
+ this.yPanel = yPanel;
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent aev) {
+ /*
+ * Add point to curve.
+ */
+ double xVal = (Double) xPanel.field.getValue();
+ double yVal = (Double) yPanel.field.getValue();
+
+ TDPoint punkt = new TDPoint(xVal, yVal);
+
+ pointModel.addElement(punkt);
+ curve.controls.add(punkt);
+
+ /*
+ * Reset field values.
+ */
+ xPanel.field.setValue(0.0);
+ yPanel.field.setValue(0.0);
+
+ xPanel.field.requestFocusInWindow();
+ }
+ }
+}
\ No newline at end of file |
