Repository navigation
Expand file tree
/
Copy pathvolumeShapeGUI.java
More file actions
401 lines (318 loc) · 14.9 KB
/
Copy pathvolumeShapeGUI.java
File metadata and controls
401 lines (318 loc) · 14.9 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.net.URL;
public class volumeShapeGUI {
private static VolumeShape vs = new VolumeShape(" ");
private static double resCuboid, resCube, resCylinder, resCone, resSphere, resHemisphere;
private static JLabel statusBar;
private static JFrame currentFrame;
private static void createAndShowGUI() {
JFrame frame = new JFrame("Volume Shape Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // Maximize the frame
frame.setSize(600, 400);
JToolBar toolBar = new JToolBar();
JButton cuboidButton = createToolbarButton("Cuboid", "cuboid.png", e -> {
updateStatusBar("Selected: Compute Cuboid Surface Area");
closeCurrentFrame();
openCuboidFrame();
});
toolBar.add(cuboidButton);
JButton cubeButton = createToolbarButton("Cube", "cube.png", e -> {
updateStatusBar("Selected: Compute Cube Surface Area");
closeCurrentFrame();
openCubeFrame();
});
toolBar.add(cubeButton);
JButton cylinderButton = createToolbarButton("Cylinder", "cylinder.png", e -> {
updateStatusBar("Selected: Compute Cylinder Surface Area");
closeCurrentFrame();
openCylinderFrame();
});
toolBar.add(cylinderButton);
JButton coneButton = createToolbarButton("Cone", "cone.png", e -> {
updateStatusBar("Selected: Compute Cone Surface Area");
closeCurrentFrame();
openConeFrame();
});
toolBar.add(coneButton);
JButton sphereButton = createToolbarButton("sphere", "sphere.png", e -> {
updateStatusBar("selected: compute surface area");
closeCurrentFrame();
OpenSphereFrame();
});
toolBar.add(sphereButton);
JButton hemisphereButton = createToolbarButton("Hemispere", "hemisphere.png", e -> {
updateStatusBar("selected: compute hemisphere surface area");
closeCurrentFrame();
openHemiSphereFrame();});
toolBar.add(hemisphereButton);
JButton displayButton = createToolbarButton("display", "setting.png", e->{
updateStatusBar("selected: Dislay calculations");
closeCurrentFrame();
displayCalculations();
});
JButton GroupButton = createToolbarButton("NAME OF STUDENTS", "group.png", e -> {
updateStatusBar("selected: list name of student");
closeCurrentFrame();
nameofstudent();
});
toolBar.add(GroupButton);
toolBar.add(displayButton);
JButton exitButton = createToolbarButton("Exit", "exit.png", e ->
System.exit(0));
toolBar.add(exitButton);
statusBar = new JLabel("Ready");
statusBar.setBorder(BorderFactory.createEtchedBorder());
frame.getContentPane().add(toolBar, BorderLayout.NORTH);
frame.getContentPane().add(statusBar, BorderLayout.SOUTH);
frame.setVisible(true);
}
private static void nameofstudent() {
currentFrame = new JFrame("Name of student");
currentFrame.setSize(500, 500);
currentFrame.setLayout(new GridLayout(5, 3));
JLabel sideLabel = new JLabel("1->abeba abebab ");
JLabel sideLabe2 = new JLabel("1->abeba abebab ");
JLabel sideLabe3 = new JLabel("1->abeba abebab ");
JLabel sideLabe4 = new JLabel("1->abeba abebab ");
JLabel sideLabe5 = new JLabel("1->abeba abebab ");
JLabel sideL1 = new JLabel("1->abeba abebab ");
JLabel sideL2 = new JLabel("1->abeba abebab ");
JLabel sideL3 = new JLabel("1->abeba abebab ");
JLabel sideL4 = new JLabel("1->abeba abebab ");
JLabel sideL5 = new JLabel("1->abeba abebab ");
JLabel sl = new JLabel("1->abeba abebab ");
JLabel s2 = new JLabel("1->abeba abebab ");
JLabel s3 = new JLabel("1->abeba abebab ");
JLabel s4 = new JLabel("1->abeba abebab ");
JLabel s5 = new JLabel("1->abeba abebab ");
currentFrame.add(sideLabel);
currentFrame.add(sideL1);
currentFrame.add(sl);
currentFrame.add(sideLabe2);
currentFrame.add(sideL2);
currentFrame.add(s2);
currentFrame.add(sideLabe3);
currentFrame.add(sideL3);
currentFrame.add(s3);
currentFrame.add(sideLabe4);
currentFrame.add(sideL4);
currentFrame.add(s4);
currentFrame.add(sideLabe5);
currentFrame.add(sideL5);
currentFrame.add(s5);
centerFrame(currentFrame);
currentFrame.setVisible(true);
}
private static void OpenSphereFrame() {
currentFrame = new JFrame("Compute Surface Area of Sphere");
currentFrame.setSize(300, 150);
currentFrame.setLayout(new GridLayout(2, 2));
JLabel radiusLabel = new JLabel("Enter value of radius: ");
JTextField radiusField = new JTextField();
JButton computeButton = new JButton("Compute");
computeButton.addActionListener(e -> {
try {
double radius = Double.parseDouble(radiusField.getText());
resSphere = vs.surfaceAreaSphere(radius);
JOptionPane.showMessageDialog(currentFrame, "Surface area of SPHERE: " + resSphere);
updateStatusBar("Computed surface area of Sphere");
closeCurrentFrame();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(currentFrame, "Invalid input! Please enter valid numbers.");
updateStatusBar("Error: Invalid input for Sphere");
}
});
currentFrame.add(radiusLabel);
currentFrame.add(radiusField);
currentFrame.add(computeButton);
centerFrame(currentFrame);
currentFrame.setVisible(true);
}
private static void openCuboidFrame() {
currentFrame = new JFrame("Compute Surface Area of Cuboid");
currentFrame.setSize(300, 200);
currentFrame.setLayout(new GridLayout(4, 2));
JLabel baseLabel = new JLabel("Enter value of base: ");
JTextField baseField = new JTextField();
JLabel heightLabel = new JLabel("Enter value of height: ");
JTextField heightField = new JTextField();
JLabel lengthLabel = new JLabel("Enter value of length: ");
JTextField lengthField = new JTextField();
JButton computeButton = new JButton("Compute");
computeButton.addActionListener(e -> {
try {
double base = Double.parseDouble(baseField.getText());
double height = Double.parseDouble(heightField.getText());
double length = Double.parseDouble(lengthField.getText());
double resCuboid = vs.surfaceAreaCuboid(base, height, length);
JOptionPane.showMessageDialog(currentFrame, "Surface area of CUBOID: " + resCuboid);
updateStatusBar("Computed surface area of Cuboid");
closeCurrentFrame();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(currentFrame, "Invalid input! Please enter valid numbers.");
updateStatusBar("Error: Invalid input for Cuboid");
}
});
currentFrame.add(baseLabel);
currentFrame.add(baseField);
currentFrame.add(heightLabel);
currentFrame.add(heightField);
currentFrame.add(lengthLabel);
currentFrame.add(lengthField);
currentFrame.add(computeButton);
centerFrame(currentFrame);
currentFrame.setVisible(true);
}
private static void openConeFrame() {
currentFrame = new JFrame("Compute Surface Area of Cone");
currentFrame.setSize(300, 200);
currentFrame.setLayout(new GridLayout(3, 2));
JLabel lLabel = new JLabel("Enter value of l: ");
JTextField lField = new JTextField();
JLabel radiusLabel = new JLabel("Enter value of radius: ");
JTextField radiusField = new JTextField();
JButton computeButton = new JButton("Compute");
computeButton.addActionListener(e -> {
try {
double l = Double.parseDouble(lField.getText());
double radius = Double.parseDouble(radiusField.getText());
double resCone = vs.surfaceAreaCone(l, radius);
JOptionPane.showMessageDialog(currentFrame, "Surface area of CONE: " + resCone);
updateStatusBar("Computed surface area of Cone");
closeCurrentFrame();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(currentFrame, "Invalid input! Please enter valid numbers.");
updateStatusBar("Error: Invalid input for Cone");
}
});
currentFrame.add(lLabel);
currentFrame.add(lField);
currentFrame.add(radiusLabel);
currentFrame.add(radiusField);
currentFrame.add(computeButton);
centerFrame(currentFrame);
currentFrame.setVisible(true);
}
private static void openCubeFrame() {
currentFrame = new JFrame("Compute Surface Area of Cube");
currentFrame.setSize(300, 150);
currentFrame.setLayout(new GridLayout(2, 2));
JLabel sideLabel = new JLabel("Enter value of side: ");
JTextField sideField = new JTextField();
JButton computeButton = new JButton("Compute");
computeButton.addActionListener(e -> {
try {
double side = Double.parseDouble(sideField.getText());
resCube = vs.surfaceAreaCube(side);
JOptionPane.showMessageDialog(currentFrame, "Surface area of CUBE: " + resCube);
updateStatusBar("Computed surface area of Cube");
closeCurrentFrame();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(currentFrame, "Invalid input! Please enter valid numbers.");
updateStatusBar("Error: Invalid input for Cube");
}
});
currentFrame.add(sideLabel);
currentFrame.add(sideField);
currentFrame.add(computeButton);
centerFrame(currentFrame);
currentFrame.setVisible(true);
}
private static void openCylinderFrame() {
currentFrame = new JFrame("Compute Surface Area of Cylinder");
currentFrame.setSize(300, 200);
currentFrame.setLayout(new GridLayout(3, 2));
JLabel radiusLabel = new JLabel("Enter value of radius: ");
JTextField radiusField = new JTextField();
JLabel heightLabel = new JLabel("Enter value of height: ");
JTextField heightField = new JTextField();
JButton computeButton = new JButton("Compute");
computeButton.addActionListener(e -> {
try {
double radius = Double.parseDouble(radiusField.getText());
double height = Double.parseDouble(heightField.getText());
resCylinder = vs.surfaceAreaCylinder(radius, height);
JOptionPane.showMessageDialog(currentFrame, "Surface area of CYLINDER: " + resCylinder);
updateStatusBar("Computed surface area of Cylinder");
closeCurrentFrame();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(currentFrame, "Invalid input! Please enter valid numbers.");
updateStatusBar("Error: Invalid input for Cylinder");
}
});
currentFrame.add(radiusLabel);
currentFrame.add(radiusField);
currentFrame.add(heightLabel);
currentFrame.add(heightField);
currentFrame.add(computeButton);
centerFrame(currentFrame);
currentFrame.setVisible(true);
}
private static void openHemiSphereFrame() {
currentFrame = new JFrame("Compute Surface Area of Hemisphere");
currentFrame.setSize(300, 150);
currentFrame.setLayout(new GridLayout(2, 2));
JLabel radiusLabel = new JLabel("Enter value of radius: ");
JTextField radiusField = new JTextField();
JButton computeButton = new JButton("Compute");
computeButton.addActionListener(e -> {
try {
double radius = Double.parseDouble(radiusField.getText());
resHemisphere = vs.surfaceAreaHemisphere(radius);
JOptionPane.showMessageDialog(currentFrame, "Surface area of HEMISPHERE: " + resHemisphere);
updateStatusBar("Computed surface area of Hemisphere");
closeCurrentFrame();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(currentFrame, "Invalid input! Please enter valid numbers.");
updateStatusBar("Error: Invalid input for Hemisphere");
}
});
currentFrame.add(radiusLabel);
currentFrame.add(radiusField);
currentFrame.add(computeButton);
centerFrame(currentFrame);
currentFrame.setVisible(true);
}
private static void displayCalculations() {
String calculations = String.format(
"Surface Area of CUBOID: %f\n" +
"Surface Area of CUBE: %f\n" +
"Surface Area of CYLINDER: %f\n" +
"Surface Area of CONE: %f\n" +
"Surface Area of SPHERE: %f\n" +
"Surface Area of HEMISPHERE: %f\n",
resCuboid, resCube, resCylinder, resCone, resSphere, resHemisphere
);
JOptionPane.showMessageDialog(null, calculations);
updateStatusBar("Displayed all calculations");
}
private static void updateStatusBar(String message) {
statusBar.setText(message);
}
private static JButton createToolbarButton(String text, String iconPath, ActionListener actionListener) {
JButton button = new JButton(text);
URL iconURL = volumeShapeGUI.class.getResource(iconPath);
if (iconURL != null) {
ImageIcon originalIcon = new ImageIcon(iconURL);
Image scaledImage = originalIcon.getImage().getScaledInstance(64, 64, Image.SCALE_SMOOTH);
button.setIcon(new ImageIcon(scaledImage));
}
button.addActionListener(actionListener);
return button;
}
private static void closeCurrentFrame() {
if (currentFrame != null) {
currentFrame.dispose();
}
}
private static void centerFrame(JFrame frame) {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(volumeShapeGUI::createAndShowGUI);
}
}