Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.client;
- import javafx.application.Application;
- import javafx.application.Platform;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.image.Image;
- import javafx.scene.layout.*;
- import javafx.scene.Scene;
- import javafx.scene.paint.Color;
- import javafx.scene.text.Font;
- import javafx.scene.text.Text;
- import javafx.stage.Modality;
- import javafx.stage.Stage;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.Arrays;
- import java.util.Objects;
- import java.util.Timer;
- import java.util.TimerTask;
- public class HelloApplication extends Application {
- @Override
- public void start(Stage stage) throws IOException {
- window = stage;
- initConnectScene();
- initRunScene();
- initAboutScene();
- window.setTitle("Connect");
- setConnectScene();
- window.setResizable(false);
- window.show();
- }
- private void setConnectScene() {
- window.setMaxHeight(300);
- window.setMinHeight(300);
- window.setMaxWidth(300);
- window.setMinWidth(300);
- window.setScene(connectScene);
- }
- private void setRunScene() {
- window.setScene(runScene);
- }
- private void initConnectScene() throws IOException {
- VBox mainVbox = new VBox();
- {
- Text serverText = new Text("Server:");
- serverText.setFont(new Font(20));
- mainVbox.getChildren().add(serverText);
- }
- {
- TextField ipTextField = new TextField("localhost");
- serverField = ipTextField;
- ipTextField.setMaxSize(150, 30);
- ipTextField.setMinSize(150, 30);
- ipTextField.setOpacity(0.85);
- mainVbox.getChildren().add(ipTextField);
- }
- {
- Text portText = new Text("Port:");
- portText.setFont(new Font(20));
- mainVbox.getChildren().add(portText);
- }
- {
- TextField portTextField = new TextField("5619");
- portField = portTextField;
- portTextField.setOpacity(0.85);
- portTextField.setMaxSize(150, 30);
- portTextField.setMinSize(150, 30);
- mainVbox.getChildren().add(portTextField);
- }
- {
- Text nameText = new Text("Name:");
- nameText.setFont(new Font(20));
- mainVbox.getChildren().add(nameText);
- }
- {
- TextField nameTextField = new TextField("Amogus");
- nameField = nameTextField;
- nameTextField.setMaxSize(150, 30);
- nameTextField.setMinSize(150, 30);
- nameTextField.setOpacity(0.85);
- mainVbox.getChildren().add(nameTextField);
- }
- {
- Button playButton = new Button("Play");
- playButton.setMinSize(150, 40);
- playButton.setMaxSize(150, 40);
- playButton.setOpacity(0.85);
- playButton.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent actionEvent) {
- try {
- startGame();
- } catch (IOException e) {
- System.out.println(Arrays.toString(e.getStackTrace()));
- }
- }
- });
- mainVbox.getChildren().add(playButton);
- }
- {
- Button aboutButton = new Button("About");
- aboutButton.setMinSize(150, 40);
- aboutButton.setMaxSize(150, 40);
- aboutButton.setOpacity(0.85);
- aboutButton.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent actionEvent) {
- Stage newWindow = new Stage();
- newWindow.setTitle("About");
- newWindow.setScene(aboutScene);
- newWindow.setMinHeight(300);
- newWindow.setMaxHeight(300);
- newWindow.setMinWidth(300);
- newWindow.setMaxWidth(300);
- newWindow.initOwner(window);
- newWindow.initModality(Modality.WINDOW_MODAL);
- newWindow.show();
- }
- });
- Image backgroundPicture = new Image(getClass().getResource("/images/car_background.jpg").openStream());
- mainVbox.setBackground(
- new Background(
- new BackgroundImage(
- backgroundPicture,
- BackgroundRepeat.NO_REPEAT,
- BackgroundRepeat.NO_REPEAT,
- BackgroundPosition.DEFAULT,
- new BackgroundSize(
- BackgroundSize.AUTO,
- 100,
- true,
- true,
- true,
- true
- )
- )
- )
- );
- mainVbox.getChildren().add(aboutButton);
- }
- mainVbox.setAlignment(Pos.TOP_CENTER);
- mainVbox.setSpacing(5);
- connectScene = new Scene(mainVbox);
- }
- private void initAboutScene() throws IOException {
- VBox mainVBox = new VBox();
- Text aboutText = new Text(" author: andrejj\n Аска или Мисато или Рей?????????\n");
- aboutText.setFont(new Font(17));
- mainVBox.getChildren().add(aboutText);
- Image backgroundPicture = new Image(getClass().getResource("/images/car_background_1.jpg").openStream());
- mainVBox.setBackground(
- new Background(
- new BackgroundImage(
- backgroundPicture,
- BackgroundRepeat.NO_REPEAT,
- BackgroundRepeat.NO_REPEAT,
- BackgroundPosition.CENTER,
- new BackgroundSize(
- 100,
- BackgroundSize.AUTO,
- true,
- true,
- true,
- true
- )
- )
- )
- );
- aboutScene = new Scene(mainVBox);
- }
- private void initRunScene() throws IOException {
- VBox mainVBox = new VBox();
- {
- HBox hbox = new HBox();
- hbox.setAlignment(Pos.CENTER);
- hbox.setPadding(new Insets(20, 0, 20, 0));
- Text gameText = new Text("Klavogonkiiiii");
- gameText.setFont(new Font(30));
- hbox.getChildren().add(gameText);
- mainVBox.getChildren().add(hbox);
- }
- {
- HBox gameStatusHBox = new HBox();
- {
- TextArea playersTextArea = new TextArea();
- infoField = playersTextArea;
- playersTextArea.setPrefSize(760, 220);
- playersTextArea.setFont(new Font(30));
- playersTextArea.setBackground(Background.fill(new Color(1, 1, 1, 0.1)));
- playersTextArea.setEditable(false);
- gameStatusHBox.getChildren().add(playersTextArea);
- }
- {
- TextArea timeTextArea = new TextArea();
- timeTextArea.setFont(new Font(30));
- timerField = timeTextArea;
- timeTextArea.setPrefSize(350, 220);
- timeTextArea.setBackground(Background.fill(new Color(1, 1, 1, 0.3)));
- timeTextArea.setEditable(false);
- gameStatusHBox.getChildren().add(timeTextArea);
- }
- gameStatusHBox.setPadding(new Insets(0, 0, 0, 40));
- gameStatusHBox.setSpacing(40);
- mainVBox.getChildren().add(gameStatusHBox);
- }
- {
- HBox hBox = new HBox();
- hBox.setPadding(new Insets(0, 0, 0, 40));
- Text textText = new Text("Text:");
- textText.setFont(new Font(30));
- hBox.getChildren().add(textText);
- mainVBox.getChildren().add(hBox);
- }
- {
- TextArea mainText = new TextArea();
- mainText.setMinSize(600, 250);
- mainText.setMaxSize(600, 250);
- mainTextField = mainText;
- HBox hbox = new HBox();
- mainText.setEditable(false);
- hbox.getChildren().add(mainText);
- hbox.setPadding(new Insets(0, 0, 0, 40));
- mainVBox.getChildren().add(hbox);
- }
- {
- HBox hBox = new HBox();
- hBox.setPadding(new Insets(0, 0, 0, 40));
- Text textText = new Text("Input:");
- textText.setFont(new Font(30));
- hBox.getChildren().add(textText);
- mainVBox.getChildren().add(hBox);
- }
- {
- TextField inputTextField = new TextField();
- inputField = inputTextField;
- inputField.setEditable(false);
- HBox hbox = new HBox();
- inputTextField.setMinSize(760, 70);
- inputTextField.setMaxSize(760, 70);
- inputTextField.setFont(new Font(35));
- hbox.getChildren().add(inputTextField);
- hbox.setPadding(new Insets(0, 0, 0, 40));
- mainVBox.getChildren().add(hbox);
- }
- Image backgroundPicture = new Image(getClass().getResource("/images/aska_rey.png").openStream());
- mainVBox.setBackground(
- new Background(
- new BackgroundImage(
- backgroundPicture,
- BackgroundRepeat.NO_REPEAT,
- BackgroundRepeat.NO_REPEAT,
- BackgroundPosition.CENTER,
- new BackgroundSize(
- BackgroundSize.AUTO,
- BackgroundSize.AUTO,
- true,
- true,
- true,
- true
- )
- )
- )
- );
- mainVBox.setSpacing(10);
- runScene = new Scene(mainVBox);
- runScene.getStylesheets().add(Objects.requireNonNull(this.getClass()
- .getResource("/css/textarea.css")).toExternalForm());
- }
- private void startGame() throws IOException {
- initRunScene();
- initConnection();
- Stage newWindow = new Stage();
- newWindow.setTitle("Game");
- newWindow.setScene(runScene);
- newWindow.setMinHeight(820);
- newWindow.setMaxHeight(820);
- newWindow.setMinWidth(1300);
- newWindow.setMaxWidth(1300);
- newWindow.initOwner(window);
- newWindow.initModality(Modality.WINDOW_MODAL);
- newWindow.show();
- inputField.textProperty().addListener(
- (observable, oldValue, newValue) -> {
- System.out.println("CHANGEEEE");
- String target = mainTextField.getText();
- System.out.println(target);
- System.out.println(newValue);
- long e = 0;
- long s = 0;
- for (int i = 0; i < newValue.length() && i < target.length(); ++i) {
- if (target.charAt(i) == newValue.charAt(i) || target.charAt(i) == '\n' && newValue.charAt(i) == ' ') {
- ++s;
- } else {
- if (i >= oldValue.length())
- ++e;
- }
- }
- errors += e;
- symbols = s;
- });
- timer.schedule(new updateTask(), 1000);
- }
- private volatile long symbols = 0;
- private volatile long errors = 0;
- public class updateTask extends TimerTask {
- public void run() {
- try {
- StringBuilder req = new StringBuilder();
- req.append("info");
- req.append("\n");
- out.write(req.toString());
- out.flush();
- String[] resp = in.readLine().trim().split(",");
- if (resp.length != 3)
- resp = in.readLine().trim().split(",");
- infoField.setText(resp[2].replace('_', '\n'));
- if (resp[0].trim().equals("waiting")) {
- if (timerField != null) {
- timerField.setText("waiting " + resp[1]);
- }
- if (Long.parseLong(resp[1].trim()) <= 5000) {
- reqText();
- }
- } else if (resp[0].trim().equals("started")) {
- if (timerField != null) {
- timerField.setText("running game " + resp[1]);
- }
- reqText();
- if (inputField != null) {
- inputField.setEditable(true);
- Platform.runLater(() -> {
- inputField.requestFocus();
- });
- }
- }
- push();
- timer.schedule(new updateTask(), 1000);
- } catch (Exception ex) {
- System.out.println("error running thread " + ex.getMessage());
- }
- }
- public void push() {
- StringBuilder req = new StringBuilder();
- req.append("push");
- req.append(",");
- req.append(symbols);
- req.append(",");
- req.append(errors);
- req.append("\n");
- out.write(req.toString());
- out.flush();
- try {
- String resp = in.readLine();
- System.out.println(resp);
- } catch (IOException e) {
- System.out.println(Arrays.toString(e.getStackTrace()));
- }
- }
- public void reqText() throws IOException {
- out.write("text\n");
- out.flush();
- String text = in.readLine();
- if (text.trim().equals(""))
- text = in.readLine();
- mainTextField.setText(text.trim().replace('_', '\n'));
- }
- }
- Timer timer = new Timer();
- private void initConnection() throws IOException {
- String address = serverField.getText();
- String port = portField.getText();
- String name = nameField.getText();
- socket = new Socket(address, Integer.parseInt(port));
- out = new PrintWriter(socket.getOutputStream(), true);
- in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- StringBuilder req = new StringBuilder();
- req.append("reg,");
- req.append(name);
- out.println(req);
- out.flush();
- String resp = in.readLine();
- System.out.println(resp);
- }
- public static void main(String[] args) {
- launch();
- }
- private TextArea timerField;
- private TextArea infoField;
- private TextField inputField;
- private TextArea mainTextField;
- private Scene connectScene;
- private Scene runScene;
- private Scene aboutScene;
- private TextField serverField;
- private TextField portField;
- private TextField nameField;
- Socket socket;
- PrintWriter out;
- BufferedReader in;
- // TextField
- Stage window;
- }
Advertisement
Add Comment
Please, Sign In to add comment