1+ import java .io .*;
2+ import java .net .*;
3+ import java .util .Date ;
4+ import javafx .application .Application ;
5+ import javafx .geometry .Pos ;
6+ import javafx .scene .Scene ;
7+ import javafx .scene .control .Button ;
8+ import javafx .scene .control .Label ;
9+ import javafx .scene .control .TextArea ;
10+ import javafx .scene .control .TextField ;
11+ import javafx .scene .control .Label ;
12+ import javafx .scene .control .ScrollPane ;
13+ import javafx .scene .layout .GridPane ;
14+ import javafx .scene .layout .BorderPane ;
15+ import javafx .stage .Stage ;
16+
17+ public class Exercise31_02Client extends Application {
18+ // IO streams
19+ DataOutputStream toServer =null ;
20+ DataInputStream fromServer =null ;
21+
22+ // Text fields for BMI information
23+ private TextField tfWeight =new TextField ();
24+ private TextField tfHeight =new TextField ();
25+
26+ @ Override // Override the start method in the Application class
27+ public void start (Stage primaryStage ) {
28+ // Main pane
29+ BorderPane pane =new BorderPane ();
30+
31+ // Set text field alignment right
32+ tfWeight .setAlignment (Pos .BASELINE_RIGHT );
33+ tfHeight .setAlignment (Pos .BASELINE_RIGHT );
34+
35+ // Create button to send BMI info to server
36+ Button btSubmit =new Button ("Submit" );
37+
38+ // Pane to hold BMI information and submit button
39+ GridPane paneForBmiInfo =new GridPane ();
40+ paneForBmiInfo .add (new Label ("Weight in pounds" ),0 ,0 );
41+ paneForBmiInfo .add (tfWeight ,1 ,0 );
42+ paneForBmiInfo .add (new Label ("Height in inches" ),0 ,1 );
43+ paneForBmiInfo .add (tfHeight ,1 ,1 );
44+ paneForBmiInfo .add (btSubmit ,2 ,1 );
45+
46+ // Text Area to display contents
47+ TextArea ta =new TextArea ();
48+ pane .setTop (paneForBmiInfo );
49+ pane .setCenter (new ScrollPane (ta ));
50+
51+ // Create a scene and place it in the stage
52+ Scene scene =new Scene (pane ,400 ,200 );
53+ primaryStage .setTitle ("Exercise31_01Client" );// Set the stage title
54+ primaryStage .setScene (scene );// Place the scene in the stage
55+ primaryStage .show ();// Display the stage
56+
57+ btSubmit .setOnAction (e -> {
58+ try {
59+ // Get the weight and height from the text fields
60+ double weight =Double .parseDouble (tfWeight .getText ().trim ());
61+ double height =Double .parseDouble (tfHeight .getText ().trim ());
62+
63+ // Send the BMI information to the server
64+ toServer .writeDouble (weight );
65+ toServer .writeDouble (height );
66+ toServer .flush ();
67+
68+ // Get string from the server
69+ String bmi =fromServer .readUTF ();
70+
71+ // Display to text area
72+ ta .appendText ("Weight: " +weight +'\n' );
73+ ta .appendText ("Height: " +height +'\n' );
74+ ta .appendText (bmi +'\n' );
75+ }
76+ catch (IOException ex ) {
77+ System .err .println (ex );
78+ }
79+ });
80+
81+ try {
82+ // Create a socket to connect to the server
83+ Socket socket =new Socket ("localhost" ,8000 );
84+
85+ // Create an input stream to receive data from the server
86+ fromServer =new DataInputStream (socket .getInputStream ());
87+
88+ // Create an output stream to send data to the server
89+ toServer =new DataOutputStream (socket .getOutputStream ());
90+ }
91+ catch (IOException ex ) {
92+ ta .appendText (ex .toString () +'\n' );
93+ }
94+ }
95+ }