WeatherApp Assignment
WeatherApp Assignment
Assignment
Overview
This Java application provides real-time weather updates using the OpenWeatherMap API.
It includes a GUI developed with JavaFX, supports unit conversion, error handling, search
history tracking, and dynamic backgrounds based on the time of day.
Features
• API integration with OpenWeatherMap
Main.java
Handles GUI, user interaction, and displays weather data.
WeatherService.java
Fetches and parses weather data from OpenWeatherMap API.
WeatherData.java
Model class to store weather data.
IconUtil.java
Utility to load weather icons from OpenWeatherMap.
BackgroundChanger.java
Changes GUI background based on time of day.
// WeatherApp - Main.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.geometry.*;
import java.util.*;
@Override
unitBox.getItems().addAll("Celsius", "Fahrenheit");
unitBox.setValue("Celsius");
topBox.setAlignment(Pos.CENTER);
topBox.setPadding(new Insets(10));
weatherBox.setAlignment(Pos.CENTER);
root.setTop(topBox);
root.setCenter(weatherBox);
root.setRight(historyList);
searchButton.setOnAction(e -> {
if (!city.isEmpty()) {
try {
updateUI(data);
BackgroundChanger.updateBackground(root);
} else {
});
primaryStage.setScene(scene);
primaryStage.show();
iconView.setImage(IconUtil.getIcon(data.getIconCode()));
alert.setTitle(title);
alert.setContentText(content);
alert.showAndWait();
launch(args);
// WeatherApp - WeatherService.java
import org.json.JSONObject;
import java.io.*;
import java.net.*;
conn.setRequestMethod("GET");
if (conn.getResponseCode() != 200) {
String inputLine;
response.append(inputLine);
in.close();
// WeatherApp - WeatherData.java
public class WeatherData {
this.temperature = temperature;
this.humidity = humidity;
this.windSpeed = windSpeed;
this.condition = condition;
this.iconCode = iconCode;
// WeatherApp - IconUtil.java
import javafx.scene.image.Image;
public class IconUtil {
// WeatherApp - BackgroundChanger.java
import javafx.scene.layout.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.time.LocalTime;
String bg;
else bg = "night.jpg";
new Image(BackgroundChanger.class.getResource("/backgrounds/" +
bg).toExternalForm()),
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
pane.setBackground(new Background(backgroundImage));
Usage Instructions
1. Replace 'YOUR_API_KEY' in WeatherService.java with your actual OpenWeatherMap API
key.
2. Compile the project with Java 11 or higher.
3. Run Main.java to launch the application.
4. Enter a city name and select a unit to view the weather information.
5. Check the right-side panel for your search history.