-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeatherAppGUI.java
163 lines (118 loc) · 5.12 KB
/
WeatherAppGUI.java
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
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(){
super("Weather-App");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(850, 650);
setLocationRelativeTo(null);
setLayout(null);
setResizable(false);
addGUIComponents();
}
private void addGUIComponents(){
//search field
JTextField searchTextField= new JTextField();
searchTextField.setBounds(15,50,750,30);
searchTextField.setFont(new Font("Dialog", Font.PLAIN, 20));
add(searchTextField);
//Weather Image
JLabel weatherConditionImage= new JLabel(loadImage("imgs/cloudy.png"));
weatherConditionImage.setBounds(0, 125, 450, 217);
add(weatherConditionImage);
//temperature text
JLabel temperatureText= new JLabel("Temperature");
temperatureText.setBounds(0, 350,450, 54);
temperatureText.setFont(new Font("Dialog", Font.BOLD, 48));
//center-align
temperatureText.setHorizontalAlignment(SwingConstants.CENTER
);
add(temperatureText);
//Weather Description
JLabel weatherConditionDescription= new JLabel("Weather");
weatherConditionDescription.setBounds(0, 420, 450, 44);
weatherConditionDescription.setFont(new Font("Dialog", Font.BOLD, 18));
weatherConditionDescription.setHorizontalAlignment(SwingConstants.CENTER);
add(weatherConditionDescription);
//humidity image
JLabel humidityImage= new JLabel(loadImage("imgs/humidity.png"));
humidityImage.setBounds(500,170,74,66);
add(humidityImage);
//humidity text
JLabel humidityText= new JLabel("<html><b>Humidity</b>\n100%</html>");
humidityText.setBounds(600,185,95,55);
humidityText.setFont(new Font("Dialog",Font.PLAIN, 18));
add(humidityText);
//windspeed image
JLabel windspeedImage= new JLabel(loadImage("imgs/windspeed.png"));
windspeedImage.setBounds(500,350,74,66);
add(windspeedImage);
//windspeed text
JLabel windspeedText= new JLabel("<html><b>Wind-Speed</b>\n10km/h</html>");
windspeedText.setBounds(600,350,105,55);
windspeedText.setFont(new Font("Dialog", Font.PLAIN, 18));
add(windspeedText);
//search button
JButton searchButton= new JButton(loadImage("imgs/pngtree-vector-search-icon-png-i.jpg"));
//cursor appearance
searchButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
searchButton.setBounds(775, 50, 35, 30);
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String userInput= searchTextField.getText();
if(userInput.replaceAll("\\s", "").length()<=0){
return;
}
//Retrieve weather data
weatherData= WeatherApp.getWeatherData(userInput);
String weatherCondition= (String) weatherData.get("weather_condition");
//Update weather image
switch(weatherCondition){
case "Clear":
weatherConditionImage.setIcon(loadImage("imgs/clear.png"));
break;
case "Cloudy":
weatherConditionImage.setIcon(loadImage("imgs/cloudy.png"));
break;
case "Humidity":
weatherConditionImage.setIcon(loadImage("imgs/humidity.png"));
break;
case "Rain":
weatherConditionImage.setIcon(loadImage("imgs/rain.png"));
break;
case "Snow":
weatherConditionImage.setIcon(loadImage("imgs/snow.png"));
break;
}
double temperature= (double) weatherData.get("temperature");
temperatureText.setText(temperature+" C");
weatherConditionDescription.setText(weatherCondition);
long humidity= (long) weatherData.get("humidity");
humidityText.setText(("<html><b>Humidity</b> "+humidity+"%</html>"));
double windspeed= (double) weatherData.get("windspeed");
windspeedText.setText("<html><b>Wind-Speed</b> "+windspeed+"km/h</html>");
}
});
add(searchButton);
}
// Images for GUI
private ImageIcon loadImage(String resourcePath){
try{
BufferedImage image= ImageIO.read(new File(resourcePath));
return new ImageIcon(image);
}catch(IOException e){
e.printStackTrace();
}
System.out.println("Could not find image path");
return null;
}
}