Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.18 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import javax.swing.*;
  7. import java.awt.*;
  8. import java.net.*;
  9. import java.util.Scanner;
  10.  
  11. public class gra extends JFrame implements ActionListener{
  12.  
  13.     private String gracz;
  14.     private JButton nowaGra;
  15.     private JButton zakoncz;
  16.     private JLabel wynik;
  17.     private JButton sprawdz;
  18.     private JButton wybierzX;
  19.     private JButton wybierzO;
  20.     private JTextField tekst;
  21.     private JButton ok;
  22.     private JButton[] b = new JButton[9];  
  23.     private static Socket socket = null;
  24.     static DataInputStream in;
  25.     static DataOutputStream out;
  26.    
  27.     private JPanel panel()
  28.     {
  29.         JPanel tlo = new JPanel();
  30.         tlo.setBackground(Color.LIGHT_GRAY);
  31.         tlo.setPreferredSize(new Dimension(800, 400));
  32.         tlo.setLayout(new BoxLayout(tlo, BoxLayout.Y_AXIS));
  33.        
  34.         JPanel menu = new JPanel();
  35.         nowaGra = new JButton("Nowa gra");
  36.         nowaGra.setBackground(Color.GRAY);
  37.         nowaGra.setFont(new Font("Comic Sans", Font.BOLD, 40));
  38.         nowaGra.setPreferredSize(new Dimension(250, 150));
  39.         nowaGra.addActionListener(this);
  40.         menu.add(nowaGra);
  41.        
  42.         sprawdz = new JButton("Wynik");
  43.         sprawdz.setPreferredSize(new Dimension(250, 150));
  44.         sprawdz.setBackground(Color.GRAY);
  45.         sprawdz.setFont(new Font("Comic Sans", Font.BOLD, 40));
  46.         sprawdz.addActionListener(this);
  47.         menu.add(sprawdz); 
  48.        
  49.         zakoncz = new JButton("Zakończ");
  50.         zakoncz.setPreferredSize(new Dimension(250, 150));
  51.         zakoncz.setBackground(Color.GRAY);
  52.         zakoncz.setFont(new Font("Comic Sans", Font.BOLD, 40));
  53.         zakoncz.addActionListener(this);
  54.         menu.add(zakoncz);
  55.        
  56.         JPanel wynikTlo = new JPanel();
  57.         wynikTlo.setBackground(Color.DARK_GRAY);
  58.         wynikTlo.setPreferredSize(new Dimension(750,100));
  59.         wynik = new JLabel();
  60.         wynik.setFont(new Font("Comic Sans", Font.BOLD, 50));
  61.         wynik.setForeground(Color.WHITE);
  62.         wynikTlo.add(wynik);
  63.         menu.add(wynikTlo);
  64.        
  65.         JLabel w = new JLabel();
  66.         w.setText("Wybierz X lub O:");
  67.         w.setFont(new Font("Comic Sans", Font.BOLD, 30));
  68.         menu.add(w);
  69.         menu.add(Box.createRigidArea(new Dimension(100,0)));
  70.        
  71.         wybierzX = new JButton("X");
  72.         wybierzX.setBackground(Color.DARK_GRAY);
  73.         wybierzX.setFont(new Font("Comic Sans", Font.BOLD, 50));
  74.         wybierzX.setPreferredSize(new Dimension(100,100));
  75.         wybierzX.setForeground(Color.WHITE);
  76.         wybierzX.addActionListener(this);
  77.         menu.add(wybierzX);
  78.        
  79.         wybierzO = new JButton("O");
  80.         wybierzO.setBackground(Color.DARK_GRAY);
  81.         wybierzO.setFont(new Font("Comic Sans", Font.BOLD, 50));
  82.         wybierzO.setPreferredSize(new Dimension(100,100));
  83.         wybierzO.setForeground(Color.WHITE);
  84.         wybierzO.addActionListener(this);
  85.         menu.add(wybierzO);
  86.         menu.add(Box.createRigidArea(new Dimension(100,0)));
  87.  
  88.         tlo.add(menu);
  89.        
  90.         JPanel plansza = new JPanel();
  91.         for(int i=0;i<9;i++)
  92.         {
  93.             b[i] = new JButton();
  94.             b[i].addActionListener(this);
  95.             b[i].setPreferredSize(new Dimension(20,20));
  96.             b[i].setFont(new Font("Comic Sans", Font.BOLD, 40));
  97.             b[i].setForeground(Color.BLACK);
  98.             b[i].setBackground(Color.LIGHT_GRAY);
  99.         }
  100.         plansza.setLayout(new GridLayout(3,3));
  101.         plansza.add(b[0]); plansza.add(b[1]); plansza.add(b[2]);
  102.         plansza.add(b[3]); plansza.add(b[4]); plansza.add(b[5]);
  103.         plansza.add(b[6]); plansza.add(b[7]); plansza.add(b[8]);
  104.         tlo.add(plansza);
  105.        
  106.         return tlo;
  107.     }
  108.    
  109.     private JPanel dane()
  110.     {
  111.         JPanel tlo1 = new JPanel();
  112.         tlo1.setBackground(Color.LIGHT_GRAY);
  113.         tlo1.setPreferredSize(new Dimension(800, 400));
  114.         tlo1.setLayout(new BoxLayout(tlo1, BoxLayout.Y_AXIS));
  115.        
  116.         JLabel ww = new JLabel();
  117.         ww.setText("Wprowadz IP:");
  118.         ww.setFont(new Font("Comic Sans", Font.BOLD, 30));
  119.         tlo1.add(ww);
  120.         tlo1.add(Box.createRigidArea(new Dimension(100,0)));
  121.         tekst = new JTextField();
  122.         tekst.setPreferredSize(new Dimension(300,50));
  123.         tekst.setFont(new Font("Comic Sans", Font.BOLD, 20));
  124.         tekst.setBackground(Color.LIGHT_GRAY);
  125.         tlo1.add(tekst);
  126.        
  127.         ok = new JButton("OK");
  128.         ok.setBackground(Color.DARK_GRAY);
  129.         ok.setFont(new Font("Comic Sans", Font.BOLD, 20));
  130.         ok.setPreferredSize(new Dimension(70,50));
  131.         ok.setForeground(Color.WHITE);
  132.         ok.addActionListener(this);
  133.         tlo1.add(ok);
  134.        
  135.         return tlo1;
  136.     }
  137.    
  138.     private boolean wygrana(String s)
  139.     {
  140.         if( (b[0].getLabel() == s && b[1].getLabel() == s && b[2].getLabel() == s) ||
  141.             (b[3].getLabel() == s && b[4].getLabel() == s && b[5].getLabel() == s) ||
  142.             (b[6].getLabel() == s && b[7].getLabel() == s && b[8].getLabel() == s) ||
  143.             (b[0].getLabel() == s && b[4].getLabel() == s && b[8].getLabel() == s) ||
  144.             (b[2].getLabel() == s && b[4].getLabel() == s && b[6].getLabel() == s) ||
  145.             (b[0].getLabel() == s && b[3].getLabel() == s && b[6].getLabel() == s) ||
  146.             (b[1].getLabel() == s && b[4].getLabel() == s && b[7].getLabel() == s) ||
  147.             (b[2].getLabel() == s && b[5].getLabel() == s && b[8].getLabel() == s) )
  148.             return true;
  149.             return false;
  150.     }
  151.    
  152.     public gra()
  153.     {
  154.         super();
  155.         this.getContentPane().add(this.panel());
  156.     }
  157.    
  158.     public static void okno()
  159.     {
  160.         gra g = new gra();
  161.         g.setDefaultCloseOperation(EXIT_ON_CLOSE);
  162.         g.pack();
  163.         g.setLocation(400, 0);
  164.         g.setTitle("GRA: KÓŁKO I KRZYŻYK");
  165.         g.setSize(800, 800);
  166.         g.setResizable(true);
  167.         g.setVisible(true);
  168.     }
  169.  
  170.     @Override
  171.     public void actionPerformed(ActionEvent action) {
  172.         Object source = action.getSource();
  173.         if(source == ok)
  174.         {
  175.             this.getContentPane().add(this.panel());
  176.             dane().setVisible(false);
  177.             panel().setVisible(true);
  178.         }
  179.         else if(((JButton)source).getLabel()=="" && gracz == "X")
  180.         {
  181.             ((JButton)source).setLabel("X");
  182.             gracz = "O";
  183.             nowaGra.setBackground(Color.GRAY);
  184.         }
  185.         else if(((JButton)source).getLabel()=="" && gracz == "O")
  186.         {
  187.             ((JButton)source).setLabel("O");
  188.             gracz = "X";
  189.             nowaGra.setBackground(Color.GRAY);
  190.         }
  191.         else if(source == zakoncz)
  192.         {
  193.             zakoncz.setBackground(Color.RED);
  194.             zakoncz.setForeground(Color.BLACK);
  195.             int w = JOptionPane.showConfirmDialog(this, "Czy na pewno chcesz zamknąć program?", "Pytanie", JOptionPane.YES_NO_OPTION);
  196.             if(w == JOptionPane.YES_OPTION)
  197.             {
  198.                 try {
  199.                     socket.close();
  200.                 } catch (IOException e) {
  201.                     e.printStackTrace();
  202.                 }
  203.                 dispose(); 
  204.             }
  205.             else
  206.             {
  207.                 zakoncz.setBackground(Color.LIGHT_GRAY);
  208.                 zakoncz.setForeground(Color.RED);
  209.             }
  210.         }
  211.         else if(source == nowaGra)
  212.         {
  213.             int w = JOptionPane.showConfirmDialog(this, "Czy na pewno chcesz rozpocząć nową grę?", "Pytanie", JOptionPane.YES_NO_OPTION);
  214.             if(w == JOptionPane.YES_OPTION)
  215.             {
  216.                 nowaGra.setBackground(Color.GREEN);
  217.                 for(int i=0; i<9; i++)
  218.                 {
  219.                     b[i].setLabel("");
  220.                     wynik.setText("");
  221.                     wybierzO.setForeground(Color.WHITE);
  222.                     wybierzO.setEnabled(true);
  223.                     wybierzX.setForeground(Color.WHITE);
  224.                     wybierzX.setEnabled(true);
  225.                     gracz = "";
  226.                 }
  227.             }
  228.         }
  229.         else if(source == sprawdz)
  230.         {
  231.             if(wygrana("X"))
  232.             {
  233.                 wynik.setText("Wygrał zawodnik: X");
  234.             }
  235.             else if(wygrana("O"))
  236.             {
  237.                 wynik.setText("Wygrał zawodnik: O");
  238.             }
  239.             else
  240.             {
  241.                 for(int i=0;i<9;i++)
  242.                 {
  243.                     if(b[i].getLabel()=="")
  244.                     {
  245.                         wynik.setText("Pola są puste!");
  246.                         break;
  247.                     }
  248.                     else if(i==8)
  249.                         wynik.setText("Remis!!!");
  250.                 }
  251.             }
  252.         }
  253.         else if(source == wybierzX)
  254.         {
  255.             wybierzX.setForeground(Color.GREEN);
  256.             nowaGra.setBackground(Color.GRAY);
  257.             gracz = "X";
  258.             wybierzO.setEnabled(false);
  259.         }
  260.         else if(source == wybierzO)
  261.         {
  262.             wybierzO.setForeground(Color.GREEN);
  263.             nowaGra.setBackground(Color.GRAY);
  264.             gracz = "O";
  265.             wybierzX.setEnabled(false);
  266.         }
  267.     }
  268.  
  269.     public static void main(String[] args)throws IOException{
  270.         try{
  271.             socket = new Socket("localhost", 6666);
  272.             out = new DataOutputStream(socket.getOutputStream());
  273.             in = new DataInputStream(socket.getInputStream());
  274.         }catch (UnknownHostException e) {
  275.             System.err.println("Don't know about host: localhost.");
  276.             System.exit(1);
  277.         } catch (IOException e) {
  278.             System.err.println("Couldn't get I/O for "
  279.             + "the connection to: localhost.");
  280.             System.exit(1);
  281.         }
  282.             EventQueue.invokeLater(new Runnable() {
  283.             public void run() {
  284.                 okno();
  285.             }
  286.             });    
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement