Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JButton;
- import java.awt.GridLayout;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionAdapter;
- import java.awt.event.MouseMotionListener;
- import javax.swing.JTextField;
- import javax.swing.JComboBox;
- import javax.swing.ImageIcon;
- import java.awt.Font;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class pole extends JFrame {
- public int player;
- private JPanel contentPane;
- private int first;
- public int xx;
- public int yy;
- public int razm = 50;
- public JButton[][] knopki = new JButton[razm][razm];
- public int[][] map = new int[razm][razm];
- public int x , y;
- public int blueS , redS;
- public pole(int color) {
- player = color;
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 818, 709);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JPanel panel = new JPanel();
- panel.setBounds(0, 0, 802, 600);
- contentPane.add(panel);
- panel.setLayout(new GridLayout(50, 50, 0, 0));
- JLabel lblNewLabel = new JLabel("New label");
- lblNewLabel.setIcon(new ImageIcon("\\\\main\\RDP\\32\u041F\\\u043C\u0438\u0445\u0430\u0439\u043B\u043E\u0432\u0432\u0430\\Desktop\\\u041C\u0414\u041A0302\\KALKULATOR\\\u043A\u0430\u0440\u0442\u0438\u043D\u043A\u0438\\red.jpg"));
- lblNewLabel.setBounds(30, 611, 48, 48);
- contentPane.add(lblNewLabel);
- JLabel label = new JLabel("New label");
- label.setIcon(new ImageIcon("\\\\main\\RDP\\32\u041F\\\u043C\u0438\u0445\u0430\u0439\u043B\u043E\u0432\u0432\u0430\\Desktop\\\u041C\u0414\u041A0302\\KALKULATOR\\\u043A\u0430\u0440\u0442\u0438\u043D\u043A\u0438\\blue.jpg"));
- label.setBounds(196, 611, 48, 48);
- contentPane.add(label);
- JLabel red = new JLabel("0");
- red.setFont(new Font("Tahoma", Font.PLAIN, 20));
- red.setBounds(88, 611, 67, 48);
- contentPane.add(red);
- JLabel blue = new JLabel("0");
- blue.setFont(new Font("Tahoma", Font.PLAIN, 20));
- blue.setBounds(255, 611, 67, 48);
- contentPane.add(blue);
- JButton btnNewButton = new JButton("\u0417\u0430\u043A\u043E\u043D\u0447\u0438\u0442\u044C \u0438\u0433\u0440\u0443");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- if (redS > blueS)
- {
- JOptionPane.showMessageDialog(contentPane, "Победили красные набрав " +redS +" очков" ,"Победа", JOptionPane.INFORMATION_MESSAGE);
- }
- else
- { if (redS < blueS)
- JOptionPane.showMessageDialog(contentPane, "Победили синие набрав " +blueS +" очков" ,"Победа", JOptionPane.INFORMATION_MESSAGE);
- }
- }
- });
- btnNewButton.setBounds(411, 624, 140, 23);
- contentPane.add(btnNewButton);
- JLabel kost1 = new JLabel("New label");
- kost1.setFont(new Font("Tahoma", Font.PLAIN, 20));
- kost1.setBounds(605, 616, 48, 31);
- contentPane.add(kost1);
- JLabel kost2 = new JLabel("New label");
- kost2.setFont(new Font("Tahoma", Font.PLAIN, 20));
- kost2.setBounds(664, 616, 48, 31);
- contentPane.add(kost2);
- x = random();
- y = random();
- kost1.setText(x +"");
- kost2.setText(y +"");
- for (int i = 0; i< razm; i++)
- {
- for (int j = 0; j< razm; j++)
- {
- map[i][j] = 0;
- }
- }
- for (int i = 0; i< knopki.length; i++)
- {
- for (int j = 0; j< knopki.length; j++)
- {
- knopki[i][j] = new JButton();
- panel.add(knopki[i][j]);
- knopki[i][j].setBackground(Color.WHITE);
- }
- }
- for (int i = 0; i< knopki.length; i++)
- {
- for (int j = 0; j< knopki.length; j++)
- {
- knopki[i][j].addMouseMotionListener(new MouseMotionListener() {
- public void mouseMoved(MouseEvent e) {
- rebuild();
- var elem1 = e.getComponent();
- for (int i = 0; i< knopki.length; i++)
- {
- for (int j = 0; j< knopki.length; j++)
- {
- if(knopki[i][j].equals(elem1))
- {
- xx = i;
- yy = j;
- }
- }
- }
- for (int i = xx; i < xx+x; i++)
- {
- for (int j = yy; j< yy+y; j++)
- {
- switch(player)
- {
- case 1:
- knopki[i][j].setBackground(Color.RED);
- break;
- case 2:
- knopki[i][j].setBackground(Color.BLUE);
- break;
- }
- }
- }
- }
- public void mouseDragged(MouseEvent arg0) {
- }
- });
- }
- }
- for (int i = 0; i< knopki.length; i++)
- {
- for (int j = 0; j< knopki.length; j++)
- {
- knopki[i][j].addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- for (int i = xx; i < xx+x; i++)
- {
- for (int j = yy; j< yy+y; j++)
- {
- if(map[i][j] != 0)
- {
- return;
- }
- }
- }
- int chislo = x*y;
- for (int i = xx; i < xx+x; i++)
- {
- for (int j = yy; j< yy+y; j++)
- {
- switch(player)
- {
- case 1:
- knopki[i][j].setBackground(Color.RED);
- map[i][j] = 1;
- break;
- case 2:
- knopki[i][j].setBackground(Color.BLUE);
- map[i][j] = 2;
- break;
- }
- }
- }
- switch(player)
- {
- case 1:
- redS = redS + x*y;
- red.setText(redS+"");
- break;
- case 2:
- blueS += x*y;
- blue.setText(blueS+"");
- break;
- }
- if(player == 1)
- {
- player = 2;
- }
- else
- {
- player = 1;
- }
- x = random();
- y = random();
- kost1.setText(x +"");
- kost2.setText(y +"");
- }
- });
- }
- }
- if (color == 1)
- {
- first = 1;
- }
- else
- {
- first = 2;
- }
- }
- public void rebuild()
- {
- for (int i = 0; i< map.length; i++)
- {
- for (int j = 0; j< map.length; j++)
- {
- if (map[i][j] == 0)
- {
- knopki[i][j].setBackground(Color.WHITE);
- }else
- if (map[i][j] == 1)
- {
- knopki[i][j].setBackground(Color.RED);
- }else
- if (map[i][j] == 2)
- {
- knopki[i][j].setBackground(Color.BLUE);
- }
- }
- }
- }
- public int random()
- {
- int kub = (int) (Math.random()*6+1);
- return kub;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement