Skip to content

Commit

Permalink
Add weather icons for offline use
Browse files Browse the repository at this point in the history
  • Loading branch information
daawaan4U committed May 29, 2024
1 parent c80284b commit 810544c
Show file tree
Hide file tree
Showing 23 changed files with 94 additions and 45 deletions.
8 changes: 1 addition & 7 deletions src/main/java/edu/project/components/GeoMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.io.IOException;
import java.util.HashSet;

import javax.swing.event.MouseInputListener;
Expand Down Expand Up @@ -63,12 +62,7 @@ public void mouseClicked(MouseEvent e) {
DefaultWaypoint waypoint = new DefaultWaypoint();
waypoints.add(waypoint);
waypointPainter.setWaypoints(waypoints);

try {
waypointPainter.setRenderer(new GeoMapMarker());
} catch (IOException e) {
e.printStackTrace();
}
waypointPainter.setRenderer(new GeoMapMarker());

// Display overlay marker
setOverlayPainter(waypointPainter);
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/edu/project/components/GeoMapMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.jxmapviewer.JXMapViewer;
import org.jxmapviewer.viewer.DefaultWaypoint;
import org.jxmapviewer.viewer.WaypointRenderer;

public class GeoMapMarker implements WaypointRenderer<DefaultWaypoint> {

private BufferedImage markerIconImage;
private BufferedImage markerShadowImage;
private static BufferedImage markerIconImage;
private static BufferedImage markerShadowImage;

public GeoMapMarker() throws IOException {
markerIconImage = ImageIO.read(getClass().getResource("/images/marker-icon.png"));
markerShadowImage = ImageIO.read(getClass().getResource("/images/marker-shadow.png"));
static {
try {
markerIconImage = ImageIO.read(GeoMapMarker.class.getResource("/images/marker-icon.png"));
markerShadowImage = ImageIO.read(GeoMapMarker.class.getResource("/images/marker-shadow.png"));
} catch (Exception exception) {
exception.printStackTrace();
}
}

@Override
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/edu/project/components/TimeForecast.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@
import edu.project.Context;
import edu.project.api.WeatherForecast5Data;

import javax.imageio.ImageIO;
import javax.swing.*;

import com.formdev.flatlaf.FlatClientProperties;

import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

public class TimeForecast extends JPanel {

private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h a")
Expand Down Expand Up @@ -119,16 +128,7 @@ private void addForecastPanels() {
}

private JPanel createIconPanel(String iconId) {
String iconUrl = "https://openweathermap.org/img/wn/" + iconId + "@2x.png";
BufferedImage image = null;

try {
URL url = new URL(iconUrl);
image = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}

BufferedImage image = WeatherIcons.getIcon(iconId);
ImageIcon icon = new ImageIcon(image);

JPanel iconPanel = new JPanel() {
Expand Down
31 changes: 15 additions & 16 deletions src/main/java/edu/project/components/WeatherForecastPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
import edu.project.Context;
import edu.project.api.WeatherForecast5Data;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import com.formdev.flatlaf.FlatClientProperties;

import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
Expand Down Expand Up @@ -120,17 +129,7 @@ private void updateForecast(WeatherForecast5Data forecastData) {
}

private JPanel createiconPanel(String iconId) {
// Base URL for weather icons
String iconUrl = "http://openweathermap.org/img/wn/" + iconId + "@2x.png";
BufferedImage image = null;

try {
URL url = new URL(iconUrl);
image = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}

BufferedImage image = WeatherIcons.getIcon(iconId);
ImageIcon icon = new ImageIcon(image);

JPanel iconPanel = new JPanel() {
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/edu/project/components/WeatherIcons.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package edu.project.components;

import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

/**
* An image provider for OpenWeatherMap's icons
*
* @see https://openweathermap.org/weather-conditions
*/
public class WeatherIcons {

/**
* Key-Value cache for the icons
*/
private static Map<String, BufferedImage> icons = new HashMap<>();

/**
* List of icon IDs
*/
private static String[] iconIds = new String[] {
// Day-time icons
"01d", "02d", "03d", "04d", "09d", "10d", "11d", "13d", "50d",

// Night-time icons
"01n", "02n", "03n", "04n", "09n", "10n", "11n", "13n", "50n"
};

static {
// Load resource image for each icon ID
for (String id : iconIds) {
try {
String path = "/images/weather/" + id + "@2x.png";
URL url = WeatherIcons.class.getResource(path);
BufferedImage image = ImageIO.read(url);
icons.put(id, image);
} catch (Exception exception) {
exception.printStackTrace();
}
}
}

/**
* Gets the corresponding {@code BufferedImage} for the {@code iconId}
*/
public static BufferedImage getIcon(String iconId) {
return icons.get(iconId);
}
}
Binary file added src/main/resources/images/weather/01d@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/01n@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/02d@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/02n@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/03d@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/03n@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/04d@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/04n@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/09d@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/09n@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/10d@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/10n@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/11d@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/11n@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/13d@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/13n@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/50d@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/images/weather/50n@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 810544c

Please sign in to comment.