-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA3.java
More file actions
161 lines (157 loc) · 6.37 KB
/
Copy pathA3.java
File metadata and controls
161 lines (157 loc) · 6.37 KB
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
/*
* ============================================================================================
* A3.java : Extends JFrame and contains a panel where shapes move around on the screen.
* NAME: JANHAV KHANNA
* YOUR UPI: JKHA639
* DATE: 20/05/21
* DESCRIPTION: This is the main class for the program it is required in creating the graphical interface for the program.
* ============================================================================================
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
public class A3 extends JFrame {
private AnimationViewer panel; // panel for bouncing area
JComboBox<ShapeType> shapesComboBox;
JComboBox<PathType> pathComboBox;
JTextField heightText, widthText, textMessageText;
JButton borderButton, fillButton;
/** main method for A3 */
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new A3();
}
});
}
/** constructor to initialise components */
public A3() {
super("Bouncing Application");
panel = new AnimationViewer(true);
add(panel, BorderLayout.CENTER);
panel.setPreferredSize(new Dimension(Shape.DEFAULT_MARGIN_WIDTH, Shape.DEFAULT_MARGIN_HEIGHT));
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);
}
/** Set up the tools panel
* @return toolsPanel the Panel */
public JPanel setUpToolsPanel() {
JPanel toolsPanel = new JPanel();
toolsPanel.setLayout(new BoxLayout(toolsPanel, BoxLayout.X_AXIS));
shapesComboBox = new JComboBox<ShapeType>();
shapesComboBox.setModel(new DefaultComboBoxModel<ShapeType>(ShapeType.values()));
shapesComboBox.setToolTipText("Set shape");
shapesComboBox.addActionListener( new ShapeActionListener()) ;
pathComboBox = new JComboBox<PathType>();
pathComboBox.setModel(new DefaultComboBoxModel<PathType>(PathType.values()));
pathComboBox.setToolTipText("Set path");
pathComboBox.addActionListener(new PathActionListener());
heightText = new JTextField(""+ Shape.DEFAULT_HEIGHT);
heightText.setToolTipText("Set Height");
heightText.addActionListener( new HeightActionListener());
//Set up the width TextField
widthText = new JTextField(""+ Shape.DEFAULT_WIDTH);
widthText.setToolTipText("Set Width");
widthText.addActionListener( new WidthActionListener());
//set up the text message TextField
textMessageText = new JTextField(""+ Shape.DEFAULT_TEXT);
textMessageText.setToolTipText("Set Text");
textMessageText.addActionListener( new TextActionListener());
//Set up the fill colour button
fillButton = new JButton("Fill");
fillButton.setToolTipText("Set Fill Color");
fillButton.setForeground(panel.getCurrentFillColor());
fillButton.addActionListener( new FillActionListener());
//Set up the border colour button
borderButton = new JButton("Border");
borderButton.setToolTipText("Set Border Color");
borderButton.setForeground(panel.getCurrentBorderColor());
borderButton.addActionListener( new BorderActionListener());
toolsPanel.add(new JLabel(" Shape: ", JLabel.RIGHT));
toolsPanel.add(shapesComboBox);
toolsPanel.add(new JLabel(" Path: ", JLabel.RIGHT));
toolsPanel.add(pathComboBox);
toolsPanel.add(new JLabel(" Width: ", JLabel.RIGHT));
toolsPanel.add(widthText);
toolsPanel.add( new JLabel(" Height: ", JLabel.RIGHT));
toolsPanel.add(heightText);
toolsPanel.add( new JLabel(" Text: ", JLabel.RIGHT));
toolsPanel.add(textMessageText);
toolsPanel.add(borderButton);
toolsPanel.add(fillButton);
return toolsPanel;
}
class ShapeActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
panel.setCurrentShapeType(shapesComboBox.getSelectedIndex());
}
}
class PathActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
panel.setCurrentPathType(pathComboBox.getSelectedIndex());
}
}
class WidthActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
int newValue = Integer.parseInt(widthText.getText());
if (newValue > 0 && newValue < Shape.DEFAULT_MARGIN_WIDTH/2 ) // if the value is valid, then change the current height
panel.setCurrentWidth(newValue);
else
widthText.setText(panel.getCurrentWidth()+""); //undo the changes
} catch (Exception ex) {
widthText.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(heightText.getText());
if (newValue > 0 && newValue < Shape.DEFAULT_MARGIN_HEIGHT/2 ) // if the value is valid, then change the current height
panel.setCurrentHeight(newValue);
else
heightText.setText(panel.getCurrentHeight()+""); //undo the changes
} catch (Exception ex) {
heightText.setText(panel.getCurrentHeight()+""); //if the number entered is invalid, reset it
}
}
}
class TextActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
panel.setCurrentText(textMessageText.getText());
}
}
class FillActionListener implements ActionListener {
public void actionPerformed( ActionEvent e) {
Color newColor = JColorChooser.showDialog(panel, "Fill Color", panel.getCurrentFillColor());
if ( newColor != null) {
fillButton.setForeground(newColor);
panel.setCurrentFillColor(newColor);
}
}
}
class BorderActionListener implements ActionListener {
public void actionPerformed( ActionEvent e) {
Color newColor = JColorChooser.showDialog(panel, "Border Color", panel.getCurrentBorderColor());
if ( newColor != null) {
borderButton.setForeground(newColor);
panel.setCurrentBorderColor(newColor);
}
}
}
}