Simple weather application


Weather app powered by OpenWeatherMap API. Done with JavaFX/fxml.
Libs: java-json.jar, owm-japis-2.5.0.5.jar.

model.WeatherModel.java
 /*  
  * MyWeather App with OpenWeatherMap API  
  */  
 package model;  
   
 import javafx.beans.property.LongProperty;  
 import javafx.beans.property.SimpleLongProperty;  
 import javafx.beans.property.SimpleStringProperty;  
 import javafx.beans.property.StringProperty;  
 import net.aksingh.owmjapis.CurrentWeather;  
 import net.aksingh.owmjapis.OpenWeatherMap;  
 import org.json.JSONException;  
   
 /**  
  *  
  * @author djordje gavrilovic  
  */  
 public class WeatherModel {  
     
   private final LongProperty cityID = new SimpleLongProperty(this,"cityID");  
   private final StringProperty cityName = new SimpleStringProperty(this,"cityName");  
   private final StringProperty description = new SimpleStringProperty(this,"description");  
   private final StringProperty temperature = new SimpleStringProperty(this,"temperature");  
   private final StringProperty pressure = new SimpleStringProperty(this,"pressure");  
   private final StringProperty humidity = new SimpleStringProperty(this,"humidity");  
   private final StringProperty wind = new SimpleStringProperty(this,"wind");  
   private final StringProperty iconID = new SimpleStringProperty(this,"iconID");  
     
   public long getCityID() {return cityID.get();}  
   public void setCityID(int cityID) {this.cityID.set(cityID);}  
   public LongProperty cityIDProperty() {return cityID;}  
     
   public String getCityName() {return cityName.get();}  
   public void setCityName(String cityName) {this.cityName.set(cityName);}  
   public StringProperty cityNameProperty() {return cityName;}  
     
   public String getDescription() {return description.get();}  
   public void setDescription(String description) {this.description.set(description);}  
   public StringProperty descriptionProperty() {return description;}  
     
   public String getTemperature() {return temperature.get();}  
   public void setTemperature(String temperature) {this.temperature.set(temperature);}  
   public StringProperty temperatureProperty() {return temperature;}  
     
   public String getPressure() {return pressure.get();}  
   public void setPressure(String pressure) {this.pressure.set(pressure);}  
   public StringProperty pressureProperty() {return pressure;}  
     
   public String getHumidity() {return humidity.get();}  
   public void setHumidity(String humidity) {this.humidity.set(humidity);}  
   public StringProperty humidityProperty() {return humidity;}  
     
   public String getWind() {return wind.get();}  
   public void setWind(String wind) {this.wind.set(wind);}  
   public StringProperty windProperty() {return wind;}  
     
   public String getIconID() {return iconID.get();}  
   public void setIconID(String iconID) {this.iconID.set(iconID);}  
   public StringProperty iconIDProperty() {return iconID;}  
       
   public WeatherModel(){}  
   public WeatherModel(int cityID) throws JSONException{  
     this.cityID.set(cityID);  
     getWeather();  
   }  
     
   // gets weather data from OWM  
   public void getWeather() throws JSONException{  
     OpenWeatherMap wm = new OpenWeatherMap("***************************"); // personal OWM API KEY  
     CurrentWeather cw = wm.currentWeatherByCityCode(cityID.longValue());  
       
     this.cityName.set(cw.getCityName());  
     this.description.set(cw.getWeatherInstance(0).getWeatherDescription());  
     float tempC = (((cw.getMainInstance().getTemperature())-32)*5)/9;  
     this.temperature.set(String.format("%.1f",tempC));  
     this.pressure.set(String.valueOf(cw.getMainInstance().getPressure()));  
     this.humidity.set(String.valueOf(cw.getMainInstance().getHumidity()));  
     float kmph = (cw.getWindInstance().getWindSpeed()) * 1.609F;  
     this.wind.set(String.format("%.1f",kmph));  
     this.iconID.set(cw.getWeatherInstance(0).getWeatherIconName());          
   }  
     
   @Override  
   public String toString() {  
     return   
         "ID: " + cityID.get() + "\n" +   
         "City Name: " + cityName.get() + "\n" +   
         "Description: " + description.get() + "\n" +   
         "Temperature: " + temperature.get() + " C" + "\n" +   
         "Pressure: " + pressure.get() + " mbar" + "\n" +   
         "Humidity: " + humidity.get() + " %" + "\n" +  
         "Wind: " + wind.get() + " kmph" + "\n" +  
         "Icon Name: " + iconID.get();  
   }  
     
 }  
   

