Skip to content

atomicstructure/weather_app_project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Weather Information App

This is a Java Swing application that provides real-time weather updates and a short-term forecast for a specified location using the OpenWeatherMap API. Features

Real-time Weather: Displays current temperature, humidity, wind speed, and conditions.

Weather Icons: Visual representation of current weather conditions.

Short-Term Forecast: Shows a 24-hour forecast (in 3-hour intervals).

Unit Conversion: Toggle between Celsius/Fahrenheit for temperature and m/s/km/h for wind speed.

Error Handling: Gracefully handles invalid location input or API request failures.

Search History: Keeps track of recent weather searches with timestamps.

Dynamic Backgrounds: Changes the background color of the current weather panel based on the time of day (day/night/sunrise/sunset).

Setup and Running the Application in IntelliJ IDEA Prerequisites

Java Development Kit (JDK): Ensure you have JDK 8 or higher installed. You can download it from Oracle or use an OpenJDK distribution.

IntelliJ IDEA: Download and install IntelliJ IDEA Community Edition (free) or Ultimate Edition.

OpenWeatherMap API Key:

    Go to OpenWeatherMap.

    Sign up for a free account.

    Navigate to your API keys section and generate a new API key.

    Replace "YOUR_OPENWEATHERMAP_API_KEY" in the WeatherApp.java file with your actual API key.

org.json Library:

    This project uses the org.json library for parsing JSON responses from the API.

    Download the latest json.jar file from the Maven Central Repository.

    Place this json.jar file in a lib directory within your project folder (e.g., WeatherAppProject/lib/json.jar).

Weather Icons:

    The application uses local image files for weather icons.

    You will need to place .png image files corresponding to the icon codes used in WeatherDisplayPanel.java and ForecastPanel.java. For example, sun.png, moon.png, rain_day.png, etc. (A default.png is recommended for fallback). You can find free icon sets online or create simple ones.

Project Structure

You should create a project folder named WeatherAppProject and organize the files as follows:

WeatherAppProject/ ├── src/ │ └── main/ │ ├── java/ │ │ ├── WeatherApp.java │ │ ├── WeatherApiClient.java │ │ ├── WeatherDisplayPanel.java │ │ ├── ForecastPanel.java │ │ └── WeatherHistory.java │ └── resources/ │ └── icons/ │ ├── sun.png │ ├── moon.png │ ├── few_clouds_day.png │ ├── ... (other weather icons) │ └── default.png └── lib/ └── json.jar

Steps to create this structure:

Create a new folder named WeatherAppProject.

Inside WeatherAppProject, create a folder named src.

Inside src, create a folder named main.

Inside main, create two folders: java and resources.

Inside resources, create a folder named icons.

Inside WeatherAppProject, create a folder named lib.

Place all your .java files into WeatherAppProject/src/main/java.

Place your weather icon .png files into WeatherAppProject/src/main/resources/icons.

Place the downloaded json.jar file into WeatherAppProject/lib.

Importing and Running in IntelliJ IDEA

Open IntelliJ IDEA.

From the welcome screen, select "Open" or "Open or Import". If you already have a project open, go to File > Open....

Navigate to the WeatherAppProject folder you just created and select it. Click "Open".

IntelliJ IDEA should detect it as a Java project. If prompted, select "Trust Project".

Configure Project SDK (if necessary):

    Go to File > Project Structure... (or press Ctrl+Alt+Shift+S).

    Under Project Settings > Project, ensure a valid Project SDK (JDK 8 or higher) is selected. If not, click "New..." and add your JDK path.

Add json.jar as a Library:

    In the Project Structure window, go to Project Settings > Libraries.

    Click the + button and select Java.

    Navigate to your WeatherAppProject/lib folder and select json.jar. Click OK.

    Ensure the library is added to the correct module (your WeatherAppProject module). Click OK to close the Project Structure.

Mark resources as Resources Root:

    In the IntelliJ IDEA Project tool window (usually on the left), expand src > main.

    Right-click on the resources folder.

    Go to Mark Directory as > Resources Root. This ensures that getClass().getResource() can correctly find your icon files at runtime.

Run the Application:

    In the Project tool window, navigate to src > main > java > WeatherApp.java.

    Right-click on WeatherApp.java and select "Run 'WeatherApp.main()'".

    Alternatively, you can click the green play button next to the main method definition in the editor.

How to Use

Enter Location: Type the name of a city (e.g., "London", "New York", "Lagos") into the "Location" text field.

Get Weather: Click the "Get Weather" button to fetch and display the current weather and forecast.

Change Units: Use the radio buttons at the bottom to switch between Celsius/Fahrenheit for temperature and m/s/km/h for wind speed. The displayed values will update automatically.

View History: Click the "View History" button to see a list of your recent weather searches.

Implementation Details

GUI Framework: Java Swing is used for building the graphical user interface.

API Integration: java.net.HttpURLConnection is used to make HTTP requests to the OpenWeatherMap API. org.json is used for parsing the JSON responses.

Asynchronous Operations: SwingWorker is used to perform API calls on a background thread, preventing the GUI from freezing during network operations.

Dynamic Backgrounds: The WeatherDisplayPanel calculates the local time relative to sunrise and sunset times (obtained from the API) to set a dynamic background color (e.g., blue for day, dark blue for night, orange for dawn/dusk).

Error Handling: try-catch blocks are implemented for network errors and JSON parsing issues. User-friendly JOptionPane messages are displayed for errors.

History Tracking: The WeatherHistory class maintains a List of SearchEntry objects, storing the last 10 successful weather searches.

Icon Mapping: A HashMap in WeatherDisplayPanel and ForecastPanel maps OpenWeatherMap icon codes to local image file names.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages