Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import javafx.application.Application;
- import javafx.application.Platform;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.GridPane;
- import javafx.stage.Stage;
- public class Client extends Application {
- static DataOutputStream toServer = null;
- static DataInputStream fromServer = null;
- public TextField field;
- public static TextArea area;
- public BorderPane mainPane;
- static Button topLeft;
- static Button topMid;
- static Button topRight;
- static Button midLeft;
- static Button midMid;
- static Button midRight;
- static Button botLeft;
- static Button botMid;
- static Button botRight;
- private BorderPane borderPane;
- @Override
- public void start(Stage primaryStage) throws Exception {
- // UI
- BorderPane fieldPane = new BorderPane();
- fieldPane.setPadding(new Insets(5, 5, 5, 5));
- fieldPane.setLeft(new Label("Enter text: "));
- field = new TextField();
- field.setAlignment(Pos.BOTTOM_RIGHT);
- fieldPane.setCenter(field);
- BorderPane mainPane = new BorderPane();
- area = new TextArea();
- area.setEditable(false);
- mainPane.setCenter(new ScrollPane(area));
- mainPane.setTop(fieldPane);
- TextArea output = new TextArea();
- output.setWrapText(true);
- GridPane gamePane = new GridPane();
- gamePane.addRow(2);
- gamePane.addColumn(2);
- gamePane.setHgap(250);
- gamePane.setVgap(250);
- topLeft = new Button();
- topLeft.setMinSize(60, 60);
- topMid = new Button();
- topMid.setMinSize(60, 60);
- topRight = new Button();
- topRight.setMinSize(60, 60);
- midLeft = new Button();
- midLeft.setMinSize(60, 60);
- midMid = new Button();
- midMid.setMinSize(60, 60);
- midRight = new Button();
- midRight.setMinSize(60, 60);
- botLeft = new Button();
- botLeft.setMinSize(60, 60);
- botMid = new Button();
- botMid.setMinSize(60, 60);
- botRight = new Button();
- botRight.setMinSize(60, 60);
- gamePane.add(topLeft, 0, 0);
- gamePane.add(topMid, 0, 1);
- gamePane.add(topRight, 0, 2);
- gamePane.add(midLeft, 1, 0);
- gamePane.add(midMid, 1, 1);
- gamePane.add(midRight, 1, 2);
- gamePane.add(botLeft, 2, 0);
- gamePane.add(botMid, 2, 1);
- gamePane.add(botRight, 2, 2);
- mainPane.setCenter(gamePane);
- mainPane.setTop(output);
- // Create a serverScene and place it in the stage
- output.setText("Server Started");
- Scene scene = new Scene(mainPane, 450, 200);
- primaryStage.setTitle("Client");
- primaryStage.setScene(scene);
- primaryStage.show();
- // END UI
- try { // start connect
- Socket socket = new Socket("localhost", 8182);
- fromServer = new DataInputStream(socket.getInputStream());
- toServer = new DataOutputStream(socket.getOutputStream());
- } catch (Exception e) {
- area.appendText(e.toString() + '\n');
- }
- field.setOnAction(e -> {
- try {/*
- String normal = this.field.getText();
- toServer.writeUTF(normal);
- toServer.flush();
- String inverse = fromServer.readUTF();
- area.appendText("Inverse from the server is: " + inverse + '\n');*/
- } catch (Exception e2) {
- area.appendText(e2.toString() + '\n');
- }
- });
- topLeft.setOnAction(e -> {
- try {
- try {
- sendMove.send(1);
- } catch (InterruptedException interruptedException) {
- interruptedException.printStackTrace();
- }
- } catch (IOException ioException) {
- area.appendText(ioException.toString() + '\n');
- }
- });
- topMid.setOnAction(e -> {
- try {
- try {
- sendMove.send(2);
- } catch (InterruptedException interruptedException) {
- interruptedException.printStackTrace();
- }
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- });
- topRight.setOnAction(e -> {
- try {
- try {
- sendMove.send(3);
- } catch (InterruptedException interruptedException) {
- interruptedException.printStackTrace();
- }
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- });
- midLeft.setOnAction(e -> {
- try {
- try {
- sendMove.send(4);
- } catch (InterruptedException interruptedException) {
- interruptedException.printStackTrace();
- }
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- });
- midMid.setOnAction(e -> {
- try {
- try {
- sendMove.send(5);
- } catch (InterruptedException interruptedException) {
- interruptedException.printStackTrace();
- }
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- });
- midRight.setOnAction(e -> {
- try {
- try {
- sendMove.send(6);
- } catch (InterruptedException interruptedException) {
- interruptedException.printStackTrace();
- }
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- });
- botLeft.setOnAction(e -> {
- try {
- try {
- sendMove.send(7);
- } catch (InterruptedException interruptedException) {
- interruptedException.printStackTrace();
- }
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- });
- botMid.setOnAction(e -> {
- try {
- try {
- sendMove.send(8);
- } catch (InterruptedException interruptedException) {
- interruptedException.printStackTrace();
- }
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- });
- botRight.setOnAction(e -> {
- try {
- try {
- sendMove.send(9);
- } catch (InterruptedException interruptedException) {
- interruptedException.printStackTrace();
- }
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- });
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
- class sendMove{
- public static void send(int i) throws IOException, InterruptedException{
- Client.toServer.write(i);
- Client.toServer.flush();
- Platform.runLater(()->{
- int inverse=0;
- try{
- inverse=Client.fromServer.readInt();
- }catch(IOException e){
- e.printStackTrace();
- }
- Client.area.appendText(inverse+" recieved back");
- });
- Thread.sleep(100);
- }
- }
Add Comment
Please, Sign In to add comment