Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package projectresurrection; import java.awt.Image; import javax.swing.*; import java.util.*; import org.json.*; import java.io.*; import java.net.URL; import javax.swing.border.TitledBorder; /** * * @author Onryo */ public class Weather implements Runnable { private Thread t; private final String THREAD_NAME = "weatherThread"; private static final String API_KEY = "3e1e10372d738dfb8a85086f0b83c"; public static final int BRAIN = 0; public static final int HOME = 1; public static final int TAB = 2; public static final int UPDATE = 0; public static final int NO_UPDATE = 1; private String location; private String base = "https://api.worldweatheronline.com/free/v2/weather.ashx/?format=json&tp=24"; private static List<String> currentWeather = new ArrayList(); private static List<List> forecast = new ArrayList(); private JPanel tab; private int mode; private int update; private String timeZone = (String) Eve.getPref("time zone"); public Weather(int m, int u, String city) { this(new JPanel(), m, u, city); } public Weather(JPanel pan, int m, int u, String city) { try { tab = pan; mode = m; update = u; location = city; update(); } catch (Exception e) { } t = new Thread(this, THREAD_NAME); t.start(); } public void changeLocation(String city) { try { location = city; update(); updateLbl(); } catch (Exception e) { e.printStackTrace(System.out); } } public static List getCurrent() { return currentWeather; } public static List getForecast() { return forecast; } private static String readUrl(String urlString) throws Exception { BufferedReader reader = null; try { URL url = new URL(urlString); reader = new BufferedReader(new InputStreamReader(url.openStream())); StringBuffer buffer = new StringBuffer(); int read; char[] chars = new char[1024]; while ((read = reader.read(chars)) != -1) { buffer.append(chars, 0, read); } return buffer.toString(); } finally { if (reader != null) { reader.close(); } } } private void update() throws Exception { forecast = new ArrayList(); currentWeather = new ArrayList(); String url = base + "&q=" + location.replace(' ', '+') + "&key=" + API_KEY; try { JSONObject json = new JSONObject(readUrl(url)); currentWeather.add(json.getJSONObject("data").getJSONArray("current_condition").getJSONObject(0) .getString("temp_C")); currentWeather.add(json.getJSONObject("data").getJSONArray("current_condition").getJSONObject(0) .getString("temp_F")); currentWeather.add(json.getJSONObject("data").getJSONArray("current_condition").getJSONObject(0) .getJSONArray("weatherDesc").getJSONObject(0).getString("value")); currentWeather.add(json.getJSONObject("data").getJSONArray("current_condition").getJSONObject(0) .getString("windspeedMiles")); currentWeather.add(json.getJSONObject("data").getJSONArray("current_condition").getJSONObject(0) .getString("windspeedKmph")); currentWeather.add(json.getJSONObject("data").getJSONArray("current_condition").getJSONObject(0) .getString("winddir16Point")); for (int i = 0; i < json.getJSONObject("data").getJSONArray("weather").length(); i++) { List<String> sub = new ArrayList(); sub.add(json.getJSONObject("data").getJSONArray("weather").getJSONObject(i).getString("maxtempC")); sub.add(json.getJSONObject("data").getJSONArray("weather").getJSONObject(i).getString("maxtempF")); sub.add(json.getJSONObject("data").getJSONArray("weather").getJSONObject(i).getString("mintempC")); sub.add(json.getJSONObject("data").getJSONArray("weather").getJSONObject(i).getString("mintempF")); sub.add(json.getJSONObject("data").getJSONArray("weather").getJSONObject(i).getJSONArray("hourly") .getJSONObject(0).getJSONArray("weatherDesc").getJSONObject(0).getString("value")); sub.add(json.getJSONObject("data").getJSONArray("weather").getJSONObject(i).getJSONArray("hourly") .getJSONObject(0).getString("chanceofrain")); sub.add(json.getJSONObject("data").getJSONArray("weather").getJSONObject(i).getJSONArray("hourly") .getJSONObject(0).getString("windspeedMiles")); sub.add(json.getJSONObject("data").getJSONArray("weather").getJSONObject(i).getJSONArray("hourly") .getJSONObject(0).getString("windspeedKmph")); sub.add(json.getJSONObject("data").getJSONArray("weather").getJSONObject(i).getJSONArray("hourly") .getJSONObject(0).getString("winddir16Point")); forecast.add(sub); } } catch (JSONException e) { e.printStackTrace(); } } private ImageIcon getImage(String desc) { String iconPath = "F:\\Java\\ProjectResurrection\\icons\\weather\\"; if (Eve.clock.getCurrent().getHourOfDay() > 19 || Eve.clock.getCurrent().getHourOfDay() < 6) { iconPath += "night\\"; } switch (desc) { case "Overcast": iconPath += "cloudy.png"; break; case "Clear": iconPath += "clear.png"; break; case "Partly Cloudy": iconPath += "partly cloudy.png"; break; case "Moderate or heavy rain shower": iconPath += "storm.png"; break; case "Sunny": iconPath += "clear.png"; break; case "Mist": iconPath += "fog.png"; break; case "Ligt rain shower": iconPath += "shower.png"; break; case "Thundery outbreaks in nearby": iconPath += "storm.png"; break; case "Patchy light rain in area with thunder": iconPath += "storm.png"; break; case "Light rain shower": iconPath += "shower.png"; break; case "Light drizzle": iconPath += "shower.png"; break; case "Patchy light drizzle": iconPath += "shower.png"; break; case "Patchy rain nearby": iconPath += "shower.png"; break; case "Heavy Rain With Thunderstorm, Mist": iconPath += "storm.png"; break; case "Thunderstorm In Vicinity": iconPath += "storm.png"; break; case "Light Rain": iconPath += "shower.png"; default: iconPath += "clear.png"; break; } Image img = new ImageIcon(iconPath).getImage(); Image newimg = img.getScaledInstance(75, 75, java.awt.Image.SCALE_SMOOTH); return new ImageIcon(newimg); } private void updateLbl() { if (mode == BRAIN) { } else if (mode == HOME) { ((JLabel) ((JPanel) ((JPanel) ((JViewport) ((JScrollPane) tab.getComponent(0)).getComponent(0)) .getComponent(0)).getComponent(1)).getComponent(0)).setIcon(getImage(currentWeather.get(2))); ((JLabel) ((JPanel) ((JPanel) ((JViewport) ((JScrollPane) tab.getComponent(0)).getComponent(0)) .getComponent(0)).getComponent(1)).getComponent(0)) .setText( "<html>Temp: " + currentWeather.get(1) + "F/" + currentWeather.get(0) + "C<br />Wind: " + ((currentWeather.get(3).equals("0")) ? "None" : currentWeather.get(3) + "mph/" + currentWeather.get(4) + "Kmph " + currentWeather.get(5)) + "<br />" + currentWeather.get(2)); } else if (mode == TAB) { int dayNum; String day; for (int i = 0; i < 5; i++) { dayNum = (Eve.clock.getCurrent().getDayOfWeek() + i) % 7; switch (dayNum) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; break; default: day = "Weather"; break; } if (dayNum == (Eve.clock.getCurrent().getDayOfWeek() % 7)) { day = "Today"; } else if (dayNum == (Eve.clock.getCurrent().getDayOfWeek() + 1) % 7) { day = "Tomorrow"; } ((JPanel) ((JPanel) ((JViewport) ((JScrollPane) tab.getComponent(0)).getComponent(0)) .getComponent(0)).getComponent(i)).setBorder( new TitledBorder(null, day, TitledBorder.LEADING, TitledBorder.TOP, null, null)); ((JLabel) ((JPanel) ((JPanel) ((JViewport) ((JScrollPane) tab.getComponent(0)).getComponent(0)) .getComponent(0)).getComponent(i)).getComponent(0)) .setIcon(getImage((String) forecast.get(i).get(4))); ((JLabel) ((JPanel) ((JPanel) ((JViewport) ((JScrollPane) tab.getComponent(0)).getComponent(0)) .getComponent(0)).getComponent(i)).getComponent(0)) .setText("<html>High: " + forecast.get(i).get(1) + "\u00B0F/" + forecast.get(i).get(0) + "\u00B0C<br />Low: " + forecast.get(i).get(3) + "\u00B0F/" + forecast.get(i).get(2) + "\u00B0C<br />Wind: " + forecast.get(i).get(6) + "mph/" + forecast.get(i).get(7) + "Kmph " + forecast.get(i).get(8) + "<br />Rain: " + forecast.get(i).get(5) + "%<br />" + forecast.get(i).get(4)); tab.revalidate(); } } } public void checkUpdate() { if (update == UPDATE) { if (!Eve.getPref("zip").equals(location)) { changeLocation((String) Eve.getPref("zip")); } if (!Eve.getPref("time zone").equals(timeZone)) { updateLbl(); } } } public void run() { updateLbl(); while (true) { try { checkUpdate(); if ((Eve.clock.getEpoch()) % 1800000 == 0) { update(); updateLbl(); } Thread.sleep(1000); } catch (Exception e) { Thread.currentThread().interrupt(); } } } }