From dcb6c2159446135a142cad41eec185bb24c45bfb Mon Sep 17 00:00:00 2001 From: bjculkin Date: Wed, 11 Apr 2018 15:29:25 -0400 Subject: Split #9 into one class/file --- CSMath/src/bezier/PointRemover.java | 72 +++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 CSMath/src/bezier/PointRemover.java (limited to 'CSMath/src/bezier/PointRemover.java') diff --git a/CSMath/src/bezier/PointRemover.java b/CSMath/src/bezier/PointRemover.java new file mode 100644 index 0000000..98cf8c0 --- /dev/null +++ b/CSMath/src/bezier/PointRemover.java @@ -0,0 +1,72 @@ +package bezier; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.DefaultListModel; +import javax.swing.JFrame; +import javax.swing.JList; +import javax.swing.JOptionPane; + +/** + * Listener to remove points from a bezier curve. + * + * @author bjculkin + * + */ +public class PointRemover implements ActionListener { + private final JFrame fram; + private final JList pointList; + private final DefaultListModel pointModel; + + private Bezier curve; + + /** + * Create a new listener to remove points from a bezier curve. + * + * @param fram + * The frame to use. + * @param pointList + * The list the points are stored in. + * @param pointModel + * The data backing the list. + * @param curveHolder + * The current curve. + */ + public PointRemover(JFrame fram, JList pointList, DefaultListModel pointModel, + Holder curveHolder) { + this.fram = fram; + this.pointList = pointList; + this.pointModel = pointModel; + + /* + * Change our curve if the current one changes. + */ + curve = curveHolder.getVal(); + curveHolder.addHolderListener((val) -> { + curve = val; + }); + } + + @Override + public void actionPerformed(ActionEvent ev) { + int selectedIndex = pointList.getSelectedIndex(); + + /* + * Nothing selected. + */ + if (selectedIndex == -1) + return; + + TDPoint punkt = pointModel.get(selectedIndex); + + String msg = String.format("Do you want to remove the control point (%.2f, %.2f)?", punkt.x, punkt.y); + + int confirmed = JOptionPane.showConfirmDialog(fram, msg, "Remove Control Point?", JOptionPane.YES_NO_OPTION); + + if (confirmed == JOptionPane.YES_OPTION) { + pointModel.remove(selectedIndex); + curve.controls.remove(punkt); + } + } +} \ No newline at end of file -- cgit v1.2.3