summaryrefslogtreecommitdiff
path: root/CSMath/src/bezier/PointAdder.java
blob: 4b96f50076b618bec0ce477d902fbfc5aa07d403 (plain)
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
package bezier;

import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.GridLayout;
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.geom.Bezier;
import bezier.geom.TDPoint;

/**
 * Listener for adding points to a bezier curve.
 * 
 * @author bjculkin
 *
 */
public class PointAdder implements ActionListener {
	final DefaultListModel<TDPoint> pointModel;
	private final JFrame fram;

	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();
		}
	}
}