0andrejj0

Untitled

Jun 10th, 2023
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.26 KB | None | 0 0
  1. package com.example.client;
  2.  
  3. import javafx.application.Application;
  4. import javafx.application.Platform;
  5. import javafx.event.ActionEvent;
  6. import javafx.event.EventHandler;
  7. import javafx.geometry.Insets;
  8. import javafx.geometry.Pos;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.TextArea;
  11. import javafx.scene.control.TextField;
  12. import javafx.scene.image.Image;
  13. import javafx.scene.layout.*;
  14. import javafx.scene.Scene;
  15. import javafx.scene.paint.Color;
  16. import javafx.scene.text.Font;
  17. import javafx.scene.text.Text;
  18. import javafx.stage.Modality;
  19. import javafx.stage.Stage;
  20.  
  21. import java.io.BufferedReader;
  22. import java.io.IOException;
  23. import java.io.InputStreamReader;
  24. import java.io.PrintWriter;
  25. import java.net.Socket;
  26. import java.util.Arrays;
  27. import java.util.Objects;
  28. import java.util.Timer;
  29. import java.util.TimerTask;
  30.  
  31. public class HelloApplication extends Application {
  32.  
  33. @Override
  34. public void start(Stage stage) throws IOException {
  35. window = stage;
  36.  
  37. initConnectScene();
  38. initRunScene();
  39. initAboutScene();
  40.  
  41. window.setTitle("Connect");
  42. setConnectScene();
  43. window.setResizable(false);
  44. window.show();
  45. }
  46.  
  47. private void setConnectScene() {
  48. window.setMaxHeight(300);
  49. window.setMinHeight(300);
  50. window.setMaxWidth(300);
  51. window.setMinWidth(300);
  52. window.setScene(connectScene);
  53. }
  54.  
  55. private void setRunScene() {
  56. window.setScene(runScene);
  57. }
  58.  
  59. private void initConnectScene() throws IOException {
  60. VBox mainVbox = new VBox();
  61. {
  62. Text serverText = new Text("Server:");
  63. serverText.setFont(new Font(20));
  64. mainVbox.getChildren().add(serverText);
  65. }
  66. {
  67. TextField ipTextField = new TextField("localhost");
  68. serverField = ipTextField;
  69. ipTextField.setMaxSize(150, 30);
  70. ipTextField.setMinSize(150, 30);
  71. ipTextField.setOpacity(0.85);
  72. mainVbox.getChildren().add(ipTextField);
  73. }
  74. {
  75. Text portText = new Text("Port:");
  76. portText.setFont(new Font(20));
  77. mainVbox.getChildren().add(portText);
  78. }
  79. {
  80. TextField portTextField = new TextField("5619");
  81. portField = portTextField;
  82. portTextField.setOpacity(0.85);
  83. portTextField.setMaxSize(150, 30);
  84. portTextField.setMinSize(150, 30);
  85. mainVbox.getChildren().add(portTextField);
  86. }
  87. {
  88. Text nameText = new Text("Name:");
  89. nameText.setFont(new Font(20));
  90. mainVbox.getChildren().add(nameText);
  91. }
  92. {
  93. TextField nameTextField = new TextField("Amogus");
  94. nameField = nameTextField;
  95. nameTextField.setMaxSize(150, 30);
  96. nameTextField.setMinSize(150, 30);
  97. nameTextField.setOpacity(0.85);
  98. mainVbox.getChildren().add(nameTextField);
  99. }
  100. {
  101. Button playButton = new Button("Play");
  102. playButton.setMinSize(150, 40);
  103. playButton.setMaxSize(150, 40);
  104. playButton.setOpacity(0.85);
  105.  
  106. playButton.setOnAction(new EventHandler<ActionEvent>() {
  107. @Override
  108. public void handle(ActionEvent actionEvent) {
  109. try {
  110. startGame();
  111. } catch (IOException e) {
  112. System.out.println(Arrays.toString(e.getStackTrace()));
  113. }
  114. }
  115. });
  116.  
  117. mainVbox.getChildren().add(playButton);
  118. }
  119. {
  120. Button aboutButton = new Button("About");
  121. aboutButton.setMinSize(150, 40);
  122. aboutButton.setMaxSize(150, 40);
  123. aboutButton.setOpacity(0.85);
  124.  
  125. aboutButton.setOnAction(new EventHandler<ActionEvent>() {
  126. @Override
  127. public void handle(ActionEvent actionEvent) {
  128. Stage newWindow = new Stage();
  129. newWindow.setTitle("About");
  130. newWindow.setScene(aboutScene);
  131. newWindow.setMinHeight(300);
  132. newWindow.setMaxHeight(300);
  133. newWindow.setMinWidth(300);
  134. newWindow.setMaxWidth(300);
  135. newWindow.initOwner(window);
  136. newWindow.initModality(Modality.WINDOW_MODAL);
  137. newWindow.show();
  138. }
  139. });
  140.  
  141.  
  142. Image backgroundPicture = new Image(getClass().getResource("/images/car_background.jpg").openStream());
  143.  
  144. mainVbox.setBackground(
  145. new Background(
  146. new BackgroundImage(
  147. backgroundPicture,
  148. BackgroundRepeat.NO_REPEAT,
  149. BackgroundRepeat.NO_REPEAT,
  150. BackgroundPosition.DEFAULT,
  151. new BackgroundSize(
  152. BackgroundSize.AUTO,
  153. 100,
  154. true,
  155. true,
  156. true,
  157. true
  158. )
  159. )
  160. )
  161. );
  162.  
  163. mainVbox.getChildren().add(aboutButton);
  164. }
  165. mainVbox.setAlignment(Pos.TOP_CENTER);
  166. mainVbox.setSpacing(5);
  167. connectScene = new Scene(mainVbox);
  168.  
  169. }
  170.  
  171. private void initAboutScene() throws IOException {
  172. VBox mainVBox = new VBox();
  173. Text aboutText = new Text(" author: andrejj\n Аска или Мисато или Рей?????????\n");
  174. aboutText.setFont(new Font(17));
  175. mainVBox.getChildren().add(aboutText);
  176. Image backgroundPicture = new Image(getClass().getResource("/images/car_background_1.jpg").openStream());
  177.  
  178. mainVBox.setBackground(
  179. new Background(
  180. new BackgroundImage(
  181. backgroundPicture,
  182. BackgroundRepeat.NO_REPEAT,
  183. BackgroundRepeat.NO_REPEAT,
  184. BackgroundPosition.CENTER,
  185. new BackgroundSize(
  186. 100,
  187. BackgroundSize.AUTO,
  188. true,
  189. true,
  190. true,
  191. true
  192. )
  193. )
  194. )
  195. );
  196.  
  197. aboutScene = new Scene(mainVBox);
  198. }
  199.  
  200. private void initRunScene() throws IOException {
  201. VBox mainVBox = new VBox();
  202.  
  203. {
  204. HBox hbox = new HBox();
  205. hbox.setAlignment(Pos.CENTER);
  206. hbox.setPadding(new Insets(20, 0, 20, 0));
  207. Text gameText = new Text("Klavogonkiiiii");
  208. gameText.setFont(new Font(30));
  209. hbox.getChildren().add(gameText);
  210. mainVBox.getChildren().add(hbox);
  211. }
  212.  
  213. {
  214. HBox gameStatusHBox = new HBox();
  215. {
  216. TextArea playersTextArea = new TextArea();
  217. infoField = playersTextArea;
  218. playersTextArea.setPrefSize(760, 220);
  219. playersTextArea.setFont(new Font(30));
  220. playersTextArea.setBackground(Background.fill(new Color(1, 1, 1, 0.1)));
  221. playersTextArea.setEditable(false);
  222. gameStatusHBox.getChildren().add(playersTextArea);
  223. }
  224. {
  225. TextArea timeTextArea = new TextArea();
  226. timeTextArea.setFont(new Font(30));
  227. timerField = timeTextArea;
  228. timeTextArea.setPrefSize(350, 220);
  229. timeTextArea.setBackground(Background.fill(new Color(1, 1, 1, 0.3)));
  230. timeTextArea.setEditable(false);
  231. gameStatusHBox.getChildren().add(timeTextArea);
  232. }
  233. gameStatusHBox.setPadding(new Insets(0, 0, 0, 40));
  234. gameStatusHBox.setSpacing(40);
  235. mainVBox.getChildren().add(gameStatusHBox);
  236. }
  237. {
  238. HBox hBox = new HBox();
  239. hBox.setPadding(new Insets(0, 0, 0, 40));
  240. Text textText = new Text("Text:");
  241. textText.setFont(new Font(30));
  242. hBox.getChildren().add(textText);
  243. mainVBox.getChildren().add(hBox);
  244. }
  245. {
  246. TextArea mainText = new TextArea();
  247. mainText.setMinSize(600, 250);
  248. mainText.setMaxSize(600, 250);
  249. mainTextField = mainText;
  250. HBox hbox = new HBox();
  251. mainText.setEditable(false);
  252. hbox.getChildren().add(mainText);
  253. hbox.setPadding(new Insets(0, 0, 0, 40));
  254. mainVBox.getChildren().add(hbox);
  255. }
  256. {
  257. HBox hBox = new HBox();
  258. hBox.setPadding(new Insets(0, 0, 0, 40));
  259. Text textText = new Text("Input:");
  260. textText.setFont(new Font(30));
  261. hBox.getChildren().add(textText);
  262. mainVBox.getChildren().add(hBox);
  263. }
  264. {
  265.  
  266. TextField inputTextField = new TextField();
  267. inputField = inputTextField;
  268. inputField.setEditable(false);
  269. HBox hbox = new HBox();
  270. inputTextField.setMinSize(760, 70);
  271. inputTextField.setMaxSize(760, 70);
  272. inputTextField.setFont(new Font(35));
  273. hbox.getChildren().add(inputTextField);
  274. hbox.setPadding(new Insets(0, 0, 0, 40));
  275. mainVBox.getChildren().add(hbox);
  276. }
  277.  
  278.  
  279. Image backgroundPicture = new Image(getClass().getResource("/images/aska_rey.png").openStream());
  280.  
  281. mainVBox.setBackground(
  282. new Background(
  283. new BackgroundImage(
  284. backgroundPicture,
  285. BackgroundRepeat.NO_REPEAT,
  286. BackgroundRepeat.NO_REPEAT,
  287. BackgroundPosition.CENTER,
  288. new BackgroundSize(
  289. BackgroundSize.AUTO,
  290. BackgroundSize.AUTO,
  291. true,
  292. true,
  293. true,
  294. true
  295. )
  296. )
  297. )
  298. );
  299.  
  300. mainVBox.setSpacing(10);
  301.  
  302. runScene = new Scene(mainVBox);
  303.  
  304. runScene.getStylesheets().add(Objects.requireNonNull(this.getClass()
  305. .getResource("/css/textarea.css")).toExternalForm());
  306. }
  307.  
  308. private void startGame() throws IOException {
  309. initRunScene();
  310. initConnection();
  311. Stage newWindow = new Stage();
  312. newWindow.setTitle("Game");
  313. newWindow.setScene(runScene);
  314. newWindow.setMinHeight(820);
  315. newWindow.setMaxHeight(820);
  316. newWindow.setMinWidth(1300);
  317. newWindow.setMaxWidth(1300);
  318. newWindow.initOwner(window);
  319. newWindow.initModality(Modality.WINDOW_MODAL);
  320. newWindow.show();
  321.  
  322. inputField.textProperty().addListener(
  323. (observable, oldValue, newValue) -> {
  324. System.out.println("CHANGEEEE");
  325. String target = mainTextField.getText();
  326. System.out.println(target);
  327. System.out.println(newValue);
  328. long e = 0;
  329. long s = 0;
  330. for (int i = 0; i < newValue.length() && i < target.length(); ++i) {
  331. if (target.charAt(i) == newValue.charAt(i) || target.charAt(i) == '\n' && newValue.charAt(i) == ' ') {
  332. ++s;
  333. } else {
  334. if (i >= oldValue.length())
  335. ++e;
  336. }
  337. }
  338. errors += e;
  339. symbols = s;
  340. });
  341.  
  342. timer.schedule(new updateTask(), 1000);
  343. }
  344.  
  345. private volatile long symbols = 0;
  346. private volatile long errors = 0;
  347.  
  348. public class updateTask extends TimerTask {
  349. public void run() {
  350. try {
  351. StringBuilder req = new StringBuilder();
  352. req.append("info");
  353. req.append("\n");
  354. out.write(req.toString());
  355. out.flush();
  356. String[] resp = in.readLine().trim().split(",");
  357. if (resp.length != 3)
  358. resp = in.readLine().trim().split(",");
  359. infoField.setText(resp[2].replace('_', '\n'));
  360. if (resp[0].trim().equals("waiting")) {
  361. if (timerField != null) {
  362. timerField.setText("waiting " + resp[1]);
  363. }
  364. if (Long.parseLong(resp[1].trim()) <= 5000) {
  365. reqText();
  366. }
  367. } else if (resp[0].trim().equals("started")) {
  368. if (timerField != null) {
  369. timerField.setText("running game " + resp[1]);
  370. }
  371. reqText();
  372. if (inputField != null) {
  373. inputField.setEditable(true);
  374. Platform.runLater(() -> {
  375. inputField.requestFocus();
  376. });
  377. }
  378. }
  379. push();
  380. timer.schedule(new updateTask(), 1000);
  381. } catch (Exception ex) {
  382. System.out.println("error running thread " + ex.getMessage());
  383. }
  384. }
  385.  
  386. public void push() {
  387. StringBuilder req = new StringBuilder();
  388. req.append("push");
  389. req.append(",");
  390. req.append(symbols);
  391. req.append(",");
  392. req.append(errors);
  393. req.append("\n");
  394. out.write(req.toString());
  395. out.flush();
  396. try {
  397. String resp = in.readLine();
  398. System.out.println(resp);
  399. } catch (IOException e) {
  400. System.out.println(Arrays.toString(e.getStackTrace()));
  401. }
  402. }
  403.  
  404. public void reqText() throws IOException {
  405. out.write("text\n");
  406. out.flush();
  407. String text = in.readLine();
  408. if (text.trim().equals(""))
  409. text = in.readLine();
  410. mainTextField.setText(text.trim().replace('_', '\n'));
  411. }
  412. }
  413.  
  414. Timer timer = new Timer();
  415.  
  416.  
  417. private void initConnection() throws IOException {
  418. String address = serverField.getText();
  419. String port = portField.getText();
  420. String name = nameField.getText();
  421.  
  422. socket = new Socket(address, Integer.parseInt(port));
  423. out = new PrintWriter(socket.getOutputStream(), true);
  424. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  425.  
  426. StringBuilder req = new StringBuilder();
  427. req.append("reg,");
  428. req.append(name);
  429. out.println(req);
  430. out.flush();
  431.  
  432. String resp = in.readLine();
  433. System.out.println(resp);
  434. }
  435.  
  436. public static void main(String[] args) {
  437. launch();
  438. }
  439.  
  440. private TextArea timerField;
  441. private TextArea infoField;
  442. private TextField inputField;
  443. private TextArea mainTextField;
  444. private Scene connectScene;
  445. private Scene runScene;
  446. private Scene aboutScene;
  447. private TextField serverField;
  448. private TextField portField;
  449. private TextField nameField;
  450.  
  451. Socket socket;
  452. PrintWriter out;
  453. BufferedReader in;
  454.  
  455.  
  456. // TextField
  457.  
  458. Stage window;
  459. }
Advertisement
Add Comment
Please, Sign In to add comment