1  import java.io.*;
  2  import java.net.*;
  3  import javafx.application.Application;
  4  import javafx.geometry.Insets;
  5  import javafx.geometry.Pos;
  6  import javafx.scene.Scene;
  7  import javafx.scene.control.Label;
  8  import javafx.scene.control.ScrollPane;
  9  import javafx.scene.control.TextArea;
 10  import javafx.scene.control.TextField;
 11  import javafx.scene.layout.BorderPane;
 12  import javafx.stage.Stage;
 13  
 14  public class Client extends Application {
 15    // IO streams
 16    DataOutputStream toServer = null;
 17    DataInputStream fromServer = null;
 18  
 19    @Override // Override the start method in the Application class
 20    public void start(Stage primaryStage) {
 21      // Panel p to hold the label and text field
 22      BorderPane paneForTextField = new BorderPane();
 23      paneForTextField.setPadding(new Insets(5, 5, 5, 5)); 
 24      paneForTextField.setStyle("-fx-border-color: green");
 25      paneForTextField.setLeft(new Label("Enter a radius: "));
 26      
 27      TextField tf = new TextField();
 28      tf.setAlignment(Pos.BOTTOM_RIGHT);
 29      paneForTextField.setCenter(tf);
 30      
 31      BorderPane mainPane = new BorderPane();
 32      // Text area to display contents
 33      TextArea ta = new TextArea();
 34      mainPane.setCenter(new ScrollPane(ta));
 35      mainPane.setTop(paneForTextField);
 36      
 37      // Create a scene and place it in the stage
 38      Scene scene = new Scene(mainPane, 450, 200);
 39      primaryStage.setTitle("Client"); // Set the stage title
 40      primaryStage.setScene(scene); // Place the scene in the stage
 41      primaryStage.show(); // Display the stage
 42      
 43      tf.setOnAction(e -> {
 44        try {
 45          // Get the radius from the text field
 46          double radius = Double.parseDouble(tf.getText().trim());
 47    
 48          // Send the radius to the server
 49          toServer.writeDouble(radius);
 50          toServer.flush();
 51    
 52          // Get area from the server
 53          double area = fromServer.readDouble();
 54    
 55          // Display to the text area
 56          ta.appendText("Radius is " + radius + "\n");
 57          ta.appendText("Area received from the server is "
 58            + area + '\n');
 59        }
 60        catch (IOException ex) {
 61          System.err.println(ex);
 62        }
 63      });
 64    
 65      try {
 66        // Create a socket to connect to the server
 67        Socket socket = new Socket("localhost", 8000);
 68        // Socket socket = new Socket("130.254.204.36", 8000);
 69        // Socket socket = new Socket("drake.Armstrong.edu", 8000);
 70  
 71        // Create an input stream to receive data from the server
 72        fromServer = new DataInputStream(socket.getInputStream());
 73  
 74        // Create an output stream to send data to the server
 75        toServer = new DataOutputStream(socket.getOutputStream());
 76      }
 77      catch (IOException ex) {
 78        ta.appendText(ex.toString() + '\n');
 79      }
 80    }
 81  
 82    /**
 83     * The main method is only needed for the IDE with limited
 84     * JavaFX support. Not needed for running from the command line.
 85     */
 86    public static void main(String[] args) {
 87      launch(args);
 88    }
 89  }