Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Last active May 4, 2021 17:16
Show Gist options
  • Save jananpatel2002/5beb9da565874a787e430dd0dabbb334 to your computer and use it in GitHub Desktop.
Save jananpatel2002/5beb9da565874a787e430dd0dabbb334 to your computer and use it in GitHub Desktop.
///*
// * Name: Janan Patel
// * Date: 5/4/2020
// * Course Number: CSC 112
// * Course Name: Intermediate topics in Java Programming
// * Problem Number: 12
// * Email: jkpatel2001@student.stcc.edu
// * GUI for API PROJECT
// * Add JavaFX Library to your project
// * Add VM Argument to Run Configuration: --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml
import java.sql.Connection;
import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.*;
import java.sql.DriverManager;
import org.json.simple.parser.ParseException;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class APIGui extends Application {
private static final int APPWIDTH = 700;
private static final int APPHEIGHT = 325;
private static final String TITLE = "The Email Program";
// I include the textFields here because they need to be accessed from other
// classes.
TextField tfipAddress2 = new TextField();
TextField tfipAddress3 = new TextField();
TextField tfipAddress1 = new TextField();
TextField tfipAddress4 = new TextField();
TextArea textArea = new TextArea();
// All the HBoxes/VBoxes and extra components that will be needed through the
// program.
@Override
public void start(Stage primaryStage) throws Exception {
AppGUI scene = new AppGUI();
Scene scene1 = new Scene(scene, APPWIDTH, APPHEIGHT);
primaryStage.setTitle(TITLE);
primaryStage.setScene(scene1);
primaryStage.show();
primaryStage.setResizable(false);
}
public static void main(String[] args) {
launch(args);
}
private class AppGUI extends BorderPane {
public AppGUI() {
topBox topBox = new topBox();
this.setTop(topBox);
textAreaBox centerBox = new textAreaBox();
this.setCenter(centerBox);
}
public class topBox extends VBox {
public topBox() {
HBox ipAddress = new HBox();
Labeled lbipAddress = new Label("Enter IP Address: ");
Label period = new Label(".");
Label period2 = new Label(".");
Label period3 = new Label(".");
Button locate = new Button("Locate");
lbipAddress.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;");
period.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;");
period2.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;");
period3.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;");
tfipAddress1.setPrefColumnCount(5);
tfipAddress1.setAlignment(Pos.CENTER);
tfipAddress1.setPrefHeight(50);
tfipAddress2.setPrefColumnCount(5);
tfipAddress2.setAlignment(Pos.CENTER);
tfipAddress2.setPrefHeight(50);
tfipAddress3.setPrefColumnCount(5);
tfipAddress3.setAlignment(Pos.CENTER);
tfipAddress3.setPrefHeight(50);
tfipAddress4.setPrefColumnCount(5);
tfipAddress4.setAlignment(Pos.CENTER);
tfipAddress4.setPrefHeight(50);
locate.setStyle("-fx-font-weight: bold; -fx-font-size: 17pt; -fx-background-color: grey");
locate.setOnAction(e -> {
try {
calculate();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});
ipAddress.setSpacing(3);
ipAddress.getChildren().addAll(lbipAddress, tfipAddress1, period, tfipAddress2, period2, tfipAddress3,
period3, tfipAddress4, locate);
this.setAlignment(Pos.BASELINE_LEFT);
this.setPadding(new Insets(20));
this.setSpacing(10);
this.getChildren().addAll(ipAddress);
}
}
}
private class textAreaBox extends VBox {
public textAreaBox() {
textArea.setPrefColumnCount(5);
textArea.setWrapText(true);
textArea.setStyle("-fx-font-size: 12pt;");
textArea.setPadding(new Insets(15));
textArea.setPrefRowCount(10);
textArea.setEditable(false);
this.setAlignment(Pos.CENTER);
this.getChildren().addAll(textArea);
}
}
void calculate() throws MalformedURLException, IOException, ParseException {
textArea.clear();
int a = Integer.parseInt(tfipAddress1.getText());
int b = Integer.parseInt(tfipAddress2.getText());
int c = Integer.parseInt(tfipAddress3.getText());
int d = Integer.parseInt(tfipAddress4.getText());
if ((tfipAddress1.getText() != "") && (tfipAddress2.getText() != "") && (tfipAddress3.getText() != "")
&& (tfipAddress4.getText() != "")) {
if ((a > 0 && a < 255)
&& (b > 0 && b < 255)
&& (c > 0 && c < 255)
&& (d > 0 && d < 255)) {
String fullIP = tfipAddress1.getText() + "." + tfipAddress2.getText() + "." + tfipAddress3.getText()
+ "." + tfipAddress4.getText();
System.out.println(fullIP);
ReadJSON ref = new ReadJSON();
String formattedString = ref.search(fullIP);
textArea.setText(formattedString);
} else {
textArea.setText("Make sure that each box has a value between 0 and 255 ");
}
} else {
textArea.setText("Make sure all the fields are filled out");
}
}
}
import java.util.Scanner;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class ReadJSON {
final String APIKey = "1d0da9a6ab18450fb2b4d1b7c55f708c";
final String URL = "https://api.ipgeolocation.io/ipgeo";
public String search(String IP) throws MalformedURLException, IOException, ParseException {
String finalProduct = "";
String begginingProduct = "";
String finalURL = URL + "?apiKey=" + APIKey + "&ip=" + IP;
try {
Scanner sc = new Scanner(new URL(finalURL).openStream());
if (sc.hasNextLine()) {
begginingProduct += sc.nextLine();
System.out.println(begginingProduct + "\n");
var parser = new JSONParser();
var jsonObjectDecode = (JSONObject) parser.parse(begginingProduct);
var ip = (String) jsonObjectDecode.get("ip");
var city = (String) jsonObjectDecode.get("city");
var state_prov = (String) jsonObjectDecode.get("state_prov");
var country_name = (String) jsonObjectDecode.get("country_name");
var zipcode = (String) jsonObjectDecode.get("zipcode");
var longitude = (String) jsonObjectDecode.get("longitude");
var latitude = (String) jsonObjectDecode.get("latitude");
var time_zone = (JSONObject) jsonObjectDecode.get("time_zone");
var name = (String) time_zone.get("name");
Long offset = (Long) time_zone.get("offset");
finalProduct += " IP Address: " + ip + "\n City: " + city + "\n State/Providence: " + state_prov
+ "\n Country: " + country_name + "\n Zipcode: " + zipcode + "\n Longitude: " + longitude
+ "\n Latitude: " + latitude + "\n Time Zone Name: " + name + "\n Time Zone Offset: " + offset;
System.out.println(finalProduct);
}
} catch (Exception ex) {
return finalProduct = "WRONG IP ADDRESS";
}
return finalProduct;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment