-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNutritionCalculatorGUI.java
More file actions
157 lines (121 loc) · 5.61 KB
/
Copy pathNutritionCalculatorGUI.java
File metadata and controls
157 lines (121 loc) · 5.61 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NutritionCalculatorGUI {
public void createAndShowWindow() {
// Window creation
JFrame frame = new JFrame("Nutrition Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,450);
frame.setLayout(new GridLayout(8,2, 5, 5));
frame.setResizable(false);
frame.setLocationRelativeTo(null);
// Components adding
frame.add(new JLabel("Weight (kg):"));
JTextField weightTextField = new JTextField();
frame.add(weightTextField);
frame.add(new JLabel("Height (cm):"));
JTextField heightTextField = new JTextField();
frame.add(heightTextField);
frame.add(new JLabel("Age:"));
JTextField ageTextField = new JTextField();
frame.add(ageTextField);
frame.add(new JLabel("Gender:"));
JComboBox<String> genderComboBox = new JComboBox<>(new String[]{"male", "female"});
frame.add(genderComboBox);
frame.add(new JLabel("Activity:"));
JComboBox<String> activityComboBox = new JComboBox<>(new String[]{
"Sedentary (low physical activity)",
"Light exercises (1-2 trainings per week)",
"Regular exercises (3-5 trainings per week)",
"Intensive exercises (6-7 trainings per week)",
"Sportsman (2 trainings per day)"
});
frame.add(activityComboBox);
frame.add(new JLabel("Goal:"));
JComboBox<String> goalComboBox = new JComboBox<>(new String[]{
"Weight Loss",
"Maintenance",
"Muscle Gain"
});
frame.add(goalComboBox);
JButton calculateButton = new JButton("Calculate");
frame.add(calculateButton);
JLabel resultCalculation = new JLabel("Daily calories:");
frame.add(resultCalculation);
JButton saveBtn = new JButton("Save results");
frame.add(saveBtn);
// STYLES
// Colors
frame.getContentPane().setBackground(new Color(240, 240, 240));
calculateButton.setBackground(new Color(70, 130, 180));
calculateButton.setForeground(Color.WHITE);
// Images
ImageIcon icon = new ImageIcon("images/icon.png");
frame.setIconImage(icon.getImage());
// Fonts
Font customFont = new Font("Montserrat", Font.PLAIN, 14);
weightTextField.setFont(customFont);
heightTextField.setFont(customFont);
ageTextField.setFont(customFont);
genderComboBox.setFont(customFont);
activityComboBox.setFont(customFont);
goalComboBox.setFont(customFont);
calculateButton.setFont(customFont);
resultCalculation.setFont(customFont);
saveBtn.setFont(customFont);
for (Component comp : frame.getContentPane().getComponents()) {
if (comp instanceof JLabel) {
((JLabel) comp).setFont(customFont);
}
}
// Borders
frame.getRootPane().setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Inputs
weightTextField.setMargin(new Insets(5, 5, 5, 5));
heightTextField.setMargin(new Insets(5, 5, 5, 5));
ageTextField.setMargin(new Insets(5, 5, 5, 5));
// Buttons
calculateButton.setBackground(new Color(70, 130, 240));
calculateButton.setForeground(Color.WHITE);
calculateButton.setFocusPainted(false);
calculateButton.setBorder(BorderFactory.createEmptyBorder(12, 25, 12, 25));
calculateButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
saveBtn.setBackground(new Color(34, 197, 94));
saveBtn.setForeground(Color.WHITE);
saveBtn.setFocusPainted(false);
saveBtn.setBorder(BorderFactory.createEmptyBorder(8, 15, 8, 15));
saveBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
// Buttons events
calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int weight = Integer.parseInt(weightTextField.getText());
int height = Integer.parseInt(heightTextField.getText());
int age = Integer.parseInt(ageTextField.getText());
String gender = (String) genderComboBox.getSelectedItem();
String activity = (String) activityComboBox.getSelectedItem();
String goal = (String) goalComboBox.getSelectedItem();
double goalMultiplier = CalculationLogic.getGoalMultiplier(goal);
double activityMultiplier = CalculationLogic.getActivityMultiplier(activity);
double bmr = CalculationLogic.calculateBMR(gender, weight, height, age);
double dailyCalories = (bmr * activityMultiplier) + goalMultiplier;
resultCalculation.setText("Daily calories: " + String.format("%.0f", dailyCalories));
} catch (Exception ex) {
resultCalculation.setText("Error occured! Try again.");
}
}
});
saveBtn.addActionListener(e -> {
try (java.io.FileWriter writer = new java.io.FileWriter("result.txt")) {
writer.write(resultCalculation.getText());
resultCalculation.setText("Saved!");
} catch (Exception ex) {
resultCalculation.setText("Save error!");
}
});
// Show window
frame.setVisible(true);
}
}