Skip to content

Commit

Permalink
Merge pull request #17 from daawaan4U/geila/update-weather-info
Browse files Browse the repository at this point in the history
updated WeatherInfo panel to display current weather data
  • Loading branch information
daawaan4U authored May 29, 2024
2 parents 4da400d + 4eeb27d commit 14f92c0
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/java/edu/project/components/WeatherInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import java.awt.Font;

import java.text.SimpleDateFormat;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import edu.project.Context;

import java.awt.BorderLayout;

import java.util.Date;

public class WeatherInfo extends JPanel {
public WeatherInfo(Context context) {
setLayout(new BorderLayout());
Expand All @@ -22,6 +27,8 @@ public WeatherInfo(Context context) {
JLabel dateLabel = new JLabel("Saturday, May 4");
dateLabel.setFont(new Font(dateLabel.getFont().getName(), Font.BOLD, 18));

JLabel timeLabel = new JLabel("8:30");

JLabel skyLabel = new JLabel("Clear Sky");
Font font = skyLabel.getFont();
skyLabel.setFont(new Font(font.getName(), font.getStyle(), 14));
Expand All @@ -31,6 +38,7 @@ public WeatherInfo(Context context) {
feelsLikeLabel.setFont(new Font(feelsFont.getName(), feelsFont.getStyle(), 14));

leftPanel.add(dateLabel);
leftPanel.add(timeLabel);
leftPanel.add(skyLabel);
leftPanel.add(feelsLikeLabel);

Expand All @@ -54,5 +62,36 @@ public WeatherInfo(Context context) {

add(leftPanel, BorderLayout.WEST);
add(rightPanel, BorderLayout.EAST);

context.store.addWeatherCurrentDataListener(data -> {
// leftpanel
SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, MMMM d"); // Example format: "Saturday, May 4"
dateLabel.setText(dateFormat.format(new Date(data.dt * 1000L)));

SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm a"); // Example format: "8:30 PM"
timeLabel.setText(timeFormat.format(new Date()));

feelsLikeLabel.setText(
"<html>Feels like <b>" + String.format("%.2f", data.main.feels_like - 273.15f) + "°C</b></html>");

// right panel
locationLabel.setText(data.name);
tempLabel.setText(String.format("%.2f°C", data.main.temp - 273.15f));
highLowTempLabel.setText(
String.format("H: %.2f°C / L: %.2f°C", data.main.temp_max - 273.15f, data.main.temp_min - 273.15f));

});

context.store.addWeatherForecast5DataListener(data -> {
// Get the weather description
String description = data.list.get(0).weather.get(0).description;

// Capitalize the first letter
String capitalizedDescription = description.substring(0, 1).toUpperCase() + description.substring(1);

// Set the capitalized description to the skyLabel
skyLabel.setText(capitalizedDescription);
});

}
}

0 comments on commit 14f92c0

Please sign in to comment.