Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Lu Liu
- * Date: 4/29/2016
- * Course Number: CSC-112
- * Course Name: Intermediate Topics in Java Programming
- * Email: lliu0001@student.stcc.edu
- *
- * Assignment: HW # 16
- * Programe Description:
- * Quadratic Equation Calculator Client
- */
- import java.io.*;
- import java.net.*;
- import javafx.application.Application;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.ScrollPane;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
- public class CalculatorClient extends Application {
- // IO streams
- DataOutputStream toServer = null;
- DataInputStream fromServer = null;
- @Override // Override the start method in the Application class
- public void start(Stage primaryStage) {
- // Create pane to hold the label and text field
- BorderPane paneForTextField = new BorderPane();
- paneForTextField.setPadding(new Insets(5, 5, 5, 5));
- paneForTextField.setStyle("-fx-border-color: green");
- TextField tf1 = new TextField();
- TextField tf2 = new TextField();
- TextField tf3 = new TextField();
- Button start = new Button("Start");
- VBox vBox = new VBox(10);
- vBox.setPadding(new Insets(5, 5, 5, 5));
- vBox.getChildren().addAll(new Label("Enter coefficient a: "), tf1, new Label("Enter coefficient b: "), tf2,
- new Label("Enter coefficient c: "), tf3, start);
- paneForTextField.setCenter(vBox);
- // Create pane to hold the paneforTextField and TextArea
- BorderPane mainPane = new BorderPane();
- // Text area to display contents
- TextArea ta = new TextArea();
- mainPane.setCenter(new ScrollPane(ta));
- mainPane.setBottom(paneForTextField);
- // Create a scene and place it in the stage
- Scene scene = new Scene(mainPane, 450, 500);
- primaryStage.setTitle("Client"); // Set the stage title
- primaryStage.setScene(scene); // Place the scene in the stage
- primaryStage.show(); // Display the stage
- start.setOnAction(e -> {
- try {
- // Get the coefficients from the text field
- double coA = Double.parseDouble(tf1.getText().trim());
- double coB = Double.parseDouble(tf2.getText().trim());
- double coC = Double.parseDouble(tf3.getText().trim());
- // Send the coefficients to the server
- toServer.writeDouble(coA);
- toServer.writeDouble(coB);
- toServer.writeDouble(coC);
- toServer.flush();
- // Get results from the server
- // Display to the text area
- ta.appendText("Coefficient A is " + coA + "\n");
- ta.appendText("Coefficient B is " + coB + "\n");
- ta.appendText("Coefficient C is " + coC + "\n");
- int status = fromServer.readInt();
- double root1, root2;
- if (status == 2) {
- root1 = fromServer.readDouble();
- root2 = fromServer.readDouble();
- ta.appendText("Root1 received from the server is " + root1 + '\n');
- ta.appendText("Root2 received from the server is " + root2 + '\n');
- } else if (status == 1) {
- root1 = fromServer.readDouble();
- ta.appendText("Root1 received from the server is " + root1 + '\n');
- } else if (status == 0) {
- ta.appendText("There is no real root return");
- }
- } catch (IOException ex) {
- System.err.println(ex);
- }
- });
- try {
- // Create a socket to connect to the server
- Socket socket = new Socket("cs.stcc.edu", 5007);
- // Socket socket = new Socket("134.241.96.49", 5007);
- // Create an input stream to receive data from the server
- fromServer = new DataInputStream(socket.getInputStream());
- // Create an output stream to send data to the server
- toServer = new DataOutputStream(socket.getOutputStream());
- } catch (IOException ex) {
- ta.appendText(ex.toString() + '\n');
- }
- }
- /**
- * The main method is only needed for the IDE with limited JavaFX support.
- * Not needed for running from the command line.
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement