Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Button;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Label;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import javax.swing.GroupLayout;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class ClientC {
- public static void main(String[] args) throws IOException{
- Socket socket = null;
- String ip = JOptionPane.showInputDialog(null,"Address");;
- String port = JOptionPane.showInputDialog(null,"Port");
- String[] conArr = new String[2];
- if(ip.equals(""))
- conArr[0] = "localhost";
- else
- conArr[0] = ip;
- if(port.equals(""))
- conArr[1] = "80";
- else
- conArr[1] = port;
- BufferedReader in = null;
- PrintWriter out = null;
- try {
- socket = new Socket(ip, Integer.parseInt(port));
- in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- out = new PrintWriter(socket.getOutputStream(), true);
- }
- catch(Exception e) {
- System.out.println(e);
- }
- String gameInfo = in.readLine();
- GUI gui = new GUI();
- gui.updateGUI(gameInfo);
- gui.start();
- String guess = "";
- while(true) {
- while(guess == null || guess.equals("")){
- guess = gui.getGuess();
- System.out.println(guess);
- }
- out.println(guess);
- guess = "";
- gameInfo = in.readLine();
- gui.updateGUI(gameInfo);
- String[] gameInfoArray = gameInfo.split(";");
- if(gameInfoArray[1].equals("true")||gameInfoArray[3].equals("0")) {
- break;
- }
- }
- socket.close();
- }
- }
- class GUI extends Thread {
- private boolean running = true;
- String guessedword;
- String text;
- int guessesleft;
- Button b1;
- String input;
- Label l1;
- Label l2;
- Label l3;
- JTextField tf1;
- public GUI(){
- super();
- JFrame f = new JFrame();
- JPanel p = new JPanel();
- //Window
- f.setVisible(true);
- f.setSize(300,400);
- f.setResizable(false);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setTitle("HANGMAN");
- f.setLocationRelativeTo(null);
- //Panel
- f.add(p);
- GroupLayout layout = new GroupLayout(p);
- p.setLayout(layout);
- p.setOpaque(true);
- p.setBackground(Color.GREEN);
- //Panelobjects
- //Labels
- l1 = new Label("Progress:" + guessedword);
- l2 = new Label("Message:" + text);
- l3 = new Label("Chances Left:" + guessesleft);
- p.add(l1);
- p.add(l2);
- p.add(l3);
- //Text Field
- Dimension textfielddimension = new Dimension(100,20);
- tf1 = new JTextField(input);
- p.add(tf1);
- tf1.setMaximumSize(textfielddimension);
- //Button
- Dimension buttondimension = new Dimension(20,20);
- Button b1 = new Button("Guess!");
- b1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- input = tf1.getText();
- tf1.setText("");
- }
- });
- p.add(b1);
- b1.setMaximumSize(buttondimension);
- //Positioning
- layout.setAutoCreateGaps(true);
- layout.setAutoCreateContainerGaps(true);
- layout.setHorizontalGroup(
- layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
- .addComponent(l1)
- .addComponent(tf1)
- .addComponent(l2)
- .addComponent(l3)
- .addComponent(b1))
- );
- layout.setVerticalGroup(
- layout.createSequentialGroup()
- .addComponent(l1)
- .addComponent(l2)
- .addComponent(l3)
- .addComponent(tf1)
- .addComponent(b1)
- );
- }
- public void run(){
- while(running){
- l1.setText(guessedword);
- l2.setText(text);
- l3.setText("Guesses left: " + Integer.toString(guessesleft));
- try {
- Thread.sleep(200);
- } catch (InterruptedException ie) {
- System.out.println(ie);
- }
- }
- }
- public void updateGUI(String gameInfo){
- String[] gameInfoArray = gameInfo.split(";");
- this.guessedword = gameInfoArray[0];
- this.text = gameInfoArray[2];
- this.guessesleft = Integer.parseInt(gameInfoArray[3]);
- }
- public void stopGUI(){
- running = false;
- }
- public String getGuess(){
- String input2 = input;
- input = "";
- return input2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement