Weather GUI App Documentation
The Weather App is a graphical user interface (GUI) application that allows users to search for weather information of a specific city. It provides real-time weather data such as temperature, minimum and maximum temperature, feels like temperature, humidity, and description.
Timeline of Development
Requirement Gathering
1 April to 3 April
Identified the key requirements and functionalities of the Weather GUI App
Design and Architecture
5 April to 10 April
Created the overall design and architectural plan for the Weather GUI App
GUI Development
11 April to 13 April
Implemented the graphical user interface elements, including textfield,search button, result display area.
API Connections
13 April to 14 April
Connect API to the Weather GUI App for fetching data from openweathermap.org
Implementing External Libraries
14 April to 17 April
Add JSON libraries and OpenWeatherMap api
Result Display
17 April to 18 April
Display the result city,temperature,minimum temperature,maximum temperature,feels like,humidity,description.
Error Handling
18 April to 19 April
Added error handling mechanisms to handle issues and other potential errors.
Testing and Refinement
20 April to 1 May
Conducted thorough testing of the program, identified and fixed any bugs or issues, and refined the overall user experience.
Documentation
10 May to 15 May
Prepared the documentation to provide instructions, guidelines, and information about the Weather GUI App program.
Packages Imported:
Packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import java.util.*;
import org.json.*;
User Interface
Features:
City Search: Users can enter the name of a city in the provided text field.
Search Button: Clicking the "Search" button initiates the weather data retrieval for the entered city.
Weather Information Display: The retrieved weather information is displayed in a text area on the GUI.
Temperature Unit: The temperature is displayed in Celsius (°C).
Error Handling: If there is an error during the data retrieval process, an appropriate error message is displayed.
Usage:
Launch the Weather App.
Enter the name of a city in the provided text field.
Click the "Search" button to retrieve the weather information for the entered city.
The weather details, including the city name, temperature, minimum and maximum temperature, feels like temperature, humidity, and description, will be displayed in the text area.
If there is an error during the retrieval process, an error message will be displayed instead.
Note: This application requires an internet connection to retrieve the weather data using the OpenWeatherMap API.
the definition of each function in the WeatherGUI class without the code, along with the references to the JSON library and OpenWeatherApp.
WeatherGUI Class
The WeatherGUI class represents a graphical user interface for a weather application. It provides a user-friendly interface for searching and displaying weather information for a specific city.
public class WeatherGUI extends JFrame implements ActionListener {
private JTextField cityField;
private JButton searchButton;
private JTextArea resultArea;
public WeatherGUI() {
super("Weather App");
// Set up the GUI components
cityField = new JTextField(20);
searchButton = new JButton("Search");
searchButton.addActionListener(this);
resultArea = new JTextArea(10, 30);
resultArea.setEditable(false);
add(new JLabel("Enter city name:"));
add(cityField);
add(searchButton);
add(resultArea);
setLayout(new FlowLayout());
setSize(450, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
Constructor: WeatherGUI()
The constructor initializes the WeatherGUI object. It sets up the GUI components, including the city text field, search button, and result text area. The constructor also sets the layout, size, and visibility of the GUI window.
public WeatherGUI() {
super("Weather App");
// Set up the GUI components
cityField = new JTextField(20);
searchButton = new JButton("Search");
searchButton.addActionListener(this);
resultArea = new JTextArea(10, 30);
resultArea.setEditable(false);
add(new JLabel("Enter city name:"));
add(cityField);
add(searchButton);
add(resultArea);
setLayout(new FlowLayout());
setSize(450, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
Method: actionPerformed(ActionEvent e)
The actionPerformed method is invoked when an action event occurs, such as clicking the search button. It handles the action event and performs the necessary actions based on the event source. In this
case, it retrieves the city name from the text field, constructs the API URL using the OpenWeatherMap API, and makes an HTTP GET request to fetch the weather data for the specified city. It then parses the JSON response using the JSON library to extract the required weather information such as city name, temperature, feels like temperature, minimum and maximum temperature, humidity, and description. Finally, it displays the weather information in the result text area.
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchButton) {
String city = cityField.getText();
String apiKey = "74e29dd6c67675b9c08dc7c4205f2bdf";
String urlString = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey
+ "&units=metric";
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
Scanner responseScanner = new Scanner(connection.getInputStream());
StringBuilder responseBuilder = new StringBuilder();
while (responseScanner.hasNextLine()) {
responseBuilder.append(responseScanner.nextLine());
}
responseScanner.close();
JSONObject jsonObject = new JSONObject(responseBuilder.toString());
String cityName = jsonObject.getString("name");
double temperature = jsonObject.getJSONObject("main").getDouble("temp");
double feelslike = jsonObject.getJSONObject("main").getDouble("feels_like");
double min = jsonObject.getJSONObject("main").getDouble("temp_min");
double max = jsonObject.getJSONObject("main").getDouble("temp_max");
double humidity = jsonObject.getJSONObject("main").getDouble("humidity");
String description = jsonObject.getJSONArray("weather").getJSONObject(0).getString("description");
String result = "City: " + cityName + "\n"
+ "Temperature: " + temperature + "°C\n"
+ "Minimum Temperature: " + min + "°C\n"
+ "Maximum Temperature: " + max + "°C\n"
+ "Feels Like " + feelslike + "°C\n"
+ "Humidity: " + humidity + "%\n"
+ "Description: " + description + "\n";
resultArea.setText(result);
} else {
resultArea.setText("Error: " + responseCode);
}
} catch (IOException | JSONException ex) {
resultArea.setText(ex.toString());
}
}
}
Method: main(String[] args)
The main method is the entry point of the WeatherGUI application. It creates an instance of the WeatherGUI class, which launches the application and displays the GUI.
public static void main(String[] args) {
new WeatherGUI();
}
External Libraries:
JSON Library: The WeatherGUI class uses the JSON library to parse the JSON response received from the OpenWeatherMap API. The JSON library provides methods for creating and manipulating JSON objects and arrays. It allows easy extraction of data from the JSON response.
import org.json.*;
OpenWeatherMap API: The WeatherGUI class utilizes the OpenWeatherMap API to retrieve weather data for the specified city. The API provides access to a wide range of weather information, including temperature, humidity, wind speed, and more. It requires an API key to make requests and provides the weather data in JSON format.
String apiKey = "74e29dd6c67675b9c08dc7c4205f2bdf";
String urlString = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey
+ "&units=metric";
Conclusion
The provided code is a basic weather application implemented in Java using Swing for the graphical user interface. It allows users to enter a city name, retrieve weather information from the OpenWeatherMap API, and display the results in a text area.
Weather GUI App Prototype Developed By:
Akshit Kumar
TCA2109007
Ashmit Kumar Saxena
TCA2109019
Flowchart:
0 Comments