controller.WeatherController.java
 /*  
  * MyWeather App with OpenWeatherMap API  
  */  
 package controller;  
   
 import java.net.URL;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 import javafx.application.Application;  
 import javafx.beans.value.ObservableValue;  
 import javafx.fxml.FXML;  
 import javafx.fxml.FXMLLoader;  
 import javafx.scene.Scene;  
 import javafx.scene.control.Button;  
 import javafx.scene.control.Label;  
 import javafx.scene.control.Toggle;  
 import javafx.scene.control.ToggleButton;  
 import javafx.scene.control.ToggleGroup;  
 import javafx.scene.image.ImageView;  
 import javafx.scene.layout.AnchorPane;  
 import javafx.stage.Stage;  
 import model.WeatherModel;  
 import org.json.JSONException;  
   
 /**  
  *  
  * @author djordje gavrilovic  
  */  
 public class WeatherController extends Application {  
     
   WeatherModel wm;  
   int cityID=3204623;  
   
   @FXML  
   private ToggleGroup cityTg;  
   @FXML  
   private Label cityNameLbl;  
   @FXML  
   private Label descriptionLbl;  
   @FXML  
   private Label temperatureLbl;  
   @FXML  
   private Label pressureLbl;  
   @FXML  
   private Label humidityLbl;  
   @FXML  
   private Label windLbl;  
   @FXML  
   private Label iconLbl;  
   @FXML  
   private Button refreshBtn;  
   @FXML  
   private Label timeLbl;  
     
   @FXML  
   private void initialize() throws ClassNotFoundException, JSONException {  
       
     wm = new WeatherModel(cityID);  
     System.out.println(wm.toString());  
       
     cityNameLbl.textProperty().bindBidirectional(wm.cityNameProperty());  
     descriptionLbl.textProperty().bindBidirectional(wm.descriptionProperty());  
     temperatureLbl.textProperty().bindBidirectional(wm.temperatureProperty());  
     pressureLbl.textProperty().bindBidirectional(wm.pressureProperty());  
     humidityLbl.textProperty().bindBidirectional(wm.humidityProperty());  
     windLbl.textProperty().bindBidirectional(wm.windProperty());  
       
     iconLbl.setGraphic(new ImageView("http://openweathermap.org/img/w/"+wm.getIconID()+".png"));  
     iconLbl.setScaleX(1.5);  
     iconLbl.setScaleY(1.5);  
     refreshBtn.setGraphic(new ImageView("/refresh1.png"));  
     refreshBtn.setScaleX(0.8);  
     refreshBtn.setScaleY(0.8);  
     timeLbl.setText(new java.util.Date().toString());  
     cityTg.selectedToggleProperty().addListener((observable,  
       oldValue, newValue) -> {  
       try {  
         toggle(observable, oldValue, newValue);  
       } catch (JSONException ex) {  
         Logger.getLogger(WeatherController.class.getName()).log(Level.SEVERE, null, ex);  
       } catch (ClassNotFoundException ex) {  
         Logger.getLogger(WeatherController.class.getName()).log(Level.SEVERE, null, ex);  
       }  
     });  
   }  
     
   @Override  
   public void start(Stage primaryStage) throws Exception {      
     URL fxmlUrl = getClass().getClassLoader().getResource("view/WeatherView.fxml");  
     AnchorPane root = FXMLLoader.<AnchorPane>load(fxmlUrl);  
     Scene scene = new Scene(root);  
     primaryStage.setScene(scene);  
     primaryStage.setTitle("MyWeather");  
     primaryStage.centerOnScreen();  
     primaryStage.setResizable(false);  
     primaryStage.setOpacity(0.9);  
     primaryStage.show();  
   }  
     
   public static void main(String[] args) {launch(args);}  
     
   private void toggle(ObservableValue<? extends Toggle> observable,  
     Toggle oldValue, Toggle newValue) throws JSONException, ClassNotFoundException {  
       
       ToggleButton tBtn = (ToggleButton) newValue;  
       switch(tBtn.getText()){  
         case "Bajina Basta":  
           cityID=3204623; // City IDs provided by OWM ( not WOEID )  
           break;  
         case "Belgrade":  
           cityID=792680;  
           break;  
         case "Zrenjanin":  
           cityID=783814;  
           break;  
       }  
       initialize();      
   }  
     
 }  
   

