Commit c060ffc0 by Patryk Czarnik

Pobieranie pogody - druga wersja

parent 5220e623
package pobieranie;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
public class PobierzPogodeDlaMiasta {
private Scanner scanner = new Scanner(System.in);
private double lat;
private double lon;
private String miasto;
public static void main(String[] args) {
new PobierzPogodeDlaMiasta().run();
}
public void run() {
try {
System.out.print("Podaj nazwę miasta: ");
String szukane = scanner.nextLine();
if(znajdzMiasto(szukane)) {
pobierzIWypiszPogode();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean znajdzMiasto(String szukane) throws IOException {
URL url = new URL("https://geocoding-api.open-meteo.com/v1/search?name=" + szukane);
try(InputStream input = url.openStream();
JsonReader reader = Json.createReader(input)) {
JsonObject json = reader.readObject();
JsonArray results = json.getJsonArray("results");
if(results.isEmpty()) {
System.out.println("Brak znalezionych lokalizacji");
return false;
}
System.out.println("Znalezione lokalizacje:");
int i = 0;
for(JsonValue result : results) {
JsonObject resulto = result.asJsonObject();
//System.out.print(result);
System.out.printf("%2d: <%+.3f %+.3f> → %s, %s%n", i++,
resulto.getJsonNumber("latitude").doubleValue(),
resulto.getJsonNumber("longitude").doubleValue(),
resulto.getString("name"), resulto.getString("country"));
}
System.out.print("Wybierz numer lokalizacji: ");
i = scanner.nextInt();
if(i < 0 || i >= results.size()) {
return false;
}
JsonObject loc = results.getJsonObject(i);
lat = loc.getJsonNumber("latitude").doubleValue();
lon = loc.getJsonNumber("longitude").doubleValue();
miasto = loc.getString("name");
return true;
}
}
private void pobierzIWypiszPogode() throws IOException {
URL url = new URL("https://api.open-meteo.com/v1/forecast?latitude=" + lat + "&longitude="+ lon +"&current_weather=true&hourly=temperature_2m");
try(InputStream input = url.openStream();
JsonReader reader = Json.createReader(input)) {
JsonObject json = reader.readObject();
JsonObject weather = json.getJsonObject("current_weather");
System.out.printf("Bieżąca pogoda dla miasta %s:%n", miasto);
System.out.println(weather);
System.out.printf("Temperatura %.1f °C%n", weather.getJsonNumber("temperature").doubleValue());
System.out.printf("Wiatr: %.1f km/h%n", weather.getJsonNumber("windspeed").doubleValue());
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.5" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel5" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="jLabel6" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="26" max="32767" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="jLabel8" pref="252" max="32767" attributes="0"/>
<Component id="jLabel7" max="32767" attributes="0"/>
</Group>
</Group>
<Group type="102" attributes="0">
<Component id="jLabel4" min="-2" pref="74" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="33" max="-2" attributes="0"/>
<Component id="jTextField1" max="32767" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="jButton1" alignment="1" max="32767" attributes="0"/>
<Group type="102" alignment="1" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel1" min="-2" pref="228" max="-2" attributes="0"/>
<Component id="jLabel2" alignment="0" min="-2" pref="228" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jSpinner2" alignment="0" min="-2" pref="119" max="-2" attributes="0"/>
<Component id="jSpinner1" min="-2" pref="119" max="-2" attributes="0"/>
</Group>
</Group>
<Group type="102" alignment="1" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel3" alignment="0" min="-2" pref="114" max="-2" attributes="0"/>
<Component id="jLabel9" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="32767" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="jLabel_Temperatura" pref="169" max="32767" attributes="0"/>
<Component id="jLabel10" max="32767" attributes="0"/>
</Group>
</Group>
</Group>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="13" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jLabel4" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jTextField1" alignment="3" min="-2" pref="37" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jLabel5" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jLabel7" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jLabel6" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jLabel8" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jSpinner1" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jLabel1" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jSpinner2" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Component id="jButton1" min="-2" pref="64" max="-2" attributes="0"/>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jLabel3" alignment="3" min="-2" pref="31" max="-2" attributes="0"/>
<Component id="jLabel_Temperatura" alignment="3" min="-2" pref="31" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jLabel9" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jLabel10" alignment="3" min="-2" pref="30" max="-2" attributes="0"/>
</Group>
<EmptySpace pref="53" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JSpinner" name="jSpinner1">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="18" style="0"/>
</Property>
<Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor">
<SpinnerModel initial="52.0" maximum="90.0" minimum="-90.0" numberType="java.lang.Double" stepSize="1.0" type="number"/>
</Property>
</Properties>
</Component>
<Component class="javax.swing.JSpinner" name="jSpinner2">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="18" style="0"/>
</Property>
<Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor">
<SpinnerModel initial="21.0" maximum="180.0" minimum="-180.0" numberType="java.lang.Double" stepSize="1.0" type="number"/>
</Property>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel1">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="18" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="Szeroko&#x15b;&#x107; geograficzna"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel2">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="18" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="D&#x142;ugo&#x15b;&#x107; geograficzna"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="jButton1">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="24" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="Pobierz pogod&#x119;"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="jLabel3">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="18" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="Temperatura"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel_Temperatura">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="18" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="jLabel4"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel4">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="18" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="Miasto:"/>
</Properties>
</Component>
<Component class="javax.swing.JTextField" name="jTextField1">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="18" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="miasto"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jTextField1ActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="jLabel5">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="15" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="Ludno&#x15b;&#x107;:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel6">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="15" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="Wysoko&#x15b;&#x107; npm"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel7">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="15" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="jLabel7"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel8">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="15" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="jLabel8"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel9">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="18" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="Pr&#x119;dko&#x15b;&#x107; wiatru:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel10">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Segoe UI" size="18" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="jLabel10"/>
</Properties>
</Component>
</SubComponents>
</Form>
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package pogoda;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
/**
*
* @author kurs
*/
public class WyswietlPogodeDlaMiasta extends javax.swing.JFrame {
/**
* Creates new form WyswietlPogodeDlaWspolrzednych
*/
public WyswietlPogodeDlaMiasta() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jSpinner1 = new javax.swing.JSpinner();
jSpinner2 = new javax.swing.JSpinner();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jLabel_Temperatura = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jSpinner1.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
jSpinner1.setModel(new javax.swing.SpinnerNumberModel(52.0d, -90.0d, 90.0d, 1.0d));
jSpinner2.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
jSpinner2.setModel(new javax.swing.SpinnerNumberModel(21.0d, -180.0d, 180.0d, 1.0d));
jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
jLabel1.setText("Szerokość geograficzna");
jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
jLabel2.setText("Długość geograficzna");
jButton1.setFont(new java.awt.Font("Segoe UI", 1, 24)); // NOI18N
jButton1.setText("Pobierz pogodę");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel3.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
jLabel3.setText("Temperatura");
jLabel_Temperatura.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
jLabel_Temperatura.setText("jLabel4");
jLabel4.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
jLabel4.setText("Miasto:");
jTextField1.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
jTextField1.setText("miasto");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel5.setFont(new java.awt.Font("Segoe UI", 0, 15)); // NOI18N
jLabel5.setText("Ludność:");
jLabel6.setFont(new java.awt.Font("Segoe UI", 0, 15)); // NOI18N
jLabel6.setText("Wysokość npm");
jLabel7.setFont(new java.awt.Font("Segoe UI", 0, 15)); // NOI18N
jLabel7.setText("jLabel7");
jLabel8.setFont(new java.awt.Font("Segoe UI", 0, 15)); // NOI18N
jLabel8.setText("jLabel8");
jLabel9.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
jLabel9.setText("Prędkość wiatru:");
jLabel10.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
jLabel10.setText("jLabel10");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
.addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(33, 33, 33)
.addComponent(jTextField1))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel9))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel_Temperatura, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
.addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(13, 13, 13)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(jLabel8))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel_Temperatura, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel9)
.addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(53, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
double lat = (Double)jSpinner1.getValue();
double lon = (Double)jSpinner2.getValue();
String adres = "https://api.open-meteo.com/v1/forecast?latitude=" + lat
+ "&longitude=" + lon
+ "&current_weather=true";
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(new URI(adres)).build();
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
JsonReader reader = Json.createReader(response.body());
JsonObject json = reader.readObject();
System.out.println(json);
JsonObject weather = json.getJsonObject("current_weather");
double temperatura = weather.getJsonNumber("temperature").doubleValue();
double wiatr = weather.getJsonNumber("windspeed").doubleValue();
jLabel_Temperatura.setText(String.valueOf(temperatura));
jLabel10.setText(String.valueOf(wiatr));
} catch (URISyntaxException | IOException | InterruptedException e) {
e.printStackTrace();
}
}//GEN-LAST:event_jButton1ActionPerformed
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// to się wykona po naciśnięciu enter w polu tekstowym
String miasto = jTextField1.getText().strip();
String adres = "https://geocoding-api.open-meteo.com/v1/search?name=" + miasto.replace(" ", "%20");
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(new URI(adres)).build();
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
JsonReader reader = Json.createReader(response.body());
JsonObject json = reader.readObject();
System.out.println(json);
JsonArray listaWynikow = json.getJsonArray("results");
if(listaWynikow == null || listaWynikow.isEmpty()) {
jTextField1.setForeground(Color.RED);
} else {
JsonObject daneMiasta = listaWynikow.getJsonObject(0);
jTextField1.setForeground(Color.BLACK);
jTextField1.setText(daneMiasta.getString("name", miasto));
double lat = daneMiasta.getJsonNumber("latitude").doubleValue();
double lon = daneMiasta.getJsonNumber("longitude").doubleValue();
jSpinner1.setValue(lat);
jSpinner2.setValue(lon);
if(daneMiasta.containsKey("population")) {
jLabel7.setText(String.valueOf(daneMiasta.getJsonNumber("population")));
} else {
jLabel7.setText("");
}
if(daneMiasta.containsKey("elevation")) {
jLabel8.setText(String.valueOf(daneMiasta.getJsonNumber("elevation")));
} else {
jLabel8.setText("");
}
}
jButton1ActionPerformed(evt);
} catch (URISyntaxException | IOException | InterruptedException e) {
e.printStackTrace();
}
}//GEN-LAST:event_jTextField1ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(WyswietlPogodeDlaMiasta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(WyswietlPogodeDlaMiasta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(WyswietlPogodeDlaMiasta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(WyswietlPogodeDlaMiasta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new WyswietlPogodeDlaMiasta().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JLabel jLabel_Temperatura;
private javax.swing.JSpinner jSpinner1;
private javax.swing.JSpinner jSpinner2;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}
...@@ -68,7 +68,7 @@ ...@@ -68,7 +68,7 @@
<Component id="jLabel3" alignment="3" min="-2" pref="31" max="-2" attributes="0"/> <Component id="jLabel3" alignment="3" min="-2" pref="31" max="-2" attributes="0"/>
<Component id="jLabel_Temperatura" alignment="3" min="-2" pref="31" max="-2" attributes="0"/> <Component id="jLabel_Temperatura" alignment="3" min="-2" pref="31" max="-2" attributes="0"/>
</Group> </Group>
<EmptySpace pref="259" max="32767" attributes="0"/> <EmptySpace pref="61" max="32767" attributes="0"/>
</Group> </Group>
</Group> </Group>
</DimensionLayout> </DimensionLayout>
......
...@@ -112,7 +112,7 @@ public class WyswietlPogodeDlaWspolrzednych extends javax.swing.JFrame { ...@@ -112,7 +112,7 @@ public class WyswietlPogodeDlaWspolrzednych extends javax.swing.JFrame {
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel_Temperatura, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel_Temperatura, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(259, Short.MAX_VALUE)) .addContainerGap(61, Short.MAX_VALUE))
); );
pack(); pack();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment