diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..748c380 Binary files /dev/null and b/.DS_Store differ diff --git a/.idea/.gitignore b/.idea/.gitignore index 26d3352..23e48bd 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -1,3 +1,6 @@ # Default ignored files +<<<<<<< HEAD +======= /shelf/ +>>>>>>> remotes/origin/Foad /workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 8147654..0e8a5de 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + - \ No newline at end of file + \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 3cb9f08..708eaa8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,6 @@ //Make sure the path on your machine is correct "java.project.referencedLibraries": [ "lib/**/*.jar", - "c:\\Users\\Foad_Olfat\\Downloads\\json-20201115.jar" + "weather/json-20201115.jar" ] } \ No newline at end of file diff --git a/weather/.DS_Store b/weather/.DS_Store new file mode 100644 index 0000000..b5418f9 Binary files /dev/null and b/weather/.DS_Store differ diff --git a/weather/json-20201115.jar b/weather/json-20201115.jar new file mode 100644 index 0000000..a9eab7a Binary files /dev/null and b/weather/json-20201115.jar differ diff --git a/weather/picture/Zoom_in.png b/weather/picture/Zoom_in.png new file mode 100644 index 0000000..d11d9d5 Binary files /dev/null and b/weather/picture/Zoom_in.png differ diff --git a/weather/picture/Zoom_out.png b/weather/picture/Zoom_out.png new file mode 100644 index 0000000..2f7b828 Binary files /dev/null and b/weather/picture/Zoom_out.png differ diff --git a/weather/picture/cloud.jpg b/weather/picture/cloud.jpg new file mode 100644 index 0000000..23bab05 Binary files /dev/null and b/weather/picture/cloud.jpg differ diff --git a/weather/picture/moon.png b/weather/picture/moon.png new file mode 100644 index 0000000..310c1f7 Binary files /dev/null and b/weather/picture/moon.png differ diff --git a/weather/picture/rain.png b/weather/picture/rain.png new file mode 100644 index 0000000..848025d Binary files /dev/null and b/weather/picture/rain.png differ diff --git a/weather/picture/snow.jpg b/weather/picture/snow.jpg new file mode 100644 index 0000000..66fd63f Binary files /dev/null and b/weather/picture/snow.jpg differ diff --git a/weather/picture/sun.png b/weather/picture/sun.png new file mode 100644 index 0000000..a5c711f Binary files /dev/null and b/weather/picture/sun.png differ diff --git a/weather/picture/thunder.jpg b/weather/picture/thunder.jpg new file mode 100644 index 0000000..e7ff79d Binary files /dev/null and b/weather/picture/thunder.jpg differ diff --git a/weather/src/BufferedImage.java b/weather/src/BufferedImage.java new file mode 100644 index 0000000..0e6703a --- /dev/null +++ b/weather/src/BufferedImage.java @@ -0,0 +1,5 @@ + + +public interface BufferedImage { + +} diff --git a/weather/src/Fetch.java b/weather/src/Fetch.java index 1b2df46..dd348cc 100644 --- a/weather/src/Fetch.java +++ b/weather/src/Fetch.java @@ -2,54 +2,112 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; -import org.json.JSONArray; +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; + import org.json.JSONObject; -import java.net.URI; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.net.URI; +import java.net.URL; +import java.awt.AlphaComposite; +import java.awt.Graphics; public class Fetch { - private final String key = "843b5b1093b07ff0e1d9f681a3a3cb0c"; //API token - private final String google_key = "AIzaSyCVMgcmhkqfQKxbeUnJPGx6V5twYGMq-9k"; //google api token - private String query; //API URL - private String google_query; //google API URL - JSONObject json = new JSONObject(); //the JSON object that contains weather data - JSONObject google_json = new JSONObject(); //the JSON object that contains city co ordinates - - - public Fetch(){ - + private static Fetch instance = null; + private Fetch() { + // Exists only to defeat instantiation. } - //constructor in case user inputs string - public Fetch(String city) { - //this.query = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=imperial&appid=" + key; - this.google_query = "https://maps.googleapis.com/maps/api/geocode/json?address="+ city + "&key=" + google_key; - Geolocator geolocator = new Geolocator(google_query); - //this.query = "https://api.openweathermap.org/data/2.5/weather?lat=" + geolocator.lat + "&lon=" + geolocator.lon + "&units=imperial&appid=" + key; - this.query = "https://api.openweathermap.org/data/2.5/onecall?lat="+ geolocator.lat + "&lon=" + geolocator.lon + "&units=imperial&appid=" + key; - set_json(query); + - } - //constructor in case user inputs lat/lon - //WORK IN PROGRESS - public Fetch(String[] lat_lon){ - //38.7521235,-121.2880059 - this.query = "https://api.openweathermap.org/data/2.5/weather?lat=" + lat_lon[0] + "&lon=" + lat_lon[1] + "&units=imperial&appid=" + key; + private final static String key = "843b5b1093b07ff0e1d9f681a3a3cb0c"; // API token + private final static String google_key = "AIzaSyCVMgcmhkqfQKxbeUnJPGx6V5twYGMq-9k"; // google api token + private static String query; // API URL + private static String google_query; // google API URL + private static String weather_map_query; + static JSONObject json = new JSONObject(); // the JSON object that contains weather data + JSONObject google_json = new JSONObject(); // the JSON object that contains city co ordinates + static ImageIcon map = new ImageIcon(); + static String static_map_query; + static Geolocator geolocator; + static ImageIcon weather_map = new ImageIcon(); + ImageIcon composite = new ImageIcon(); + String view = ""; + + // constructor in case user inputs string + public static Fetch getInstance(String city, String view, int zoom) throws IOException { + if (instance == null) { + instance = new Fetch(); + } + google_query = "https://maps.googleapis.com/maps/api/geocode/json?address=" + city + "&key=" + google_key; + geolocator = new Geolocator(google_query, zoom); + query = "https://api.openweathermap.org/data/2.5/onecall?lat=" + geolocator.lat + "&lon=" + geolocator.lon + + "&units=imperial&appid=" + key; set_json(query); + set_map(zoom, view); + return instance; } - //creates JSON object based on user input - public void set_json(String query){ + // creates JSON object based on user input + public static void set_json(String query) { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder().uri(URI.create(query)).build(); - this.json = new JSONObject(client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(HttpResponse::body) - .join()); + json = new JSONObject( + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body).join()); } + public static BufferedImage get_weather_map_img(BufferedImage image) { + + BufferedImage tmpImg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = (Graphics2D) tmpImg.getGraphics(); + g2d.setComposite(AlphaComposite.SrcOver.derive(1.0f)); + // set the transparency level in range 0.0f - 1.0f + g2d.drawImage(image, 0, 0, null); + image = tmpImg; + return image; + } + public void get_composite() throws IOException { + // Assumed that these are non-null + BufferedImage bigIcon, smallIcon; + URL url = new URL(static_map_query); + URL url2 = new URL(weather_map_query); + bigIcon = ImageIO.read(url); + smallIcon = ImageIO.read(url2); + // Create a new image. + BufferedImage finalIcon = new BufferedImage(bigIcon.getWidth(), bigIcon.getHeight(), + BufferedImage.TYPE_INT_ARGB); // start transparent + // Get the graphics object. This is like the canvas you draw on. + Graphics g = finalIcon.getGraphics(); + // Now we draw the images. + g.drawImage(bigIcon, 0, 0, null); // start at (0, 0) + g.drawImage(smallIcon, 0, 0, null); // start at (10, 10) + // Once we're done drawing on the Graphics object, we should + // call dispose() on it to free up memory. + g.dispose(); + // Finally, convert to ImageIcon and apply. + composite = new ImageIcon(finalIcon); + } + + public static void set_map(int zoom, String view) throws IOException { + static_map_query = "https://maps.googleapis.com/maps/api/staticmap?center=" + geolocator.lat + "," + + geolocator.lon + "&zoom=" + zoom + "&size=256x256&maptype=hybrid&key=" + google_key; + URL url = new URL(static_map_query); + BufferedImage img = ImageIO.read(url); + map = new ImageIcon(img); + + Geolocator.getTileNumber(geolocator.lon, geolocator.lat, zoom); + weather_map_query = "https://tile.openweathermap.org/map/" + view + "/" + zoom + "/" + Geolocator.x + "/" + + Geolocator.y + ".png?appid=" + key; + URL url2 = new URL(weather_map_query); + BufferedImage img2 = ImageIO.read(url2); + weather_map = new ImageIcon(get_weather_map_img(img2)); + } } \ No newline at end of file diff --git a/weather/src/Forecast.java b/weather/src/Forecast.java index b9d51ff..05b7d2b 100644 --- a/weather/src/Forecast.java +++ b/weather/src/Forecast.java @@ -1,17 +1,216 @@ +import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.TimeZone; +import java.sql.Date; +import java.awt.image.BufferedImage; +import java.io.IOException; + +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; +import javax.swing.JLabel; + import org.json.JSONArray; import org.json.JSONObject; -public class Forecast { - private JSONObject json = new JSONObject(); - private JSONArray daily = new JSONArray(); - private double temp_day; +public class Forecast implements Weather_Data{ + private JSONArray daily; + private double temp_day,temp_night,feel_day,feel_night,windspeed,temp_min,temp_max,temp_eve,temp_morn,feel_eve,feel_morn; + private int sunrise_dt,sunset_dt,cloudPercentage,humid,pressure,dt; + private String timezone,sunrise,sunset,day_of_week; + private String icon_url = ""; + ImageIcon icon = new ImageIcon(); + JLabel week_day_label; + JLabel icon_label = new JLabel(""); + JLabel temp_min_label = new JLabel(""); + JLabel temp_max_label = new JLabel(""); + + public Forecast(){ - public Forecast(JSONObject json){ - this.json = json; + } + + public Forecast(JSONObject json, int day) throws IOException{ this.daily = (JSONArray) json.get("daily"); - JSONObject currentObj = (JSONObject) daily.get(1); - this.temp_day = currentObj.getJSONObject("temp").getDouble("day"); - System.out.println(temp_day); + JSONObject currentObj = (JSONObject) daily.get(day); + + this.timezone = (String) json.get("timezone"); + + this.temp_day = currentObj.getJSONObject("temp").getDouble("day"); //get day temp + this.temp_night = currentObj.getJSONObject("temp").getDouble("night"); //get night temp + this.temp_min = currentObj.getJSONObject("temp").getDouble("min"); //get min temp + this.temp_max = currentObj.getJSONObject("temp").getDouble("max"); //getmax temp + this.temp_eve = currentObj.getJSONObject("temp").getDouble("eve"); //get evening temp + this.temp_morn = currentObj.getJSONObject("temp").getDouble("morn"); //get morning temp + + this.feel_day = currentObj.getJSONObject("feels_like").getDouble("night"); //get feels like night temp + this.feel_night = currentObj.getJSONObject("feels_like").getDouble("day"); //get feels likes day temp + this.feel_eve = currentObj.getJSONObject("feels_like").getDouble("eve"); //get feels like in evening temp + this.feel_morn = currentObj.getJSONObject("feels_like").getDouble("morn"); //get feels like in morning temp + + + this.windspeed = currentObj.getDouble("wind_speed"); //get winf speed + this.cloudPercentage = currentObj.getInt("clouds"); //get cloud % + this.sunrise_dt = currentObj.getInt("sunrise"); //get sunrise time + this.sunset_dt = currentObj.getInt("sunset"); //get sunset time + this.humid = currentObj.getInt("humidity"); //get humidty + this.pressure = currentObj.getInt("pressure"); //get pressure + this.dt = currentObj.getInt("dt"); + + JSONArray cloud_condition = (JSONArray) currentObj.get("weather"); //create json array from weather + JSONObject icon_object = (JSONObject) cloud_condition.get(0); + this.icon_url = "http://openweathermap.org/img/w/" + icon_object.get("icon") + ".png"; //get icon url from api + URL url = new URL(icon_url); + BufferedImage img = ImageIO.read(url); + icon = new ImageIcon(img); //icon for current weather + + this.sunrise = unix_timestamp_convertor(sunrise_dt); //set sunrise date/time + this.sunset = unix_timestamp_convertor(sunset_dt); //set sunset date/time + day_of_week_calculator(); + this.week_day_label = new JLabel(this.day_of_week); + + } + + //decode unix timestamp into time format based on search location's timezone + private String unix_timestamp_convertor(int dt){ + //Unix seconds + long unix_seconds = dt; + //convert seconds to milliseconds + Date date = new Date(unix_seconds*1000L); + // format of the date + SimpleDateFormat jdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); + jdf.setTimeZone(TimeZone.getTimeZone(timezone)); + String java_date = jdf.format(date); + return java_date; + + } + + private void day_of_week_calculator(){ + int day_num = (int) (Math.floor(this.dt/86400)+4)%7; + switch(day_num){ + case 0: this.day_of_week = "Monday"; + break; + + case 1: this.day_of_week = "Tuesday"; + break; + + case 2: this.day_of_week = "Wednesday"; + break; + + case 3: this.day_of_week = "Thursday"; + break; + + case 4: this.day_of_week = "Friday"; + break; + + case 5: this.day_of_week = "Saturday"; + break; + + case 6: this.day_of_week = "Sunday"; + break; + + default: this.day_of_week = "INVALID"; + break; + } + } + + public double getTemp_day() { + return temp_day; + } + + public double getTemp_night() { + return temp_night; + } + + public double getFeel_day() { + return feel_day; + } + + public double getFeel_night() { + return feel_night; + } + + public int get_wind_speed() { + return (int) windspeed; + } + + public int get_cloud_percent() { + return cloudPercentage; + } + + public String get_icon_url(){ + return icon_url; + } + //return weather icon + public ImageIcon get_icon(){ + return icon; } + + public double get_temp_min(){ + return temp_min; + } + + public double get_temp_max(){ + return temp_max; + } + + public double get_temp_eve(){ + return temp_eve; + } + + public double get_temp_morn(){ + return temp_morn; + } + + public double get_feel_eve(){ + return feel_eve; + } + + public double get_feel_morn(){ + return feel_morn; + } + + public int get_humidity(){ + return humid; + } + + public int get_pressure(){ + return pressure; + } + + public int get_sunrise_dt(){ + return sunrise_dt; + } + + public int get_sunset_dt(){ + return sunset_dt; + } + + public String get_sunset(){ + return sunset; + } + + public String get_sunrise(){ + return sunrise; + } + + public String get_day_of_week(){ + return day_of_week; + } + + public JLabel get_day_label(){ + return week_day_label; + } + + public JLabel get_icon_label(){ + return icon_label; + } + + public JLabel get_temp_min_label(){ + return temp_min_label; + } + + public JLabel get_temp_max_label(){ + return temp_max_label; + } + -} +} \ No newline at end of file diff --git a/weather/src/Geolocator.java b/weather/src/Geolocator.java index 29f9802..771bc67 100644 --- a/weather/src/Geolocator.java +++ b/weather/src/Geolocator.java @@ -6,29 +6,60 @@ import org.json.JSONArray; import org.json.JSONObject; -public class Geolocator { +public class Geolocator{ double lat; double lon; - private String google_query; - JSONObject google_json = new JSONObject(); //the JSON object that contains city co ordinates + static int x; + static int y; + JSONObject google_json = new JSONObject(); // the JSON object that contains city co ordinates + // ImageIcon map = new ImageIcon(); + + public Geolocator(String google_query, int zoom) { - public Geolocator(String google_query){ - this.google_query = google_query; set_google_json(google_query); set_lat_lon(); + getTileNumber(lon, lat, zoom); } - public void set_lat_lon(){ - JSONArray results = (JSONArray) google_json.get("results"); //create json array from weather - JSONObject currentObj = (JSONObject) results.get(0); //create json obect from json array + public void set_lat_lon() { + JSONArray results = (JSONArray) google_json.get("results"); // create json array from weather + JSONObject currentObj = (JSONObject) results.get(0); // create json obect from json array this.lat = currentObj.getJSONObject("geometry").getJSONObject("location").getDouble("lat"); this.lon = currentObj.getJSONObject("geometry").getJSONObject("location").getDouble("lng"); } - public void set_google_json(String google_query){ + + public void set_google_json(String google_query) { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder().uri(URI.create(google_query)).build(); - this.google_json = new JSONObject(client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(HttpResponse::body) - .join()); + this.google_json = new JSONObject( + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body).join()); } + + public static void getTileNumber(final double lon, final double lat, final int zoom) { + + + int xtile = (int) Math.floor((lon + 180) / 360 * (1 << zoom)); + int ytile = (int) Math + .floor((1 - Math.log(Math.tan(Math.toRadians(lat)) + 1 / Math.cos(Math.toRadians(lat))) / Math.PI) / 2 + * (1 << zoom)); + if (xtile < 0) + xtile = 0; + if (xtile >= (1 << zoom)) + xtile = ((1 << zoom) - 1); + if (ytile < 0) + ytile = 0; + if (ytile >= (1 << zoom)) + ytile = ((1 << zoom) - 1); + x = (int)xtile; + y = (int)ytile; + } + + + + + + } + + + diff --git a/weather/src/Input.java b/weather/src/Input.java deleted file mode 100644 index 5ddc64e..0000000 --- a/weather/src/Input.java +++ /dev/null @@ -1,48 +0,0 @@ -import java.io.IOException; - - -//class for taking user input and formatting it to work with API -public class Input { - private String input; - String city_name; - private String lat; - private String lon; - boolean is_name; //true if input is city name - boolean is_lat_lon; //true if input is lat and long - boolean is_other; //true if input is not categorized - String[] lat_lon = new String[2]; //array of strings containing lat in index 0 abd lon in index 1 - - - public Input(String input) throws IOException { - //types of input, all initialized to false - is_name = false; - is_lat_lon = false; - is_other = false; - - this.input = input; - check_type(); - } - - public void check_type(){ - - //if user input starts with digits, assume it's lat and lon and set values - if(input.substring(0, 1).matches("\\d")){ - this.lat = input.split(",")[0]; - this.lon = input.split(",")[1]; - this.lat_lon[0] = lat; - this.lat_lon[1] = lon; - is_lat_lon = true; //sets type of input to cityname - //if user input starts with alphabet characters, assume it's city name and set values - } else if(input.substring(0, 1).matches("^[a-zA-Z]*$")){ - this.city_name = this.input; - this.city_name = this.city_name.replaceAll(" ","+"); //required for openweather API to ereplace all space with + - is_name = true; //sets type of input to lat_lon - //if user input starts with naything else, set it as city name - } else{ - is_other = true; //sets type of input to other - } - } - - - -} diff --git a/weather/src/Today.java b/weather/src/Today.java index 805fb74..7f725f5 100644 --- a/weather/src/Today.java +++ b/weather/src/Today.java @@ -1,24 +1,32 @@ import org.json.JSONArray; import org.json.JSONObject; - +import javax.imageio.ImageIO; import java.io.IOException; +import java.net.URL; +import java.awt.image.BufferedImage; +import javax.swing.ImageIcon; +import java.util.*; +import java.text.*; //Class for going through json files and returning specific data points -public class Today { - private JSONObject json = new JSONObject(); +public class Today implements Weather_Data{ + JSONObject json = new JSONObject(); private JSONObject current = new JSONObject(); - private int feel; - private int temp; - private int humidity; - private int pressure; - private String cloud; - private int cloud_percent; - private int wind_speed; - private int wind_deg; - + private int feel,temp,humidity,pressure,cloud_percent,wind_speed,wind_deg,dt,time,zoom; + private String cloud,timezone,date,icon_id,view; + ImageIcon map = new ImageIcon(); + private String icon_url = ""; + ImageIcon icon = new ImageIcon(); + ImageIcon weather_map = new ImageIcon(); + ImageIcon composite = new ImageIcon(); + Fetch fetch; + - public Today(String user_input) throws IOException { + public Today(String user_input, String view, int zoom) throws IOException { + user_input=user_input.replaceAll(" ","+"); + this.view = view; + this.zoom = zoom; this.json = set_json(user_input); this.current = this.json.getJSONObject("current"); this.feel = current.getInt("feels_like"); //get feels like temp from json @@ -27,30 +35,71 @@ public Today(String user_input) throws IOException { this.humidity = current.getInt("humidity"); //get humidity from json this.wind_speed = current.getInt("wind_speed"); //get wind speed from json this.wind_deg = current.getInt("wind_deg"); //get wind degree from json - this.cloud_percent = current.getInt("clouds"); + this.cloud_percent = current.getInt("clouds"); //get cloud condition JSONArray cloud_condition = (JSONArray) current.get("weather"); //create json array from weather JSONObject currentObj = (JSONObject) cloud_condition.get(0); //create json obect from json array - this.cloud = (String) currentObj.get("description"); //get cloud description from json + this.cloud = (String) currentObj.get("description"); //get cloud description from json + this.icon_id = (String) currentObj.get("icon"); + this.icon_url = "http://openweathermap.org/img/w/" + icon_id + ".png"; //get icon url from api + this.dt = current.getInt("dt"); //gets unix timestamp + this.timezone = (String) json.get("timezone"); //get search location timezone + unix_timestamp_convertor(); + + URL url = new URL(icon_url); + BufferedImage img = ImageIO.read(url); + icon = new ImageIcon(img); //icon for current weather + + } - //calls Input to get user input //calls Fetch to get json file //returns json to constructor private JSONObject set_json(String user_input) throws IOException{ - Input input = new Input(user_input); - Fetch fetch = new Fetch(); - if(input.is_name){ - fetch = new Fetch(input.city_name); - } - else if(input.is_lat_lon){ - fetch = new Fetch(input.lat_lon); + fetch = Fetch.getInstance(user_input, view, zoom); + this.map = Fetch.map; + this.weather_map = Fetch.weather_map; + this.composite = fetch.composite; + return Fetch.json; + } + + + //decode unix timestamp into time format based on search location's timezone + private void unix_timestamp_convertor(){ + //Unix seconds + long unix_seconds = dt; + //convert seconds to milliseconds + Date date = new Date(unix_seconds*1000L); + // format of the date + SimpleDateFormat jdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); + jdf.setTimeZone(TimeZone.getTimeZone(timezone)); + String java_date = jdf.format(date); + this.date = java_date; + + + + + String dateTime = java_date; + Calendar c = Calendar.getInstance(); + try { + Date d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime); + c.setTime(d); + // System.out.println("Year: " + c.get(Calendar.YEAR)); + // System.out.println("Month: " + c.get(Calendar.MONTH)); + // System.out.println("Day in month: " + c.get(Calendar.DAY_OF_MONTH)); + // System.out.println("Hour: " + c.get(Calendar.HOUR_OF_DAY)); + // System.out.println("Minute: " + c.get(Calendar.MINUTE)); + // System.out.println("Second: " + c.get(Calendar.SECOND)); + this.time = c.get(Calendar.HOUR_OF_DAY); + //System.out.println(time); } - else if(input.is_other){ - fetch = new Fetch(); + catch (Throwable t) { + // test + t.printStackTrace(); } - - return fetch.json; + + + //System.out.println(time); } //return temp public int get_temp(){ @@ -84,5 +133,41 @@ public int get_wind_deg(){ public String get_cloud(){ return cloud; } + //return icon url + public String get_icon_url(){ + return icon_url; + } + //return weather icon + public ImageIcon get_icon(){ + return icon; + } + //return cloud % + public int get_cloud_percent(){ + return cloud_percent; + } + //return date + public String get_date(){ + return date; + } + + public int get_time(){ + return time; + } + + public String get_icon_id(){ + return icon_id; + } + + public ImageIcon get_map(){ + this.map = Fetch.map; + return map; + } + + public ImageIcon get_weather_map(){ + this.weather_map = Fetch.weather_map; + return weather_map; + } + + } diff --git a/weather/src/Today_Roseville.java b/weather/src/Today_Roseville.java new file mode 100644 index 0000000..cf94a59 --- /dev/null +++ b/weather/src/Today_Roseville.java @@ -0,0 +1,15 @@ +import java.io.IOException; + +public class Today_Roseville extends Today { + + public Today_Roseville(String user_input, String view, int zoom) throws IOException { + super(user_input, view, zoom); + if(user_input.equals("roseville")){ + System.out.println("This is an inherited function.\nYou picked Roseville!!!"); + } + else{ + System.out.println("This is an inherited function.\nYou did not pick Roseville."); + } + } + +} diff --git a/weather/src/UI.java b/weather/src/UI.java index 1025255..c67f7d2 100644 --- a/weather/src/UI.java +++ b/weather/src/UI.java @@ -1,26 +1,56 @@ -import javax.swing.*; + +//import javax.lang.model.util.ElementScanner14; +import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.IOException; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.util.Scanner; public class UI { - static JTextField text_field; + static JTextField text_field; static JLabel tempLabel; static JLabel humidLabel; static JLabel cloudLabel; - private static Today today; - private JFrame frame; + private static Today_Roseville today; + private JFrame frame; private JTextField textField; private JRadioButton celciusButton; private JRadioButton fahreneitButton; static JLabel celsius; - private boolean unit; //if true, requests F + static JLabel lblNewLabel = new JLabel(""); + static JLabel map_label = new JLabel(""); + static JLabel weather_map_label = new JLabel(""); + static JLabel composite_map_label = new JLabel(""); + static JLabel icon = new JLabel(""); + static Forecast[] forecast = new Forecast[8]; + static JLabel[] forecast_day_labels = new JLabel[8]; + static JLabel[] forecast_min_labels = new JLabel[8]; + static JLabel[] forecast_max_labels = new JLabel[8]; + static JLabel[] forecast_icon_labels = new JLabel[8]; + String[] map_boxOptions = { "Clouds", "Precipitation", "Pressure", "Temp", "Wind" }; + JComboBox map_comboBox = new JComboBox<>(map_boxOptions); + static int zoom = 8; + static String view = "clouds_new"; + static String img = ""; + Color text_color = Color.WHITE; + JButton zoom_in_button = new JButton(); + JButton zoom_out_button = new JButton(); + BufferedReader br = new BufferedReader(new FileReader("weather/src/favoriteCity.txt")); + String[] boxOptions = {}; + JComboBox comboBox = new JComboBox<>(boxOptions); + JButton saveCity = new JButton("Favorite"); + JButton deleteCity = new JButton("Delete City"); - - public static void frame() throws IOException { - - EventQueue.invokeLater(new Runnable() { + public static void frame() throws IOException { + EventQueue.invokeLater(new Runnable() { public void run() { try { UI window = new UI(); @@ -30,19 +60,23 @@ public void run() { } } }); - } + } - public UI() { + public UI() throws IOException { initialize(); } /** * Initialize the contents of the frame. + * + * @throws IOException */ - private void initialize() { - - /***************WINDOW FRAME************************/ - + private void initialize() throws IOException { + for (int i = 0; i < 8; i++) { + forecast[i] = new Forecast(); + } + comboBox.setEditable(false); + /*************** WINDOW FRAME ************************/ frame = new JFrame(); frame.setTitle("The Weather"); frame.setBackground(Color.BLACK); @@ -51,144 +85,565 @@ private void initialize() { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); frame.setResizable(false); - - - /***************SEARCH BUTTON**********************/ - + set_forecast_days(frame); + + /*************** SEARCH BUTTON **********************/ JButton button = new JButton("Search"); - button.setForeground(new Color(47, 79, 79)); - button.setBackground(new Color(100, 149, 237)); //BUTTON COLOR - button.setBounds(632, 337, 117, 50); //BUTTON PLACEMENT - frame.getContentPane().add(button); //ADDING BUTTTON - - /**************CELSIUS RADIO BUTTON******************/ - + button.setForeground(Color.BLACK); + button.setBackground(new Color(100, 149, 237)); // BUTTON COLOR + button.setBounds(515, 348, 102, 23); // BUTTON PLACEMENT + frame.getContentPane().add(button); // ADDING BUTTTON + + /************** CELSIUS RADIO BUTTON ******************/ celciusButton = new JRadioButton("Celsius"); celciusButton.setForeground(new Color(230, 230, 250)); celciusButton.setOpaque(false); celciusButton.setBackground(new Color(0, 0, 0)); - celciusButton.setBounds(632, 273, 141, 23); //RADIO BUTTON PLACEMENT - frame.getContentPane().add(celciusButton); //ADDS RADIO BUTTON - - /***************FAHRENHEIT RADIO BUTTON***************/ - + celciusButton.setBounds(499, 122, 141, 23); // RADIO BUTTON PLACEMENT + frame.getContentPane().add(celciusButton); // ADDS RADIO BUTTON + celciusButton.setVisible(false); + + /************** MAP ZOOM IN BUTTON ******************/ + ImageIcon zoom_in_icon = new ImageIcon("weather/picture/Zoom_in.png"); + zoom_in_button.setFont(new Font("Sans Serif", Font.BOLD, 10)); + zoom_in_button.setBounds(235, 6, 25, 25); + zoom_in_button.setIcon(resizeIcon(zoom_in_icon, zoom_in_button.getWidth(), zoom_in_button.getHeight())); + frame.getContentPane().add(zoom_in_button); + zoom_in_button.setVisible(false); + + /************** MAP ZOOM OUT BUTTON ******************/ + ImageIcon zoom_out_icon = new ImageIcon("weather/picture/Zoom_out.png"); + zoom_out_button.setFont(new Font("Sans Serif", Font.BOLD, 10)); + zoom_out_button.setBounds(235, 33, 25, 25); + zoom_out_button.setIcon(resizeIcon(zoom_out_icon, zoom_out_button.getWidth(), zoom_out_button.getHeight())); + frame.getContentPane().add(zoom_out_button); + zoom_out_button.setVisible(false); + + /*************** FAHRENHEIT RADIO BUTTON ***************/ fahreneitButton = new JRadioButton("Fahrenheit "); fahreneitButton.setSelected(true); - unit = true; + // unit = true; fahreneitButton.setForeground(new Color(230, 230, 250)); fahreneitButton.setOpaque(false); fahreneitButton.setBackground(new Color(0, 0, 0)); - fahreneitButton.setBounds(632, 308, 141, 23); //RADIO BUTTON PLACEMENT - frame.getContentPane().add(fahreneitButton); //ADDS RADIO BUTTON - - /***************TEXTFIELD AREA**********************/ - textField = new JTextField(); - textField.setHorizontalAlignment(SwingConstants.CENTER); //TEXT PLACEMENT - textField.setFont(new Font("Lucida Grande", Font.PLAIN, 21)); //FONT - textField.setBounds(106, 342, 514, 45); //TEXTFIELD PLACEMENT - frame.getContentPane().add(textField); //ADDS TEXTFIELD + fahreneitButton.setBounds(499, 87, 141, 23); // RADIO BUTTON PLACEMENT + frame.getContentPane().add(fahreneitButton); // ADDS RADIO BUTTON + fahreneitButton.setVisible(false); + + /*************** TEXTFIELD AREA **********************/ + textField = new JTextField(""); + textField.setHorizontalAlignment(SwingConstants.CENTER); // TEXT PLACEMENT + textField.setFont(new Font("Lucida Grande", Font.PLAIN, 21)); // FONT + textField.setBounds(166, 348, 354, 48); // TEXTFIELD PLACEMENT + frame.getContentPane().add(textField); // ADDS TEXTFIELD textField.setColumns(10); - textField.setBackground(UIManager.getColor("Button.highlight")); - - /***************TEMPERATURE**********************/ + textField.setBackground(UIManager.getColor("Button.highlight")); + + /*************** TEMPERATURE **********************/ tempLabel = new JLabel(""); - tempLabel.setFont(new Font("Lucida Grande", Font.BOLD, 38)); - tempLabel.setBounds(400, 50, 141, 61); + tempLabel.setHorizontalAlignment(SwingConstants.CENTER); + tempLabel.setForeground(text_color); + tempLabel.setFont(new Font("Lucida Grande", Font.BOLD, 57)); + tempLabel.setBounds(230, 6, 340, 187); // first one is X axis, second one is Y axis, width is 3rd, height is 4th frame.getContentPane().add(tempLabel); - /***************HUMIDITY**********************/ + /*************** HUMIDITY **********************/ humidLabel = new JLabel(""); humidLabel.setFont(new Font("Lucida Grande", Font.BOLD, 38)); - humidLabel.setBounds(300, 50, 141, 61); - frame.getContentPane().add(humidLabel); - - /***************WEATHER TYPE**********************/ + humidLabel.setBounds(6, -20, 199, 177); + // frame.getContentPane().add(humidLabel); + + /*************** WEATHER TYPE **********************/ cloudLabel = new JLabel(""); - cloudLabel.setFont(new Font("Lucida Grande", Font.BOLD, 15)); - cloudLabel.setBounds(350, 100, 141, 61); + cloudLabel.setHorizontalAlignment(SwingConstants.CENTER); + cloudLabel.setForeground(text_color); + cloudLabel.setFont(new Font("Lucida Grande", Font.BOLD, 10)); + cloudLabel.setBounds(330, 122, 102, 48); frame.getContentPane().add(cloudLabel); - /****************CELSIUS ACTION LISTENER*****************/ + /************** COMBO BOX ***************************/ + comboBox.setBounds(650, 6, 152, 27); + comboBox.setVisible(true); + + try { + String line; + while ((line = br.readLine()) != null) { + comboBox.addItem(line); + } + } finally { + // br.close(); + } + frame.getContentPane().add(comboBox);// allows the saved cities to be acessed faster + + /************** Map COMBO BOX ***************************/ + map_comboBox.setBounds(10, 6, 80, 20); + map_comboBox.setVisible(false); + frame.getContentPane().add(map_comboBox); + + /*** + * WEATHER INFO + */ + JLabel infoLabels = new JLabel(""); + infoLabels.setForeground(text_color); + infoLabels.setHorizontalAlignment(SwingConstants.CENTER); + infoLabels.setBounds(652, 74, 118, 27); + frame.getContentPane().add(infoLabels); + + JLabel lblNewLabel_4_1 = new JLabel(""); + lblNewLabel_4_1.setForeground(text_color); + lblNewLabel_4_1.setHorizontalAlignment(SwingConstants.CENTER); + lblNewLabel_4_1.setBounds(652, 102, 118, 27); + frame.getContentPane().add(lblNewLabel_4_1); + + JLabel lblNewLabel_4_1_1 = new JLabel(""); + lblNewLabel_4_1_1.setForeground(text_color); + lblNewLabel_4_1_1.setHorizontalAlignment(SwingConstants.CENTER); + lblNewLabel_4_1_1.setBounds(652, 133, 118, 27); + frame.getContentPane().add(lblNewLabel_4_1_1); + + JLabel lblNewLabel_4_1_2 = new JLabel(""); + lblNewLabel_4_1_2.setForeground(text_color); + lblNewLabel_4_1_2.setHorizontalAlignment(SwingConstants.CENTER); + lblNewLabel_4_1_2.setBounds(652, 159, 118, 27); + frame.getContentPane().add(lblNewLabel_4_1_2); + + JLabel lblNewLabel_4_1_3 = new JLabel(""); + lblNewLabel_4_1_3.setForeground(text_color); + lblNewLabel_4_1_3.setHorizontalAlignment(SwingConstants.CENTER); + lblNewLabel_4_1_3.setBounds(652, 187, 118, 27); + frame.getContentPane().add(lblNewLabel_4_1_3); + + /*** + * + */ + + /************* FAVORITE CITY BUTTON************************* */ + saveCity.setVisible(true); + saveCity.setBounds(515, 371, 102, 23); + frame.getContentPane().add(saveCity); + + /************* DELETE CITY BUTTON************************* */ + deleteCity.setVisible(true); + deleteCity.setBounds(615, 371, 102, 23); + frame.getContentPane().add(deleteCity); + + /**************** MAP_COMBOBOX ACTION LISTENER *****************/ + map_comboBox.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String s = (String) map_comboBox.getSelectedItem(); + String result = ""; + switch (s) { + case "Clouds": + result = "clouds_new"; + break; + case "Precipitation": + result = "precipitation_new"; + break; + case "Pressure": + result = "pressure_new"; + break; + case "Temp": + result = "temp_new"; + break; + case "Wind": + result = "wind_new"; + break; + default: + result = "clouds_new"; + break; + } + + try { + Fetch.set_map(zoom, result); + } catch (IOException e1) { + e1.printStackTrace(); + } + view = result; + set_weather_map(frame); + set_map(frame); + set_Background_Image(frame); + } + }); + + /**************** ZOOM IN ACTION LISTENER *****************/ + zoom_in_button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (zoom < 10) { + zoom++; + try { + Fetch.set_map(zoom, view); + } catch (IOException e1) { + + e1.printStackTrace(); + } + set_weather_map(frame); + set_map(frame); + set_Background_Image(frame); + } + + } + }); + + /**************** ZOOM OUT ACTION LISTENER *****************/ + zoom_out_button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (zoom > 0) { + zoom--; + try { + Fetch.set_map(zoom, view); + } catch (IOException e1) { + + e1.printStackTrace(); + } + set_weather_map(frame); + set_map(frame); + set_Background_Image(frame); + } + } + }); + + /**************** CELSIUS ACTION LISTENER *****************/ celciusButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { celciusButton.setSelected(true); if (celciusButton.isSelected()) { fahreneitButton.setSelected(false); - unit = false; - tempLabel.setText(""+(int)today.get_CelsiusTemp() + "°C"); - + // unit = false; + tempLabel.setText("" + (int) today.get_CelsiusTemp() + "°C"); + for (int i = 0; i < 7; i++) { + forecast_min_labels[i] + .setText("Min: " + (int) get_CelsiusTemp(forecast[i].get_temp_min()) + "°C"); + forecast_max_labels[i] + .setText("Max: " + (int) get_CelsiusTemp(forecast[i].get_temp_max()) + "°C"); + } } } }); - /****************FAHRENHEIT ACTION LISTENER*****************/ + /**************** FAHRENHEIT ACTION LISTENER *****************/ fahreneitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fahreneitButton.setSelected(true); if (fahreneitButton.isSelected()) { celciusButton.setSelected(false); - unit = true; - tempLabel.setText(""+today.get_temp() + "°F"); - + // unit = true; + tempLabel.setText("" + today.get_temp() + "°F"); + for (int i = 0; i < 7; i++) { + forecast_min_labels[i].setText("Min: " + (int) forecast[i].get_temp_min() + "°F"); + forecast_max_labels[i].setText("Max: " + (int) forecast[i].get_temp_max() + "°F"); + } } } }); - - /****************TEXTFIELD (PRESS ENTER) ACTION LISTENER******************/ + + /**************** TEXTFIELD (PRESS ENTER) ACTION LISTENER ******************/ textField.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { - if(e.getKeyCode() == KeyEvent.VK_ENTER) { - //textField.setText(textField.getText()); + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + String input = textField.getText(); + search_button_action(input, frame); + + } + } + }); + + /**************** BUTTON AND ACTION LISTENER ******************/ + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String input = textField.getText(); + search_button_action(input, frame); + + set_icon(frame); + set_map(frame); + set_Background_Image(frame); + } + }); + + /**************** DELETE BUTTON AND ACTION LISTENER ******************/ + deleteCity.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + BufferedWriter wr = null; + comboBox.setEditable(true); + try { String input = textField.getText(); + if (input == null) { + return; + } else if (input == "") { + return; + } else if (input.length() < 1) { + return; + } else { + String result = fileToString("weather/src/favoriteCity.txt"); + result = result.replaceAll(input, ""); + result = result.replaceAll(System.lineSeparator(), ""); + wr = new BufferedWriter(new FileWriter("weather/src/favoriteCity.txt", false)); + wr.write(result); + wr.newLine(); + wr.close(); + comboBox.removeItem(input); + } + } catch (Exception ioe) { + ioe.printStackTrace(); + } finally { + comboBox.setEditable(false); try { - today = new Today(input); - } catch (IOException e1) { - - e1.printStackTrace(); + if (wr != null) { + wr.close(); + } + } catch (Exception ex) { + ex.printStackTrace(); } - if(unit){ - tempLabel.setText(""+today.get_temp()+ "°F"); + } + } + }); + + /**************** FAVORITE BUTTON AND ACTION LISTENER ******************/ + saveCity.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + BufferedWriter wr = null; + comboBox.setEditable(true); + String result = ""; + try { + result = fileToString("weather/src/favoriteCity.txt"); + } catch (Exception e1) { + // TODO Auto-gene e1.printStackTrace(); + } + try { + String input = textField.getText(); + wr = new BufferedWriter(new FileWriter("weather/src/favoriteCity.txt", true)); + if(input == null) + { + return; + } + else if(result.contains(input)){ + return; + } + else if (input == "") + { + return; + } + else if (input.length() < 1) + { + return; + } + else + { + wr.write(input); + wr.newLine(); + comboBox.addItem(input); + } + } catch(IOException ioe) + { + ioe.printStackTrace(); + } + finally + { + comboBox.setEditable(false); + try { + if(wr != null) + { + wr.close(); + } + } catch(Exception ex) + { + ex.printStackTrace(); } - else{ - tempLabel.setText(""+(int)today.get_CelsiusTemp()+ "°C"); } - humidLabel.setText(""+today.get_humidity()+"%"); - cloudLabel.setText(""+today.get_cloud()); - - } - } }); + initial_set_background_image(frame); + set_forecast_days(frame); + - /****************BUTTON AND ACTION LISTENER******************/ - button.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - String input = textField.getText(); - try { - today = new Today(input); - } catch (IOException e1) { - e1.printStackTrace(); + comboBox.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) + { + JComboBox box = (JComboBox) e.getSource(); + String savedCity = box.getSelectedItem().toString(); + search_button_action(savedCity, frame); + set_icon(frame); + set_map(frame); + set_Background_Image(frame); } - if(unit){ - tempLabel.setText(""+today.get_temp()+ "°F"); + }); + } + + /**************** INITIAL BACKGROUND IMAGE ******************/ + public static void initial_set_background_image(JFrame frame) { + lblNewLabel.setText(""); + Image picture = new ImageIcon("weather/picture/cloud.jpg").getImage(); + lblNewLabel.setIcon(new ImageIcon(picture)); + lblNewLabel.setBounds(0, 0, 800, 405); // IMAGE PLACEMENT + frame.getContentPane().add(lblNewLabel); + } + + /**************** BACKGROUND IMAGE ******************/ + public static void set_Background_Image(JFrame frame) { + lblNewLabel.setText(""); + String cl = today.get_icon_id(); + if (cl.equals("02d") || cl.equals("02n") || cl.equals("03d") || cl.equals("03n") || cl.equals("04d") + || cl.equals("04n")) { + img = "cloud.jpg"; + } else if ((cl.equals("01n") || cl.equals("50n"))) { + img = "moon.png"; + } else if (cl.equals("01d") || cl.equals("50d")) { + img = "sun.png"; + } else if (cl.equals("13d") || cl.equals("13n")) { + img = "snow.jpg"; + } else if (cl.equals("11d") || cl.equals("11n")) { + img = "thunder.jpg"; + } else if (cl.equals("10d") || cl.equals("10n") || cl.equals("09d") || cl.equals("09n")) { + img = "rain.png"; + } + Image picture = new ImageIcon("weather/picture/" + img).getImage(); + lblNewLabel.setIcon(new ImageIcon(picture)); + lblNewLabel.setBounds(0, 0, 800, 405); // IMAGE PLACEMENT + frame.getContentPane().add(lblNewLabel); + + + + } + + /**************** ICONS ***********************/ + public static void set_icon(JFrame frame) { + icon.setIcon(today.get_icon()); + icon.setBounds(420, 129, 110, 35); // IMAGE PLACEMENT 129 + frame.getContentPane().add(icon); + } + + /**************** STATIC MAP ******************/ + public static void set_map(JFrame frame) { + map_label.setHorizontalAlignment(SwingConstants.CENTER); + map_label.setIcon(today.get_map()); + //map_label.setBounds(7, 6, 209, 187); // IMAGE PLACEMENT + map_label.setBounds(7, 6, 256, 210); // IMAGE PLACEMENT + frame.getContentPane().add(map_label); + } + + /**************** WEATHER MAP ******************/ + public static void set_weather_map(JFrame frame) { + weather_map_label.setHorizontalAlignment(SwingConstants.CENTER); + weather_map_label.setIcon(today.get_weather_map()); + //weather_map_label.setBounds(7, 6, 209, 187); // IMAGE PLACEMENT + weather_map_label.setBounds(7, 6, 256, 210); + frame.getContentPane().add(weather_map_label); + } + + /**************** Composte MAP ******************/ + public static void set_composite_map(JFrame frame) { + composite_map_label.setHorizontalAlignment(SwingConstants.CENTER); + composite_map_label.setIcon(today.composite); + composite_map_label.setBounds(7, 6, 209, 187); // IMAGE PLACEMENT + frame.getContentPane().add(composite_map_label); + } + + + + /**************** BUTTON ACTION ******************/ + public void search_button_action(String input, JFrame frame) { + try { + today = new Today_Roseville(input, view, zoom); + get_forecast(); + set_icon(frame); + set_weather_map(frame); + set_map(frame); + map_comboBox.setVisible(true); + //comboBox.setVisible(true); //SET TO VISIBLE UPON "SAVED CITY" FEATURE COMPLETION + //saveCity.setVisible(false); ////SET TO VISIBLE UPON "SAVED CITY" FEATURE COMPLETION + set_Background_Image(frame); + fahreneitButton.setVisible(true); + celciusButton.setVisible(true); + zoom_in_button.setVisible(true); + zoom_out_button.setVisible(true); + } catch (IOException e1) { + + e1.printStackTrace(); + } + if (fahreneitButton.isSelected()) { + tempLabel.setText("" + today.get_temp() + "°F"); + for(int i=0; i<7; i++){ + forecast_day_labels[i].setText(forecast[i].get_day_of_week()); + forecast_min_labels[i].setText("Min: " + (int) forecast[i].get_temp_min() + "°F"); + forecast_max_labels[i].setText("Max: " + (int) forecast[i].get_temp_max() + "°F"); + forecast_icon_labels[i].setIcon(forecast[i].get_icon()); + } + } else { + tempLabel.setText("" + (int) today.get_CelsiusTemp() + "°C"); + for(int i=0; i<7; i++){ + forecast_day_labels[i].setText(forecast[i].get_day_of_week()); + forecast_min_labels[i].setText("Min: " + (int) get_CelsiusTemp(forecast[i].get_temp_min()) + "°C"); + forecast_max_labels[i].setText("Max: " + (int) get_CelsiusTemp(forecast[i].get_temp_max()) + "°C"); + forecast_icon_labels[i].setIcon(forecast[i].get_icon()); } - else{ - tempLabel.setText(""+(int)today.get_CelsiusTemp()+ "°C"); + } + + humidLabel.setText("" + today.get_humidity() + "%"); + cloudLabel.setText("" + today.get_cloud()); + // cloudLabel.setText("" + forecast[7].getTemp_day()); + + + } + + public static void get_forecast() { + for (int i = 0; i < 7; i++) { + try { + forecast[i] = new Forecast(today.json, i); + } catch (IOException e) { + e.printStackTrace(); } - humidLabel.setText(""+today.get_humidity()+"%"); - cloudLabel.setText(""+today.get_cloud()); - } - }); - /****************BACKGROUND IMAGE******************/ - JLabel lblNewLabel = new JLabel(""); - Image picture = new ImageIcon("weather/picture/night.gif").getImage(); //SETS IMAGE - lblNewLabel.setIcon(new ImageIcon(picture)); - lblNewLabel.setBounds(0, 0, 800, 405); //IMAGE PLACEMENT - frame.getContentPane().add(lblNewLabel); + } + } + public static void set_forecast_days(JFrame frame) { + int[] x = {47,144,241,338,435,532,640}; + for(int i=0; i<7; i++){ + forecast_day_labels[i] = new JLabel(""); + forecast_day_labels[i].setBounds(x[i], 205, 85, 128); //Weekday placement + forecast_day_labels[i].setForeground(Color.GREEN); + frame.getContentPane().add(forecast_day_labels[i]); + forecast_icon_labels[i] = new JLabel(""); + // forecast_icon_labels[i].setIcon(forecast[i].get_icon()); //Icon placemnt + forecast_icon_labels[i].setBounds(x[i], 180, 85, 128); + frame.getContentPane().add(forecast_icon_labels[i]); + + forecast_min_labels[i] = new JLabel(""); + forecast_min_labels[i].setBounds(x[i], 230, 85, 128); //Min temp placement + forecast_min_labels[i].setForeground(Color.YELLOW); + frame.getContentPane().add(forecast_min_labels[i]); + + forecast_max_labels[i] = new JLabel(""); + forecast_max_labels[i].setBounds(x[i], 250, 85, 128); //Max temp placement + forecast_max_labels[i].setForeground(Color.RED); + frame.getContentPane().add(forecast_max_labels[i]); + + + } + + } + + public double get_CelsiusTemp(double tem){ + return (tem-32)*0.5556; + } + + private static Icon resizeIcon(ImageIcon icon, int resizedWidth, int resizedHeight) { + Image img = icon.getImage(); + Image resizedImage = img.getScaledInstance(resizedWidth, resizedHeight, java.awt.Image.SCALE_SMOOTH); + return new ImageIcon(resizedImage); + } + // To delete from file + private String fileToString(String filePath) throws Exception{ + String input = null; + Scanner sc = new Scanner(new File(filePath)); + StringBuffer sb = new StringBuffer(); + while (sc.hasNextLine()) { + input = sc.nextLine(); + sb.append(input); + } + return sb.toString(); + } } \ No newline at end of file diff --git a/weather/src/Weather_Data.java b/weather/src/Weather_Data.java new file mode 100644 index 0000000..d8aed3e --- /dev/null +++ b/weather/src/Weather_Data.java @@ -0,0 +1,11 @@ +import javax.swing.ImageIcon; + +interface Weather_Data { + public int get_wind_speed(); + public String get_icon_url(); + public ImageIcon get_icon(); + public int get_humidity(); + public int get_pressure(); + public int get_cloud_percent(); + +} diff --git a/weather/src/favoriteCity.txt b/weather/src/favoriteCity.txt new file mode 100644 index 0000000..daf901f --- /dev/null +++ b/weather/src/favoriteCity.txt @@ -0,0 +1,3 @@ + +roseville +london