view.WeatherView.fxml
 <?xml version="1.0" encoding="UTF-8"?>  
   
 <?import java.lang.*?>  
 <?import java.net.*?>  
 <?import java.util.*?>  
 <?import javafx.scene.*?>  
 <?import javafx.scene.control.*?>  
 <?import javafx.scene.layout.*?>  
   
 <AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="400.0" styleClass="mainFxmlClass"   
       xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.WeatherController">  
   <stylesheets>  
     <URL value="@WeatherStyle.css"/>  
   </stylesheets>  
   <HBox spacing="20" alignment="BASELINE_CENTER" AnchorPane.topAnchor="10.0"   
      AnchorPane.leftAnchor="10.0">  
     <fx:define>  
       <ToggleGroup fx:id="cityTg"/>  
     </fx:define>  
     <children>  
       <RadioButton text="Bajina Basta" toggleGroup="$cityTg" selected="true"/>  
       <RadioButton text="Belgrade" toggleGroup="$cityTg"/>  
       <RadioButton text="Zrenjanin" toggleGroup="$cityTg"/>  
       <Button fx:id="refreshBtn" onAction="#initialize" />  
     </children>  
   </HBox>  
   <Label fx:id="iconLbl" AnchorPane.topAnchor="50.0" />  
   <Label fx:id="cityNameLbl" AnchorPane.topAnchor="100.0" AnchorPane.leftAnchor="10.0"/>  
   <Label fx:id="descriptionLbl" AnchorPane.topAnchor="130.0" AnchorPane.leftAnchor="10.0"/>  
   <Label fx:id="temperatureLbl" AnchorPane.topAnchor="50.0" AnchorPane.leftAnchor="60.0"/>  
   <Label fx:id="celsiusLbl" text="°C" AnchorPane.topAnchor="68.0" AnchorPane.leftAnchor="150.0"/>  
   <Label text="P:" AnchorPane.topAnchor="50.0" AnchorPane.leftAnchor="200.0" />  
   <Label text="mbar" AnchorPane.topAnchor="50.0" AnchorPane.leftAnchor="280.0" />  
   <Label fx:id="pressureLbl" AnchorPane.topAnchor="50.0" AnchorPane.leftAnchor="230.0"/>  
   <Label text="H:" AnchorPane.topAnchor="70.0" AnchorPane.leftAnchor="200.0" />  
   <Label text="\%" AnchorPane.topAnchor="70.0" AnchorPane.leftAnchor="280.0" />  
   <Label fx:id="humidityLbl" AnchorPane.topAnchor="70.0" AnchorPane.leftAnchor="230.0"/>  
   <Label text="W:" AnchorPane.topAnchor="90.0" AnchorPane.leftAnchor="200.0" />  
   <Label text="kmph" AnchorPane.topAnchor="90.0" AnchorPane.leftAnchor="280.0" />  
   <Label fx:id="windLbl" AnchorPane.topAnchor="90.0" AnchorPane.leftAnchor="230.0"/>  
   <Label fx:id="timeLbl" AnchorPane.topAnchor="130.0" AnchorPane.leftAnchor="200.0" />  
   <HBox fx:id="botBox" prefWidth="400.0" spacing="20" alignment="BASELINE_CENTER" AnchorPane.bottomAnchor="0.0"   
      AnchorPane.leftAnchor="0.0">  
     <children>  
       <Label fx:id="djoLbl" text="   Djordje Gavrilovic 2017" />  
       <Label fx:id="owmLbl" text="Powered by OpenWeatherMap" />  
     </children>  
   </HBox>  
 </AnchorPane>  
   

view.WeatherStyle.css
 #cityNameLbl {  
   -fx-font-size: 25;  
 }  
 #temperatureLbl {  
   -fx-font-size: 40;  
 }  
 #celsiusLbl {  
   -fx-font-size: 20;  
 }  
 #timeLbl {  
   -fx-font-size: 11;  
 }  
 #djoLbl {  
   -fx-font-size: 11;   
   -fx-text-fill: gray;  
 }  
 #owmLbl {  
   -fx-font-size: 11;   
   -fx-text-fill: gray;  
 }  
 #botBox {  
   -fx-background-color: black;  
 }  
   
   

Comments

  1. hello, did you use paid subscriptions to OpenWeatherMap?

    ReplyDelete

Post a Comment