-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA3.java
158 lines (143 loc) · 5.8 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
/*
* ============================================================================================
* Extends JFrame and contains a panel where shapes move around on the screen.
* Contains the main function.
* NAME: JAE KIM
* ============================================================================================
*/
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 fillButton, addNodeButton, removeNodeButton;
JComboBox<ShapeType> shapesComboBox;
JComboBox<PathType> pathComboBox;
JTree tree;
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);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation((d.width - frameSize.width) / 2, (d.height - frameSize.height) / 2);
pack();
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);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true);
JScrollPane treeScrollpane = new JScrollPane(tree);
JPanel treeButtonsPanel = new JPanel();
addNodeButton = new JButton("Add Node");
addNodeButton.addActionListener( new AddActionListener());
removeNodeButton = new JButton("Remove Node");
removeNodeButton.addActionListener( new RemoveActionListener());
treeButtonsPanel.add(addNodeButton);
treeButtonsPanel.add(removeNodeButton);
treePanel.add(treeButtonsPanel,BorderLayout.NORTH);
treePanel.add(treeScrollpane,BorderLayout.CENTER);
return treePanel;
}
class AddActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object selectedNode = tree.getLastSelectedPathComponent();
if (selectedNode == null) {
JOptionPane.showMessageDialog(null, "ERROR: No node selected.");
}
else {
if (addNodeButton.isEnabled() && selectedNode instanceof NestedShape) {
panel.addShapeNode((NestedShape)selectedNode);
}
else if ( !(selectedNode instanceof NestedShape)) {
JOptionPane.showMessageDialog(null, "ERROR: Must select a NestedShape node.");
}
}
}
}
class RemoveActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object selectedNode = tree.getLastSelectedPathComponent();
if (selectedNode == null) {
JOptionPane.showMessageDialog(null, "ERROR: No node selected.");
}
else {
if (removeNodeButton.isEnabled() && !((Shape)selectedNode == panel.getRoot()) ) {
panel.removeNodeFromParent((Shape)selectedNode);
} else if (removeNodeButton.isEnabled() && ((Shape)selectedNode) == panel.getRoot()) {
JOptionPane.showMessageDialog(null, "ERROR: Must not remove the root.");
}
}
}
}
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());
fillButton = new JButton("Fill");
fillButton.addActionListener( new FillActionListener());
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(fillButton);
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);
}
}
}
}