Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated WeatherInfo panel to display current weather data #17

Merged
merged 3 commits into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});

}
}
Loading