Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import javax.swing.*;
- import java.awt.*;
- import java.net.*;
- import java.util.Scanner;
- public class gra extends JFrame implements ActionListener{
- private String gracz;
- private JButton nowaGra;
- private JButton zakoncz;
- private JLabel wynik;
- private JButton sprawdz;
- private JButton wybierzX;
- private JButton wybierzO;
- private JTextField tekst;
- private JButton ok;
- private JButton[] b = new JButton[9];
- private static Socket socket = null;
- static DataInputStream in;
- static DataOutputStream out;
- private JPanel panel()
- {
- JPanel tlo = new JPanel();
- tlo.setBackground(Color.LIGHT_GRAY);
- tlo.setPreferredSize(new Dimension(800, 400));
- tlo.setLayout(new BoxLayout(tlo, BoxLayout.Y_AXIS));
- JPanel menu = new JPanel();
- nowaGra = new JButton("Nowa gra");
- nowaGra.setBackground(Color.GRAY);
- nowaGra.setFont(new Font("Comic Sans", Font.BOLD, 40));
- nowaGra.setPreferredSize(new Dimension(250, 150));
- nowaGra.addActionListener(this);
- menu.add(nowaGra);
- sprawdz = new JButton("Wynik");
- sprawdz.setPreferredSize(new Dimension(250, 150));
- sprawdz.setBackground(Color.GRAY);
- sprawdz.setFont(new Font("Comic Sans", Font.BOLD, 40));
- sprawdz.addActionListener(this);
- menu.add(sprawdz);
- zakoncz = new JButton("Zakończ");
- zakoncz.setPreferredSize(new Dimension(250, 150));
- zakoncz.setBackground(Color.GRAY);
- zakoncz.setFont(new Font("Comic Sans", Font.BOLD, 40));
- zakoncz.addActionListener(this);
- menu.add(zakoncz);
- JPanel wynikTlo = new JPanel();
- wynikTlo.setBackground(Color.DARK_GRAY);
- wynikTlo.setPreferredSize(new Dimension(750,100));
- wynik = new JLabel();
- wynik.setFont(new Font("Comic Sans", Font.BOLD, 50));
- wynik.setForeground(Color.WHITE);
- wynikTlo.add(wynik);
- menu.add(wynikTlo);
- JLabel w = new JLabel();
- w.setText("Wybierz X lub O:");
- w.setFont(new Font("Comic Sans", Font.BOLD, 30));
- menu.add(w);
- menu.add(Box.createRigidArea(new Dimension(100,0)));
- wybierzX = new JButton("X");
- wybierzX.setBackground(Color.DARK_GRAY);
- wybierzX.setFont(new Font("Comic Sans", Font.BOLD, 50));
- wybierzX.setPreferredSize(new Dimension(100,100));
- wybierzX.setForeground(Color.WHITE);
- wybierzX.addActionListener(this);
- menu.add(wybierzX);
- wybierzO = new JButton("O");
- wybierzO.setBackground(Color.DARK_GRAY);
- wybierzO.setFont(new Font("Comic Sans", Font.BOLD, 50));
- wybierzO.setPreferredSize(new Dimension(100,100));
- wybierzO.setForeground(Color.WHITE);
- wybierzO.addActionListener(this);
- menu.add(wybierzO);
- menu.add(Box.createRigidArea(new Dimension(100,0)));
- tlo.add(menu);
- JPanel plansza = new JPanel();
- for(int i=0;i<9;i++)
- {
- b[i] = new JButton();
- b[i].addActionListener(this);
- b[i].setPreferredSize(new Dimension(20,20));
- b[i].setFont(new Font("Comic Sans", Font.BOLD, 40));
- b[i].setForeground(Color.BLACK);
- b[i].setBackground(Color.LIGHT_GRAY);
- }
- plansza.setLayout(new GridLayout(3,3));
- plansza.add(b[0]); plansza.add(b[1]); plansza.add(b[2]);
- plansza.add(b[3]); plansza.add(b[4]); plansza.add(b[5]);
- plansza.add(b[6]); plansza.add(b[7]); plansza.add(b[8]);
- tlo.add(plansza);
- return tlo;
- }
- private JPanel dane()
- {
- JPanel tlo1 = new JPanel();
- tlo1.setBackground(Color.LIGHT_GRAY);
- tlo1.setPreferredSize(new Dimension(800, 400));
- tlo1.setLayout(new BoxLayout(tlo1, BoxLayout.Y_AXIS));
- JLabel ww = new JLabel();
- ww.setText("Wprowadz IP:");
- ww.setFont(new Font("Comic Sans", Font.BOLD, 30));
- tlo1.add(ww);
- tlo1.add(Box.createRigidArea(new Dimension(100,0)));
- tekst = new JTextField();
- tekst.setPreferredSize(new Dimension(300,50));
- tekst.setFont(new Font("Comic Sans", Font.BOLD, 20));
- tekst.setBackground(Color.LIGHT_GRAY);
- tlo1.add(tekst);
- ok = new JButton("OK");
- ok.setBackground(Color.DARK_GRAY);
- ok.setFont(new Font("Comic Sans", Font.BOLD, 20));
- ok.setPreferredSize(new Dimension(70,50));
- ok.setForeground(Color.WHITE);
- ok.addActionListener(this);
- tlo1.add(ok);
- return tlo1;
- }
- private boolean wygrana(String s)
- {
- if( (b[0].getLabel() == s && b[1].getLabel() == s && b[2].getLabel() == s) ||
- (b[3].getLabel() == s && b[4].getLabel() == s && b[5].getLabel() == s) ||
- (b[6].getLabel() == s && b[7].getLabel() == s && b[8].getLabel() == s) ||
- (b[0].getLabel() == s && b[4].getLabel() == s && b[8].getLabel() == s) ||
- (b[2].getLabel() == s && b[4].getLabel() == s && b[6].getLabel() == s) ||
- (b[0].getLabel() == s && b[3].getLabel() == s && b[6].getLabel() == s) ||
- (b[1].getLabel() == s && b[4].getLabel() == s && b[7].getLabel() == s) ||
- (b[2].getLabel() == s && b[5].getLabel() == s && b[8].getLabel() == s) )
- return true;
- return false;
- }
- public gra()
- {
- super();
- this.getContentPane().add(this.panel());
- }
- public static void okno()
- {
- gra g = new gra();
- g.setDefaultCloseOperation(EXIT_ON_CLOSE);
- g.pack();
- g.setLocation(400, 0);
- g.setTitle("GRA: KÓŁKO I KRZYŻYK");
- g.setSize(800, 800);
- g.setResizable(true);
- g.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent action) {
- Object source = action.getSource();
- if(source == ok)
- {
- this.getContentPane().add(this.panel());
- dane().setVisible(false);
- panel().setVisible(true);
- }
- else if(((JButton)source).getLabel()=="" && gracz == "X")
- {
- ((JButton)source).setLabel("X");
- gracz = "O";
- nowaGra.setBackground(Color.GRAY);
- }
- else if(((JButton)source).getLabel()=="" && gracz == "O")
- {
- ((JButton)source).setLabel("O");
- gracz = "X";
- nowaGra.setBackground(Color.GRAY);
- }
- else if(source == zakoncz)
- {
- zakoncz.setBackground(Color.RED);
- zakoncz.setForeground(Color.BLACK);
- int w = JOptionPane.showConfirmDialog(this, "Czy na pewno chcesz zamknąć program?", "Pytanie", JOptionPane.YES_NO_OPTION);
- if(w == JOptionPane.YES_OPTION)
- {
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- dispose();
- }
- else
- {
- zakoncz.setBackground(Color.LIGHT_GRAY);
- zakoncz.setForeground(Color.RED);
- }
- }
- else if(source == nowaGra)
- {
- int w = JOptionPane.showConfirmDialog(this, "Czy na pewno chcesz rozpocząć nową grę?", "Pytanie", JOptionPane.YES_NO_OPTION);
- if(w == JOptionPane.YES_OPTION)
- {
- nowaGra.setBackground(Color.GREEN);
- for(int i=0; i<9; i++)
- {
- b[i].setLabel("");
- wynik.setText("");
- wybierzO.setForeground(Color.WHITE);
- wybierzO.setEnabled(true);
- wybierzX.setForeground(Color.WHITE);
- wybierzX.setEnabled(true);
- gracz = "";
- }
- }
- }
- else if(source == sprawdz)
- {
- if(wygrana("X"))
- {
- wynik.setText("Wygrał zawodnik: X");
- }
- else if(wygrana("O"))
- {
- wynik.setText("Wygrał zawodnik: O");
- }
- else
- {
- for(int i=0;i<9;i++)
- {
- if(b[i].getLabel()=="")
- {
- wynik.setText("Pola są puste!");
- break;
- }
- else if(i==8)
- wynik.setText("Remis!!!");
- }
- }
- }
- else if(source == wybierzX)
- {
- wybierzX.setForeground(Color.GREEN);
- nowaGra.setBackground(Color.GRAY);
- gracz = "X";
- wybierzO.setEnabled(false);
- }
- else if(source == wybierzO)
- {
- wybierzO.setForeground(Color.GREEN);
- nowaGra.setBackground(Color.GRAY);
- gracz = "O";
- wybierzX.setEnabled(false);
- }
- }
- public static void main(String[] args)throws IOException{
- try{
- socket = new Socket("localhost", 6666);
- out = new DataOutputStream(socket.getOutputStream());
- in = new DataInputStream(socket.getInputStream());
- }catch (UnknownHostException e) {
- System.err.println("Don't know about host: localhost.");
- System.exit(1);
- } catch (IOException e) {
- System.err.println("Couldn't get I/O for "
- + "the connection to: localhost.");
- System.exit(1);
- }
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- okno();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement