-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathWeatherAppGui.java
More file actions
176 lines (132 loc) · 6.23 KB
/
Copy pathWeatherAppGui.java
File metadata and controls
176 lines (132 loc) · 6.23 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
import org.json.simple.JSONObject;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class WeatherAppGui extends JFrame {
private JSONObject weatherData;
public WeatherAppGui(){
// setup our gui and add a title
super("Weather App");
// configure gui to end the program's process once it has been closed
setDefaultCloseOperation(EXIT_ON_CLOSE);
// set the size of our gui (in pixels)
setSize(450, 650);
// load our gui at the center of the screen
setLocationRelativeTo(null);
// make our layout manager null to manually position our components within the gui
setLayout(null);
// prevent any resize of our gui
setResizable(false);
addGuiComponents();
}
private void addGuiComponents(){
// search field
JTextField searchTextField = new JTextField();
// set the location and size of our component
searchTextField.setBounds(15, 15, 351, 45);
// change the font style and size
searchTextField.setFont(new Font("Dialog", Font.PLAIN, 24));
add(searchTextField);
// weather image
JLabel weatherConditionImage = new JLabel(loadImage("src/assets/cloudy.png"));
weatherConditionImage.setBounds(0, 125, 450, 217);
add(weatherConditionImage);
// temperature text
JLabel temperatureText = new JLabel("10 C");
temperatureText.setBounds(0, 350, 450, 54);
temperatureText.setFont(new Font("Dialog", Font.BOLD, 48));
// center the text
temperatureText.setHorizontalAlignment(SwingConstants.CENTER);
add(temperatureText);
// weather condition description
JLabel weatherConditionDesc = new JLabel("Cloudy");
weatherConditionDesc.setBounds(0, 405, 450, 36);
weatherConditionDesc.setFont(new Font("Dialog", Font.PLAIN, 32));
weatherConditionDesc.setHorizontalAlignment(SwingConstants.CENTER);
add(weatherConditionDesc);
// humidity image
JLabel humidityImage = new JLabel(loadImage("src/assets/humidity.png"));
humidityImage.setBounds(15, 500, 74, 66);
add(humidityImage);
// humidity text
JLabel humidityText = new JLabel("<html><b>Humidity</b> 100%</html>");
humidityText.setBounds(90, 500, 85, 55);
humidityText.setFont(new Font("Dialog", Font.PLAIN, 16));
add(humidityText);
// windspeed image
JLabel windspeedImage = new JLabel(loadImage("src/assets/windspeed.png"));
windspeedImage.setBounds(220, 500, 74, 66);
add(windspeedImage);
// windspeed text
JLabel windspeedText = new JLabel("<html><b>Windspeed</b> 15km/h</html>");
windspeedText.setBounds(310, 500, 85, 55);
windspeedText.setFont(new Font("Dialog", Font.PLAIN, 16));
add(windspeedText);
// search button
JButton searchButton = new JButton(loadImage("src/assets/search.png"));
// change the cursor to a hand cursor when hovering over this button
searchButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
searchButton.setBounds(375, 13, 47, 45);
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// get location from user
String userInput = searchTextField.getText();
// validate input - remove whitespace to ensure non-empty text
if(userInput.replaceAll("\\s", "").length() <= 0){
return;
}
// retrieve weather data
weatherData = WeatherApp.getWeatherData(userInput);
// update gui
// update weather image
String weatherCondition = (String) weatherData.get("weather_condition");
// depending on the condition, we will update the weather image that corresponds with the condition
switch(weatherCondition){
case "Clear":
weatherConditionImage.setIcon(loadImage("src/assets/clear.png"));
break;
case "Cloudy":
weatherConditionImage.setIcon(loadImage("src/assets/cloudy.png"));
break;
case "Rain":
weatherConditionImage.setIcon(loadImage("src/assets/rain.png"));
break;
case "Snow":
weatherConditionImage.setIcon(loadImage("src/assets/snow.pngImage"));
break;
}
// update temperature text
double temperature = (double) weatherData.get("temperature");
temperatureText.setText(temperature + " C");
// update weather condition text
weatherConditionDesc.setText(weatherCondition);
// update humidity text
long humidity = (long) weatherData.get("humidity");
humidityText.setText("<html><b>Humidity</b> " + humidity + "%</html>");
// update windspeed text
double windspeed = (double) weatherData.get("windspeed");
windspeedText.setText("<html><b>Windspeed</b> " + windspeed + "km/h</html>");
}
});
add(searchButton);
}
// used to create images in our gui components
private ImageIcon loadImage(String resourcePath){
try{
// read the image file from the path given
BufferedImage image = ImageIO.read(new File(resourcePath));
// returns an image icon so that our component can render it
return new ImageIcon(image);
}catch(IOException e){
e.printStackTrace();
}
System.out.println("Could not find resource");
return null;
}
}