-
Notifications
You must be signed in to change notification settings - Fork 0
/
A3.java
220 lines (215 loc) · 8.76 KB
/
A3.java
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
/*
* ============================================================================================
* A1.java : Extends JFrame and contains a panel where shapes move around on the screen.
* UPI: jlin865 Name: John Lin
* ============================================================================================
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.ArrayList;
import javax.swing.tree.*;
public class A3 extends JFrame {
private AnimationViewer panel; // panel for bouncing area
JButton addNodeButton, removeNodeButton, fillButton, borderButton;
JComboBox<ShapeType> shapesComboBox;
JComboBox<PathType> pathComboBox;
JTree tree;
JList<Shape> shapesList;
JTextField widthTextField, heightTextField, labelTextField;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new A3();
}
});
}
public A3() {
super("Bouncing Application");
JPanel mainPanel = setUpMainPanel();
add(mainPanel, BorderLayout.CENTER);
add(setUpToolsPanel(), BorderLayout.NORTH);
addComponentListener(
new ComponentAdapter() { // resize the frame and reset all margins for all shapes
public void componentResized(ComponentEvent componentEvent) {
panel.resetMarginSize();
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1100,600);
setVisible(true);
}
public JPanel setUpMainPanel() {
JPanel mainPanel = new JPanel();
panel = new AnimationViewer();
panel.setPreferredSize(new Dimension(Shape.DEFAULT_PANEL_WIDTH, Shape.DEFAULT_PANEL_HEIGHT));
JPanel modelPanel = setUpModelPanel();
modelPanel.setPreferredSize(new Dimension(Shape.DEFAULT_PANEL_WIDTH, Shape.DEFAULT_PANEL_HEIGHT));
JSplitPane mainSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, modelPanel, panel);
mainSplitPane.setResizeWeight(0.5);
mainSplitPane.setOneTouchExpandable(true);
mainSplitPane.setContinuousLayout(true);
mainPanel.add(mainSplitPane);
return mainPanel;
}
public JPanel setUpModelPanel() {
JPanel treePanel = new JPanel(new BorderLayout());
JPanel listPanel = new JPanel(new BorderLayout());
treePanel.setPreferredSize(new Dimension(Shape.DEFAULT_PANEL_WIDTH, Shape.DEFAULT_PANEL_HEIGHT/2));
listPanel.setPreferredSize(new Dimension(Shape.DEFAULT_PANEL_WIDTH, Shape.DEFAULT_PANEL_HEIGHT/2));
tree = new JTree(panel.model);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true);
tree.addTreeSelectionListener(new TreeNodeSelectionListener());
JScrollPane treeScrollpane = new JScrollPane(tree);
JPanel treeButtonsPanel = new JPanel();
addNodeButton = new JButton("Add Node");
addNodeButton.addActionListener( new AddListener());
removeNodeButton = new JButton("Remove Node");
removeNodeButton.addActionListener( new RemoveListener());
treeButtonsPanel.add(addNodeButton);
treeButtonsPanel.add(removeNodeButton);
treePanel.add(treeButtonsPanel,BorderLayout.NORTH);
treePanel.add(treeScrollpane,BorderLayout.CENTER);
shapesList = new JList<Shape>(panel.model);
listPanel.add(shapesList);
JPanel modelPanel = new JPanel();
JSplitPane modelSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, treePanel, listPanel);
modelSplitPane.setResizeWeight(0.5);
modelSplitPane.setOneTouchExpandable(true);
modelSplitPane.setContinuousLayout(true);
modelPanel.add(modelSplitPane);
return modelPanel;
}
class TreeNodeSelectionListener implements TreeSelectionListener {
public void valueChanged(TreeSelectionEvent e) {
Object node = tree.getLastSelectedPathComponent();
if (node != null && node instanceof NestedShape) {
NestedShape n = (NestedShape)node;
panel.model.reload(n);
}
}
}
class AddListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object node = tree.getLastSelectedPathComponent();
if (node instanceof NestedShape) {
NestedShape n = (NestedShape)node;
panel.model.addShapeNode(n);
}
else if (node == null) {
JOptionPane.showMessageDialog(null, "ERROR: No node selected.", "Message", JOptionPane.INFORMATION_MESSAGE);
}
else if (!(node instanceof NestedShape)) {
JOptionPane.showMessageDialog(null, "ERROR: Must select a NestedShape node.", "Message", JOptionPane.INFORMATION_MESSAGE);
}
}
}
class RemoveListener implements ActionListener {
public void actionPerformed( ActionEvent e) {
Object node = tree.getLastSelectedPathComponent();
if (node == null) {
JOptionPane.showMessageDialog(null, "ERROR: No node selected.", "Message", JOptionPane.INFORMATION_MESSAGE);
}
else if (node != tree.getModel().getRoot()) {
Shape n = (Shape)node;
panel.model.removeNodeFromParent(n);
}
else if (node == tree.getModel().getRoot()) {
JOptionPane.showMessageDialog(null, "ERROR: Must not remove the root.", "Message", JOptionPane.INFORMATION_MESSAGE);
}
}
}
public JPanel setUpToolsPanel() {
shapesComboBox = new JComboBox<ShapeType>(new DefaultComboBoxModel<ShapeType>(ShapeType.values()));
shapesComboBox.addActionListener( new ShapeActionListener()) ;
pathComboBox = new JComboBox<PathType>(new DefaultComboBoxModel<PathType>(PathType.values()));
pathComboBox.addActionListener( new PathActionListener());
labelTextField = new JTextField("0", 5);
widthTextField = new JTextField("" + Shape.DEFAULT_WIDTH, 5);
widthTextField.addActionListener( new WidthActionListener());
heightTextField = new JTextField("" + Shape.DEFAULT_HEIGHT, 5);
heightTextField.addActionListener( new HeightActionListener());
fillButton = new JButton("Fill");
fillButton.setBackground(panel.getCurrentColor());
fillButton.addActionListener( new FillActionListener());
borderButton = new JButton("Border");
borderButton.setBackground(panel.getCurrentBorderColor());
borderButton.addActionListener( new BorderActionListener());
JPanel toolsPanel = new JPanel();
toolsPanel.setLayout(new BoxLayout(toolsPanel, BoxLayout.X_AXIS));
toolsPanel.add(new JLabel(" Shape: ", JLabel.RIGHT));
toolsPanel.add(shapesComboBox);
toolsPanel.add(new JLabel(" Path: ", JLabel.RIGHT));
toolsPanel.add(pathComboBox);
toolsPanel.add(widthTextField);
toolsPanel.add(heightTextField);
toolsPanel.add(fillButton);
toolsPanel.add(borderButton);
toolsPanel.add( new JLabel(" Label: ", JLabel.RIGHT));
toolsPanel.add(labelTextField);
return toolsPanel;
}
class ShapeActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
panel.setCurrentShapeType((ShapeType)shapesComboBox.getSelectedItem());
}
}
class PathActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
panel.setCurrentPathType((PathType)pathComboBox.getSelectedItem());
}
}
class FillActionListener implements ActionListener {
public void actionPerformed( ActionEvent e) {
Color newColor = JColorChooser.showDialog(panel, "Fill Color", panel.getCurrentColor());
if ( newColor != null) {
panel.setCurrentColor(newColor);
fillButton.setBackground(newColor);
}
}
}
class BorderActionListener implements ActionListener {
public void actionPerformed( ActionEvent e) {
Color newColor = JColorChooser.showDialog(panel, "Border Color", panel.getCurrentColor());
if ( newColor != null) {
panel.setCurrentBorderColor(newColor);
borderButton.setBackground(newColor);
}
}
}
class LabelListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String text = labelTextField.getText();
if (text.length()>0)
panel.setCurrentLabel(text);
}
}
class WidthActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
int newValue = Integer.parseInt(widthTextField.getText());
if (newValue > 0) // if the value is valid, then change the current height
panel.setCurrentWidth(newValue);
else
widthTextField.setText(panel.getCurrentWidth()+""); //undo the changes
} catch (Exception ex) {
widthTextField.setText(panel.getCurrentWidth()+""); //if the number entered is invalid, reset it
}
}
}
class HeightActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
int newValue = Integer.parseInt(heightTextField.getText());
if (newValue > 0) // if the value is valid, then change the current height
panel.setCurrentHeight(newValue);
else
heightTextField.setText(panel.getCurrentHeight()+""); //undo the changes
} catch (Exception ex) {
heightTextField.setText(panel.getCurrentHeight()+""); //if the number entered is invalid, reset it
}
}
}
}