Guest User

Untitled

a guest
May 15th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.net.*;
  4.  
  5. import javafx.application.Application;
  6. import javafx.application.Platform;
  7. import javafx.geometry.Insets;
  8. import javafx.geometry.Pos;
  9. import javafx.scene.Scene;
  10. import javafx.scene.control.*;
  11. import javafx.scene.layout.BorderPane;
  12. import javafx.scene.layout.GridPane;
  13. import javafx.stage.Stage;
  14.  
  15. public class Client extends Application {
  16.  
  17. static DataOutputStream toServer = null;
  18. static DataInputStream fromServer = null;
  19. public TextField field;
  20. public static TextArea area;
  21. public BorderPane mainPane;
  22. static Button topLeft;
  23. static Button topMid;
  24. static Button topRight;
  25. static Button midLeft;
  26. static Button midMid;
  27. static Button midRight;
  28. static Button botLeft;
  29. static Button botMid;
  30. static Button botRight;
  31.  
  32. private BorderPane borderPane;
  33.  
  34. @Override
  35. public void start(Stage primaryStage) throws Exception {
  36. // UI
  37. BorderPane fieldPane = new BorderPane();
  38. fieldPane.setPadding(new Insets(5, 5, 5, 5));
  39. fieldPane.setLeft(new Label("Enter text: "));
  40.  
  41. field = new TextField();
  42. field.setAlignment(Pos.BOTTOM_RIGHT);
  43. fieldPane.setCenter(field);
  44.  
  45. BorderPane mainPane = new BorderPane();
  46. area = new TextArea();
  47. area.setEditable(false);
  48. mainPane.setCenter(new ScrollPane(area));
  49. mainPane.setTop(fieldPane);
  50.  
  51. TextArea output = new TextArea();
  52. output.setWrapText(true);
  53. GridPane gamePane = new GridPane();
  54. gamePane.addRow(2);
  55. gamePane.addColumn(2);
  56. gamePane.setHgap(250);
  57. gamePane.setVgap(250);
  58.  
  59. topLeft = new Button();
  60. topLeft.setMinSize(60, 60);
  61. topMid = new Button();
  62. topMid.setMinSize(60, 60);
  63. topRight = new Button();
  64. topRight.setMinSize(60, 60);
  65. midLeft = new Button();
  66. midLeft.setMinSize(60, 60);
  67. midMid = new Button();
  68. midMid.setMinSize(60, 60);
  69. midRight = new Button();
  70. midRight.setMinSize(60, 60);
  71. botLeft = new Button();
  72. botLeft.setMinSize(60, 60);
  73. botMid = new Button();
  74. botMid.setMinSize(60, 60);
  75. botRight = new Button();
  76. botRight.setMinSize(60, 60);
  77. gamePane.add(topLeft, 0, 0);
  78. gamePane.add(topMid, 0, 1);
  79. gamePane.add(topRight, 0, 2);
  80. gamePane.add(midLeft, 1, 0);
  81. gamePane.add(midMid, 1, 1);
  82. gamePane.add(midRight, 1, 2);
  83. gamePane.add(botLeft, 2, 0);
  84. gamePane.add(botMid, 2, 1);
  85. gamePane.add(botRight, 2, 2);
  86. mainPane.setCenter(gamePane);
  87. mainPane.setTop(output);
  88. // Create a serverScene and place it in the stage
  89. output.setText("Server Started");
  90.  
  91. Scene scene = new Scene(mainPane, 450, 200);
  92. primaryStage.setTitle("Client");
  93. primaryStage.setScene(scene);
  94. primaryStage.show();
  95. // END UI
  96.  
  97.  
  98. try { // start connect
  99. Socket socket = new Socket("localhost", 8182);
  100.  
  101. fromServer = new DataInputStream(socket.getInputStream());
  102. toServer = new DataOutputStream(socket.getOutputStream());
  103. } catch (Exception e) {
  104. area.appendText(e.toString() + '\n');
  105. }
  106.  
  107. field.setOnAction(e -> {
  108. try {/*
  109. String normal = this.field.getText();
  110.  
  111. toServer.writeUTF(normal);
  112. toServer.flush();
  113.  
  114. String inverse = fromServer.readUTF();
  115. area.appendText("Inverse from the server is: " + inverse + '\n');*/
  116. } catch (Exception e2) {
  117. area.appendText(e2.toString() + '\n');
  118. }
  119. });
  120. topLeft.setOnAction(e -> {
  121. try {
  122. try {
  123. sendMove.send(1);
  124. } catch (InterruptedException interruptedException) {
  125. interruptedException.printStackTrace();
  126. }
  127. } catch (IOException ioException) {
  128. area.appendText(ioException.toString() + '\n');
  129. }
  130. });
  131. topMid.setOnAction(e -> {
  132. try {
  133. try {
  134. sendMove.send(2);
  135. } catch (InterruptedException interruptedException) {
  136. interruptedException.printStackTrace();
  137. }
  138. } catch (IOException ioException) {
  139. ioException.printStackTrace();
  140. }
  141. });
  142. topRight.setOnAction(e -> {
  143. try {
  144. try {
  145. sendMove.send(3);
  146. } catch (InterruptedException interruptedException) {
  147. interruptedException.printStackTrace();
  148. }
  149. } catch (IOException ioException) {
  150. ioException.printStackTrace();
  151. }
  152. });
  153. midLeft.setOnAction(e -> {
  154. try {
  155. try {
  156. sendMove.send(4);
  157. } catch (InterruptedException interruptedException) {
  158. interruptedException.printStackTrace();
  159. }
  160. } catch (IOException ioException) {
  161. ioException.printStackTrace();
  162. }
  163. });
  164. midMid.setOnAction(e -> {
  165. try {
  166. try {
  167. sendMove.send(5);
  168. } catch (InterruptedException interruptedException) {
  169. interruptedException.printStackTrace();
  170. }
  171. } catch (IOException ioException) {
  172. ioException.printStackTrace();
  173. }
  174. });
  175. midRight.setOnAction(e -> {
  176. try {
  177. try {
  178. sendMove.send(6);
  179. } catch (InterruptedException interruptedException) {
  180. interruptedException.printStackTrace();
  181. }
  182. } catch (IOException ioException) {
  183. ioException.printStackTrace();
  184. }
  185. });
  186. botLeft.setOnAction(e -> {
  187. try {
  188. try {
  189. sendMove.send(7);
  190. } catch (InterruptedException interruptedException) {
  191. interruptedException.printStackTrace();
  192. }
  193. } catch (IOException ioException) {
  194. ioException.printStackTrace();
  195. }
  196. });
  197. botMid.setOnAction(e -> {
  198. try {
  199. try {
  200. sendMove.send(8);
  201. } catch (InterruptedException interruptedException) {
  202. interruptedException.printStackTrace();
  203. }
  204. } catch (IOException ioException) {
  205. ioException.printStackTrace();
  206. }
  207. });
  208. botRight.setOnAction(e -> {
  209. try {
  210. try {
  211. sendMove.send(9);
  212. } catch (InterruptedException interruptedException) {
  213. interruptedException.printStackTrace();
  214. }
  215. } catch (IOException ioException) {
  216. ioException.printStackTrace();
  217. }
  218. });
  219. }
  220.  
  221.  
  222. public static void main(String[] args) {
  223. launch(args);
  224. }
  225. }
  226. class sendMove{
  227.  
  228. public static void send(int i) throws IOException, InterruptedException{
  229. Client.toServer.write(i);
  230. Client.toServer.flush();
  231. Platform.runLater(()->{
  232.  
  233. int inverse=0;
  234. try{
  235. inverse=Client.fromServer.readInt();
  236. }catch(IOException e){
  237. e.printStackTrace();
  238. }
  239.  
  240. Client.area.appendText(inverse+" recieved back");
  241. });
  242. Thread.sleep(100);
  243. }
  244. }
Add Comment
Please, Sign In to add comment