Advertisement
Guest User

Untitled

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