-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcometer.java
More file actions
198 lines (167 loc) · 9.07 KB
/
Ecometer.java
File metadata and controls
198 lines (167 loc) · 9.07 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
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class Ecometer {
private JFrame frame;
private JPanel loginPanel;
private JPanel inputPanel;
public Ecometer() {
frame = new JFrame("Carbon Emission Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 600);
initLoginPanel();
initInputPanel();
frame.add(loginPanel);
frame.setVisible(true);
}
private void initLoginPanel() {
loginPanel = new JPanel();
loginPanel.setLayout(new BoxLayout(loginPanel, BoxLayout.Y_AXIS));
loginPanel.setBorder(new EmptyBorder(30, 50, 30, 50));
loginPanel.setBackground(new Color(240, 248, 255)); // light blue
JLabel title = new JLabel("Login");
title.setFont(new Font("Arial", Font.BOLD, 24));
title.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel userLabel = new JLabel("Username:");
JTextField userField = new JTextField(15);
JLabel passLabel = new JLabel("Password:");
JPasswordField passField = new JPasswordField(15);
JButton loginButton = new JButton("Login");
loginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
loginButton.setBackground(new Color(135, 206, 250));
loginButton.setForeground(Color.WHITE);
loginButton.setFocusPainted(false);
loginPanel.add(title);
loginPanel.add(Box.createRigidArea(new Dimension(0, 20)));
loginPanel.add(userLabel);
loginPanel.add(userField);
loginPanel.add(Box.createRigidArea(new Dimension(0, 10)));
loginPanel.add(passLabel);
loginPanel.add(passField);
loginPanel.add(Box.createRigidArea(new Dimension(0, 20)));
loginPanel.add(loginButton);
loginButton.addActionListener(e -> {
String username = userField.getText();
String password = new String(passField.getPassword());
if (username.equals("admin") && password.equals("1234")) {
frame.getContentPane().removeAll();
frame.add(inputPanel);
frame.revalidate();
frame.repaint();
} else {
JOptionPane.showMessageDialog(frame, "Invalid login!");
}
});
}
private void initInputPanel() {
inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
inputPanel.setBorder(new EmptyBorder(20, 50, 20, 50));
inputPanel.setBackground(new Color(245, 255, 250));
JLabel title = new JLabel("Monthly Carbon Calculator");
title.setFont(new Font("Arial", Font.BOLD, 22));
title.setAlignmentX(Component.CENTER_ALIGNMENT);
JTextField carField = new JTextField(10);
JTextField electricityField = new JTextField(10);
JTextField meatField = new JTextField(10);
JTextField flightsField = new JTextField(10);
JLabel carLabel = new JLabel("Car travel (km/month):");
JLabel electricityLabel = new JLabel("Electricity usage (kWh/month):");
JLabel meatLabel = new JLabel("Meat meals/month:");
JLabel flightsLabel = new JLabel("Flights/month:");
JButton calcButton = new JButton("Calculate Emission");
calcButton.setAlignmentX(Component.CENTER_ALIGNMENT);
calcButton.setBackground(new Color(60, 179, 113));
calcButton.setForeground(Color.WHITE);
calcButton.setFocusPainted(false);
JLabel resultLabel = new JLabel("");
resultLabel.setFont(new Font("Arial", Font.BOLD, 16));
JLabel adviceLabel = new JLabel("<html></html>");
adviceLabel.setFont(new Font("Arial", Font.ITALIC, 14));
addInputRow(inputPanel, carLabel, carField);
addInputRow(inputPanel, electricityLabel, electricityField);
addInputRow(inputPanel, meatLabel, meatField);
addInputRow(inputPanel, flightsLabel, flightsField);
inputPanel.add(Box.createRigidArea(new Dimension(0, 20)));
inputPanel.add(calcButton);
inputPanel.add(Box.createRigidArea(new Dimension(0, 20)));
inputPanel.add(resultLabel);
inputPanel.add(Box.createRigidArea(new Dimension(0, 10)));
inputPanel.add(adviceLabel);
calcButton.addActionListener(e -> {
try {
double carKm = Double.parseDouble(carField.getText());
double electricity = Double.parseDouble(electricityField.getText());
double meatMeals = Double.parseDouble(meatField.getText());
double flights = Double.parseDouble(flightsField.getText());
double carFactor = 0.21;
double electricityFactor = 0.5;
double meatFactor = 5.0;
double flightFactor = 250.0;
double totalEmission = carKm * carFactor
+ electricity * electricityFactor
+ meatMeals * meatFactor
+ flights * flightFactor;
resultLabel.setText(String.format("Total Emission: %.2f kg CO₂", totalEmission));
String advice;
Color adviceColor;
if (totalEmission < 250) {
advice = "<html>🌱 Excellent! Your monthly footprint is very low.<br>" +
"🌟 You're leading by example. Keep up your amazing eco-conscious lifestyle!<br>" +
"💡 Tip: Share your sustainability practices with friends and family to inspire change.</html>";
adviceColor = new Color(0, 128, 0);
} else if (totalEmission < 400) {
advice = "<html>👍 Good job! Your monthly footprint is low.<br>" +
"✅ You're doing better than most people!<br>" +
"💪 Keep making mindful choices, and consider exploring even greener options.</html>";
adviceColor = new Color(255, 165, 0);
} else if (totalEmission < 600) {
advice = "<html>⚖ Balanced. You’re doing okay, but small improvements can help.<br>" +
"🌿 You're on the right track. Try small tweaks like energy-saving bulbs or reducing short car trips.</html>";
adviceColor = new Color(255, 140, 0);
} else if (totalEmission < 800) {
advice = "<html>⚠ High footprint. Consider making the following changes:<br>" +
"- Reduce unnecessary car trips or carpool<br>" +
"- Unplug electronics when not in use<br>" +
"- Use energy-efficient appliances<br>" +
"- Try reducing red meat consumption<br>" +
"- Combine errands into one trip to save fuel</html>";
adviceColor = new Color(220, 20, 60);
} else if (totalEmission < 1000) {
advice = "<html>🚨 Very high footprint! Significant changes are needed:<br>" +
"- Consider switching to an electric vehicle or using public transport<br>" +
"- Install solar panels or choose renewable electricity providers<br>" +
"- Reduce meat consumption, especially red meat<br>" +
"- Avoid short flights and use trains or buses when possible<br>" +
"- Audit your home for energy leaks and insulate properly</html>";
adviceColor = new Color(178, 34, 34);
} else {
advice = "<html>🔥 Extreme footprint! Urgent action needed!<br>" +
"🌍 You may be among the top carbon emitters globally.<br>" +
"Suggestions:<br>" +
"- Stop or severely reduce flying (use virtual meetings or trains)<br>" +
"- Transition to a fully plant-based diet<br>" +
"- Sell or replace fuel-inefficient vehicles<br>" +
"- Conduct a full home energy audit and retrofit<br>" +
"- Offset your emissions by supporting reforestation or clean energy projects<br>" +
"- Consider lifestyle downsizing if feasible</html>";
adviceColor = new Color(139, 0, 0);
}
adviceLabel.setText(advice);
adviceLabel.setForeground(adviceColor);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Please enter valid numbers for all fields.");
}
});
}
private void addInputRow(JPanel panel, JLabel label, JTextField field) {
label.setAlignmentX(Component.LEFT_ALIGNMENT);
field.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
panel.add(label);
panel.add(field);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
}
public static void main(String[] args) {
new Ecometer();
}
}