Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. /**
  2. *
  3. * @author Miciałkiewicz Mateusz S17331
  4. *
  5. */
  6.  
  7. package zad1;
  8.  
  9. import org.json.JSONObject;
  10. import javafx.application.Application;
  11. import javafx.geometry.Insets;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Button;
  14. import javafx.scene.control.Tab;
  15. import javafx.scene.control.TabPane;
  16. import javafx.scene.control.TextArea;
  17. import javafx.scene.control.TabPane.TabClosingPolicy;
  18. import javafx.scene.layout.BorderPane;
  19. import javafx.scene.layout.HBox;
  20. import javafx.scene.web.WebEngine;
  21. import javafx.scene.web.WebView;
  22. import javafx.stage.Stage;
  23.  
  24. public class Main extends Application {
  25.  
  26.  
  27. private TextArea tekstArea;
  28. private Button weath, walu, waluNBP, wikiped;
  29. private WebView wwwView;
  30. private WebEngine wwwEngin;
  31. private Scene scen;
  32. private TabPane tabPane;
  33. private Tab tabli1, tabli2;
  34. private BorderPane bPMain, bPBttm, bPWeb;
  35. private HBox hebox;
  36.  
  37.  
  38. private static Service s;
  39. private static String weatherJson;
  40. private static Double rate1;
  41. private static Double rate2;
  42.  
  43. private static String wikiAdr;
  44.  
  45. private void preSgui(Stage primaryStage) {
  46. //Gui
  47. tabPane = new TabPane();
  48. tabPane.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);
  49. tabli1 = new Tab("Opis");
  50. tabli2 = new Tab("Wikipedia");
  51. tabPane.getTabs().addAll(tabli1, tabli2);
  52. bPMain = new BorderPane();
  53. bPMain.setPadding(new Insets(18, 18, 18, 18));
  54. tekstArea = new TextArea();
  55. bPMain.setCenter(tekstArea);
  56.  
  57. bPBttm = new BorderPane();
  58. bPBttm.setPadding(new Insets(12, 0, 0, 0));
  59. hebox = new HBox(16);
  60.  
  61. weath = new Button("Pogoda");
  62. walu = new Button("waluty");
  63. waluNBP = new Button("NBP");
  64. wikiped = new Button("Załaduj wikipedia");
  65.  
  66.  
  67. hebox.getChildren().add(weath);
  68. hebox.getChildren().add(walu);
  69.  
  70. hebox.getChildren().add(waluNBP);
  71.  
  72. hebox.getChildren().add(wikiped);
  73.  
  74. walu.setOnAction((event) -> {
  75. if (rate1 != null)
  76. showInfo("" + rate1);
  77. });
  78.  
  79.  
  80. weath.setOnAction((event) -> {
  81. if (weatherJson != null)
  82. showInfo(formatWeth(weatherJson));
  83. });
  84.  
  85.  
  86. wikiped.setOnAction((event) -> {
  87. if (wikiAdr != null)
  88. wwwEngin.load(wikiAdr);
  89. });
  90.  
  91. waluNBP.setOnAction((event) -> {
  92. if (rate2 != null)
  93. showInfo("" + rate2);
  94. });
  95.  
  96.  
  97.  
  98.  
  99.  
  100. bPBttm.setRight(hebox);
  101. bPMain.setBottom(bPBttm);
  102. tabli1.setContent(bPMain);
  103. bPWeb = new BorderPane();
  104. wwwView = new WebView();
  105. wwwEngin = wwwView.getEngine();
  106. bPWeb.setCenter(wwwView);
  107. tabli2.setContent(bPWeb);
  108. scen = new Scene(tabPane, 800, 600);
  109. }
  110.  
  111.  
  112. public void start(Stage primaryStage) {
  113. preSgui(primaryStage);
  114.  
  115. primaryStage.setTitle("Info City");
  116.  
  117. primaryStage.setScene(scen);
  118.  
  119. primaryStage.show();
  120. }
  121.  
  122. //dane do podmiany
  123. public static void main(String[] args) {
  124. s = new Service("Spain");
  125. weatherJson = s.getWeather("Madrid");
  126. rate1 = s.getRateFor("USD");
  127. rate2 = s.getNBPRate();
  128. wikiAdr = s.getWikiDescr("Madrid");
  129.  
  130. launch(args);
  131.  
  132. }
  133.  
  134.  
  135. public String formatWeth(String strin) {
  136. String formatStr = "";
  137.  
  138. try {
  139. //wypisanie
  140. JSONObject jsonObj = new JSONObject(strin);
  141. if (!jsonObj.isNull("main")) {
  142. formatStr += "Temperatura aktualna: " + jsonObj.getJSONObject("main").get("temp") + "\n";
  143. formatStr += "Temperatura minimalna: " + jsonObj.getJSONObject("main").get("temp_min") + "\n";
  144. formatStr += "Temperatura max: " + jsonObj.getJSONObject("main").get("temp_max") + "\n";
  145. formatStr += "Wilgotnosc: " + jsonObj.getJSONObject("main").get("humidity") + "\n";
  146. formatStr += "Cisnienie: " + jsonObj.getJSONObject("main").get("pressure");
  147. }
  148.  
  149. } catch (Exception err) {
  150. err.printStackTrace();
  151. }
  152. return formatStr;
  153. }
  154.  
  155.  
  156. public void showInfo(String rap) {
  157. tekstArea.clear();
  158. tekstArea.appendText(rap);
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement