Advertisement
Guest User

client

a guest
Nov 20th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. import java.awt.Button;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Label;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.io.PrintWriter;
  11. import java.net.Socket;
  12.  
  13. import javax.swing.GroupLayout;
  14. import javax.swing.JFrame;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JPanel;
  17. import javax.swing.JTextField;
  18.  
  19.  
  20. public class ClientC {
  21.    
  22.     public static void main(String[] args) throws IOException{
  23.         Socket socket = null;
  24.            
  25.         String ip = JOptionPane.showInputDialog(null,"Address");;
  26.         String port = JOptionPane.showInputDialog(null,"Port");
  27.            
  28.         String[] conArr = new String[2];
  29.  
  30.         if(ip.equals(""))
  31.             conArr[0] = "localhost";
  32.         else
  33.             conArr[0] = ip;
  34.            
  35.         if(port.equals(""))
  36.             conArr[1] = "80";
  37.         else
  38.             conArr[1] = port;
  39.        
  40.         BufferedReader in = null;
  41.         PrintWriter out = null;
  42.        
  43.         try {
  44.             socket = new Socket(ip, Integer.parseInt(port));
  45.             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  46.             out = new PrintWriter(socket.getOutputStream(), true);
  47.         }
  48.         catch(Exception e) {
  49.             System.out.println(e);
  50.         }
  51.  
  52.        
  53.         String gameInfo = in.readLine();
  54.        
  55.         GUI gui = new GUI();
  56.         gui.updateGUI(gameInfo);
  57.         gui.start();
  58.         String guess = "";
  59.  
  60.         while(true) {
  61.             while(guess == null || guess.equals("")){
  62.                 guess = gui.getGuess();
  63.                 System.out.println(guess);
  64.             }
  65.            
  66.             out.println(guess);
  67.             guess = "";
  68.             gameInfo = in.readLine();
  69.             gui.updateGUI(gameInfo);
  70.            
  71.             String[] gameInfoArray = gameInfo.split(";");
  72.             if(gameInfoArray[1].equals("true")||gameInfoArray[3].equals("0")) {
  73.                 break;
  74.             }
  75.         }
  76.         socket.close();
  77.  
  78.     }
  79.  
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86. class GUI extends Thread {
  87.     private boolean running = true;
  88.     String guessedword;
  89.     String text;
  90.     int guessesleft;
  91.     Button b1;
  92.     String input;
  93.     Label l1;
  94.     Label l2;
  95.     Label l3;
  96.     JTextField tf1;
  97.    
  98.     public GUI(){
  99.         super();
  100.        
  101.         JFrame f = new JFrame();
  102.         JPanel p = new JPanel();
  103.        
  104.         //Window
  105.         f.setVisible(true);
  106.         f.setSize(300,400);
  107.         f.setResizable(false);
  108.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  109.         f.setTitle("HANGMAN");
  110.         f.setLocationRelativeTo(null);
  111.        
  112.         //Panel
  113.         f.add(p);
  114.         GroupLayout layout = new GroupLayout(p);
  115.         p.setLayout(layout);
  116.         p.setOpaque(true);
  117.         p.setBackground(Color.GREEN);
  118.        
  119.         //Panelobjects
  120.            
  121.         //Labels
  122.         l1 = new Label("Progress:" + guessedword);
  123.         l2 = new Label("Message:" + text);
  124.         l3 = new Label("Chances Left:" + guessesleft);
  125.         p.add(l1);
  126.         p.add(l2);
  127.         p.add(l3);
  128.            
  129.         //Text Field
  130.         Dimension textfielddimension = new Dimension(100,20);
  131.         tf1 = new JTextField(input);
  132.         p.add(tf1);
  133.         tf1.setMaximumSize(textfielddimension);
  134.        
  135.         //Button
  136.         Dimension buttondimension = new Dimension(20,20);
  137.         Button b1 = new Button("Guess!");
  138.         b1.addActionListener(new ActionListener() {
  139.             public void actionPerformed(ActionEvent e) {
  140.                 input = tf1.getText();
  141.                 tf1.setText("");
  142.             }
  143.         });
  144.         p.add(b1);
  145.         b1.setMaximumSize(buttondimension);
  146.        
  147.         //Positioning
  148.         layout.setAutoCreateGaps(true);
  149.         layout.setAutoCreateContainerGaps(true);
  150.         layout.setHorizontalGroup(
  151.                 layout.createSequentialGroup()
  152.                     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
  153.                     .addComponent(l1)
  154.                     .addComponent(tf1)
  155.                     .addComponent(l2)
  156.                     .addComponent(l3)
  157.                     .addComponent(b1))
  158.         );
  159.         layout.setVerticalGroup(
  160.                 layout.createSequentialGroup()
  161.                     .addComponent(l1)
  162.                     .addComponent(l2)
  163.                     .addComponent(l3)
  164.                     .addComponent(tf1)
  165.                     .addComponent(b1)
  166.         );
  167.     }
  168.    
  169.    
  170.    
  171.     public void run(){
  172.         while(running){
  173.             l1.setText(guessedword);
  174.             l2.setText(text);
  175.             l3.setText("Guesses left: " + Integer.toString(guessesleft));
  176.            
  177.             try {
  178.                 Thread.sleep(200);
  179.             } catch (InterruptedException ie) {
  180.                 System.out.println(ie);
  181.             }
  182.         }
  183.     }
  184.    
  185.     public void updateGUI(String gameInfo){
  186.         String[] gameInfoArray = gameInfo.split(";");
  187.          
  188.         this.guessedword = gameInfoArray[0];
  189.         this.text = gameInfoArray[2];
  190.         this.guessesleft = Integer.parseInt(gameInfoArray[3]);
  191.     }
  192.    
  193.     public void stopGUI(){
  194.         running = false;
  195.     }
  196.  
  197.     public String getGuess(){
  198.         String input2 = input;
  199.         input = "";
  200.         return input2;
  201.     }
  202.    
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement