Advertisement
Guest User

Untitled

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